summaryrefslogtreecommitdiff
path: root/app/src/neigh.c
blob: bb7aa3005c4d5808da4630570d432453f8a20309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* \brief 简单协议栈邻居子系统
*
* 处理与主机邻接通信的所需的信息
*
* \author Lu Qiuwen<[email protected]>
* \date 2016-10-21
*/


#include <rte_hash.h>
#include <rte_rwlock.h>
#include <rte_atomic.h>
#include <rte_ether.h>
#include <rte_ip_frag.h>
#include <rte_timer.h>
#include <rte_hash_crc.h>
#include <rte_errno.h>
#include <errno.h>
#include <assert.h>
#include <common.h>
#include <neigh.h>
#include <vdev_define.h>
#include <arp.h>
#include <cJSON.h>
#include <arpa/inet.h>

enum neigh_state
{
	NEIGH_IN_NONE = 0,
	NEIGH_IN_COMPLETE = 1,
	NEIGH_IN_USE = 2,
	NEIGH_IN_TIMEOUT = 3,
	NEIGH_IN_DEAD = 4
};

static const char * str_neigh_state[] =
{
	[NEIGH_IN_NONE] = "IN_NONE",
	[NEIGH_IN_COMPLETE] = "IN_COMPLETE",
	[NEIGH_IN_USE] = "IN_USE",
	[NEIGH_IN_TIMEOUT] = "IN_TIMEOUT",
	[NEIGH_IN_DEAD] = "IN_DEAD"
};

/* Neighbour Table Object Rule */
struct neighbour
{
	/* 链表项 */
	TAILQ_ENTRY(neighbour) next;
	/* IP地址 */
	struct in_addr in_addr;
	/* 目的MAC地址 */
	struct ether_addr eth_addr;
	/* 设备描述符 */
    struct vdev_instance * vdi;
	/* 表项状态 */
	enum neigh_state state;
	/* 转入当前状态的时间 */
	time_t t_start_state;
	/* 上次发送ARP请求的时间 */
	time_t t_last_request;
	/* 上次收到ARP应答的时间 */
	time_t t_last_update;
	/* 永久标志位 */
	unsigned short en_permanent;
};

int neighbour_mamanger_init(struct neighbour_manager * object,
	const char * symbol, unsigned int max_entries, unsigned short t_timeout, unsigned int t_arp_send)
{
	int ret = 0;

	char tbsymbol[MR_SYMBOL_MAX];
	snprintf(tbsymbol, sizeof(tbsymbol), "NEIGH_%s", symbol);

	struct rte_hash_parameters hash_params =
	{
		.name = tbsymbol,
		.entries = max_entries,
		.key_len = sizeof(struct in_addr),
		.hash_func = NULL,
		.hash_func_init_val = 0,
        .socket_id = SOCKET_ID_ANY
	};

    /* 检查是否存在以前的哈希表 */
    struct rte_hash * exist_hash = rte_hash_find_existing(tbsymbol);
    if (exist_hash != NULL) rte_hash_free(exist_hash);

	object->table = rte_hash_create(&hash_params);
	if (unlikely(object->table == NULL))
	{
		MR_ERROR("Cannot create hash table for %s : %s", symbol, __str_rte_errno());
		ret = -1; goto errout;
	}

	TAILQ_INIT(&object->in_none_list);
	rte_atomic16_init(&object->in_none_list_len);
	rte_rwlock_init(&object->rwlock);

    object->t_arp_send = t_arp_send;
    object->t_timeout = t_timeout;
	return 0;

errout:
	if (object->table != NULL) rte_hash_free(object->table);
	return ret;
}

int neighbour_mamanger_deinit(struct neighbour_manager * object)
{
	rte_hash_free(object->table);
	return 0;
}

static void __change_neigh_state(struct neighbour * neigh, enum neigh_state target_state)
{
	if (target_state != neigh->state)
	{
		neigh->state = target_state;
		neigh->t_start_state = time(NULL);
	}
}

static struct neighbour * __neigh_create_and_join_hash(struct neighbour_manager * object, 
	struct in_addr in_addr, struct ether_addr * eth_addr, 
	struct vdev_instance * vdi, unsigned short en_permanent)
{
	struct neighbour * neigh = rte_zmalloc(NULL, sizeof(struct neighbour), 0);
	if (unlikely(neigh == NULL))
	{
        MR_ERROR("Cannot create neigh structure : %s", __str_rte_errno());
		return NULL;
	}

	neigh->in_addr = in_addr;
    neigh->vdi = vdi;
	neigh->en_permanent = en_permanent;
	neigh->t_last_update = 0;
	neigh->t_last_request = 0;
	neigh->state = NEIGH_IN_NONE;

	if(eth_addr != NULL)
	{
		ether_addr_copy(eth_addr, &neigh->eth_addr);
		__change_neigh_state(neigh, NEIGH_IN_USE);
	}
	
	if(en_permanent)
	{
		neigh->en_permanent = 1;
	}

	hash_sig_t hash = rte_hash_crc(&in_addr, sizeof(in_addr), 0);
	int ret = rte_hash_add_key_with_hash_data(object->table, &in_addr, hash, neigh);
	if (unlikely(ret < 0))
	{
		MR_ERROR("Insert neigh to hash table failed : %s", __str_rte_errno());
		goto errout;
	}

	return neigh;
	
errout:
	if (neigh) rte_free(neigh);
	return NULL;
}

static void __neigh_update(struct neighbour * neigh, struct in_addr in_addr,
	struct ether_addr * eth_addr, struct vdev_instance * vdi, unsigned short en_permanent)
{
	assert(neigh != NULL);
	neigh->in_addr = in_addr;
	neigh->en_permanent = en_permanent;
	neigh->vdi = vdi;
	
	if(eth_addr != NULL)
	{
		ether_addr_copy(eth_addr, &neigh->eth_addr);
		__change_neigh_state(neigh, NEIGH_IN_USE);
	}

	return;
}

int neigh_create_or_update(struct neighbour_manager * object, struct in_addr in_addr, 
	struct ether_addr * eth_addr, struct vdev_instance * vdi, unsigned short en_permanent)
{
	rte_rwlock_write_lock(&object->rwlock);
	struct neighbour * neigh = NULL;
	hash_sig_t hash = rte_hash_crc(&in_addr, sizeof(in_addr), 0);

	int ret = rte_hash_lookup_with_hash_data(object->table, &in_addr, hash, (void **)&neigh);
	if (ret < 0)
	{
		neigh = __neigh_create_and_join_hash(object, in_addr, eth_addr, vdi, en_permanent);
	}
	else
	{
		__neigh_update(neigh, in_addr, eth_addr, vdi, en_permanent);
	}

    if (unlikely(neigh == NULL)) { ret = -1; goto exit; }

    time_t now_time = time(NULL);
	if (neigh->state == NEIGH_IN_NONE && (now_time - neigh->t_last_request) >= object->t_arp_send)
	{
        arp_request_send(vdi, 0, neigh->in_addr);
        neigh->t_last_request = now_time;
	}

	ret = 0; goto exit;
	
exit:
	rte_rwlock_write_unlock(&object->rwlock);
	return ret;
}

int neigh_delete(struct neighbour_manager * object, struct in_addr in_addr)
{
	rte_rwlock_write_lock(&object->rwlock);
	struct neighbour * neigh = NULL;

	hash_sig_t hash = rte_hash_crc(&in_addr, sizeof(in_addr), 0);
	int ret = rte_hash_lookup_with_hash_data(object->table, &in_addr, hash, (void **)&neigh);
	if (ret < 0) goto exit;

	// 当处于IN_NONE状态时,从IN_NONE列表中移除
	if (neigh->state == NEIGH_IN_NONE)
	{
		TAILQ_REMOVE(&object->in_none_list,neigh, next);
		rte_atomic16_dec(&object->in_none_list_len);
	}

	rte_hash_del_key_with_hash(object->table, &in_addr, hash);
	rte_free(neigh);
	ret = 0; goto exit;

exit:
	rte_rwlock_write_unlock(&object->rwlock);
	return ret;
}

int neigh_query(struct neighbour_manager * object, struct in_addr in_addr,
	struct ether_addr * out_ether_addr, struct vdev_instance ** vdi)
{
	struct neighbour * neigh = NULL;

	rte_rwlock_read_lock(&object->rwlock);
	hash_sig_t hash = rte_hash_crc(&in_addr, sizeof(in_addr), 0);
	int ret = rte_hash_lookup_with_hash_data(object->table, &in_addr, hash, (void **)&neigh);
	
	// 没查到
	if (ret < 0) goto exit;

	// 以下状态信息不全,无法对外提供查询
	if (neigh->state == NEIGH_IN_NONE ||
		neigh->state == NEIGH_IN_COMPLETE ||
		neigh->state == NEIGH_IN_DEAD)
	{
		ret = -EFAULT; goto exit;
	}
	
	ether_addr_copy(&neigh->eth_addr, out_ether_addr);
    *vdi = neigh->vdi;
	ret = 0; goto exit;

exit:
	rte_rwlock_read_unlock(&object->rwlock);
	return ret;
}

static void __neigh_handle_in_no_queue(struct neighbour_manager * object, queue_id_t qid)
{
	if (rte_atomic16_read(&object->in_none_list_len) == 0) return;
	
	rte_rwlock_write_lock(&object->rwlock);
	struct neighbour * neigh_iter;
	
	// 对每一个在In-None列表中的Neigh,发ARP请求。
	TAILQ_FOREACH(neigh_iter, &object->in_none_list, next)
	{
        assert(neigh_iter->vdi != NULL);
		arp_request_send(neigh_iter->vdi, qid, neigh_iter->in_addr);

		// 处理完毕,移除这一项,因哈希表中还有引用,不释放空间
		TAILQ_REMOVE(&object->in_none_list, neigh_iter, next);
		rte_atomic16_dec(&object->in_none_list_len);
	}

	// In-None列表应当为空,数量计数应当为0
	assert(rte_atomic16_read(&object->in_none_list_len) == 0);
	rte_rwlock_write_unlock(&object->rwlock);
}

void neighbour_manager_loop_entry(struct neighbour_manager * object, queue_id_t qid)
{
	__neigh_handle_in_no_queue(object, qid);
}

cJSON * neighbour_manager_monit(struct neighbour_manager * object)
{
	struct cJSON * j_root = cJSON_CreateArray();
	MR_VERIFY_MALLOC(j_root);

	struct in_addr iter_in_addr;
	struct neighbour * iter_neigh;
	uint32_t iterate = 0;

	rte_rwlock_read_lock(&object->rwlock);
	while (rte_hash_iterate(object->table, (const void **)&iter_in_addr, 
		(void **)&iter_neigh, &iterate) >= 0)
	{
		char str_in_addr[MR_STRING_MAX] = { 0 };
		inet_ntop(AF_INET, &iter_neigh->in_addr, str_in_addr, INET_ADDRSTRLEN);

		char str_ether_addr[MR_STRING_MAX] = { 0 };
		ether_format_addr(str_ether_addr, sizeof(str_ether_addr), &iter_neigh->eth_addr);
		
		cJSON * j_neigh = cJSON_CreateObject();
		cJSON_AddStringToObject(j_neigh, "InAddress", str_in_addr);
		cJSON_AddStringToObject(j_neigh, "HWAddress", str_ether_addr);
		cJSON_AddStringToObject(j_neigh, "Interface", iter_neigh->vdi->vdev->symbol);
		cJSON_AddStringToObject(j_neigh, "State", str_neigh_state[iter_neigh->state]);

		cJSON_AddItemToArray(j_root, j_neigh);
	}
	
	rte_rwlock_read_unlock(&object->rwlock);
	return j_root;
}