2 // Copyright (c) 2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
23 #include <rte_common.h>
24 #include <rte_malloc.h>
26 #include <rte_byteorder.h>
28 #include <rte_table_lpm.h>
29 #include <rte_table_hash.h>
30 #include <rte_pipeline.h>
34 #include <rte_jhash.h>
35 #include <rte_cycles.h>
36 #include <rte_timer.h>
37 #include "interface.h"
40 #include "l3fwd_lpm4.h"
41 #include "vnf_common.h"
43 #if (RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN)
44 #define CHECK_ENDIAN_16(x) rte_be_to_cpu_16(x)
45 #define CHECK_ENDIAN_32(x) rte_be_to_cpu_32(x)
47 #define CHECK_ENDIAN_16(x) (x)
48 #define CHECK_ENDIAN_32(x) (x)
51 #define NB_ARPICMP_MBUF 64
52 #define NB_NDICMP_MBUF 64
53 #define IP_VERSION_4 0x40
54 #define IP_HDRLEN 0x05 /**< default IP header length == five 32-bits words. */
55 #define IP_VHL_DEF (IP_VERSION_4 | IP_HDRLEN)
57 #define is_multicast_ipv4_addr(ipv4_addr) \
58 (((rte_be_to_cpu_32((ipv4_addr)) >> 24) & 0x000000FF) == 0xE0)
60 extern uint8_t prv_in_port_a[16];
61 extern uint32_t timer_lcore;
62 uint32_t arp_timeout = ARP_TIMER_EXPIRY;
67 static int my_inet_pton_ipv6(int af, const char *src, void *dst);
68 static int inet_pton_ipv6(const char *src, unsigned char *dst);
69 static int inet_pton_ipv4(const char *src, unsigned char *dst);
70 extern void convert_prefixlen_to_netmask_ipv6(uint32_t depth,
71 uint8_t netmask_ipv6[]);
73 uint8_t vnf_common_arp_lib_init;
74 uint8_t vnf_common_nd_lib_init;
75 uint8_t loadb_pipeline_count;
77 uint32_t ARPICMP_DEBUG;
78 uint32_t NDIPV6_DEBUG;
80 uint32_t arp_route_tbl_index;
81 uint32_t nd_route_tbl_index;
82 uint32_t link_hw_addr_array_idx;
84 uint32_t lib_arp_get_mac_req;
85 uint32_t lib_arp_nh_found;
86 uint32_t lib_arp_no_nh_found;
87 uint32_t lib_arp_arp_entry_found;
88 uint32_t lib_arp_no_arp_entry_found;
89 uint32_t lib_arp_populate_called;
90 uint32_t lib_arp_delete_called;
91 uint32_t lib_arp_duplicate_found;
93 uint32_t lib_nd_get_mac_req;
94 uint32_t lib_nd_nh_found;
95 uint32_t lib_nd_no_nh_found;
96 uint32_t lib_nd_nd_entry_found;
97 uint32_t lib_nd_no_arp_entry_found;
98 uint32_t lib_nd_populate_called;
99 uint32_t lib_nd_delete_called;
100 uint32_t lib_nd_duplicate_found;
101 struct rte_mempool *lib_arp_pktmbuf_tx_pool;
102 struct rte_mempool *lib_nd_pktmbuf_tx_pool;
104 struct rte_mbuf *lib_arp_pkt;
105 struct rte_mbuf *lib_nd_pkt;
107 uint8_t default_ether_addr[6] = { 0, 0, 0, 0, 1, 1 };
108 uint8_t default_ip[4] = { 0, 0, 1, 1 };
110 static struct rte_hash_parameters arp_hash_params = {
114 .key_len = sizeof(struct arp_key_ipv4),
115 .hash_func = rte_jhash,
116 .hash_func_init_val = 0,
119 static struct rte_hash_parameters nd_hash_params = {
123 .key_len = sizeof(struct nd_key_ipv6),
124 .hash_func = rte_jhash,
125 .hash_func_init_val = 0,
128 struct rte_hash *arp_hash_handle;
129 struct rte_hash *nd_hash_handle;
131 void print_pkt1(struct rte_mbuf *pkt);
133 struct app_params *myApp;
134 struct rte_pipeline *myP;
135 uint8_t num_vnf_threads;
138 * A structure for Arp port address
140 struct arp_port_address {
141 uint32_t ip; /**< Ip address */
142 uint8_t mac_addr[6]; /**< Mac address */
145 struct arp_port_address arp_port_addresses[RTE_MAX_ETHPORTS];
146 struct rte_mempool *timer_mempool_arp;
148 int timer_objs_mempool_count = 70000;
150 #define MAX_NUM_ARP_ENTRIES 64
151 #define MAX_NUM_ND_ENTRIES 64
153 uint32_t get_nh(uint32_t, uint32_t *);
154 void get_nh_ipv6(uint8_t ipv6[], uint32_t *port, uint8_t nhipv6[]);
156 #define MAX_ARP_DATA_ENTRY_TABLE 7
158 struct table_arp_entry_data arp_entry_data_table[MAX_ARP_DATA_ENTRY_TABLE] = {
159 {{0, 0, 0, 0, 0, 1}, 1, INCOMPLETE, IPv4(192, 168, 0, 2)},
160 {{0, 0, 0, 0, 0, 2}, 0, INCOMPLETE, IPv4(192, 168, 0, 3)},
161 {{0, 0, 0, 0, 0, 1}, 1, INCOMPLETE, IPv4(30, 40, 50, 60)},
162 {{0, 0, 0, 0, 0, 1}, 1, INCOMPLETE, IPv4(120, 0, 0, 2)},
163 {{0, 0, 0, 0, 0, 4}, 3, INCOMPLETE, IPv4(1, 1, 1, 4)},
164 {{0, 0, 0, 0, 0, 5}, 4, INCOMPLETE, IPv4(1, 1, 1, 5)},
165 {{0, 0, 0, 0, 0, 6}, 1, INCOMPLETE, IPv4(1, 1, 1, 7)},
168 #define MAX_ND_DATA_ENTRY_TABLE 7
169 struct table_nd_entry_data nd_entry_data_table[MAX_ND_DATA_ENTRY_TABLE] = {
170 {{0, 0, 0, 0, 0, 8}, 1, INCOMPLETE,
171 {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, 0},
173 {{0, 0, 0, 0, 0, 9}, 1, INCOMPLETE,
174 {20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20}, 0},
175 {{0, 0, 0, 0, 0, 10}, 2, INCOMPLETE,
176 {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1}, 0},
177 {{0, 0, 0, 0, 0, 11}, 3, INCOMPLETE,
178 {4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1}, 0},
179 {{0, 0, 0, 0, 0, 12}, 4, INCOMPLETE,
180 {5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1}, 0},
181 {{0, 0, 0, 0, 0, 13}, 5, INCOMPLETE,
182 {6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1}, 0},
183 {{0, 0, 0, 0, 0, 14}, 6, INCOMPLETE,
184 {7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1}, 0},
187 struct lib_nd_route_table_entry lib_nd_route_table[MAX_ND_RT_ENTRY] = {
188 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
189 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
190 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
191 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
192 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
193 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
194 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
195 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
196 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
197 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
198 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
199 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
200 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
201 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
202 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
203 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
204 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
205 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
206 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
207 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
208 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
209 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
210 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
211 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
212 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
213 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
214 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
215 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
216 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
217 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
218 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0,
219 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }
222 struct lib_arp_route_table_entry lib_arp_route_table[MAX_ARP_RT_ENTRY] = {
241 void print_trace(void);
243 /* Obtain a backtrace and print it to stdout. */
244 void print_trace(void)
251 size = backtrace(array, 10);
252 strings = backtrace_symbols(array, size);
254 RTE_LOG(INFO, LIBARP, "Obtained %zd stack frames.\n", size);
256 for (i = 0; i < size; i++)
257 RTE_LOG(INFO, LIBARP, "%s\n", strings[i]);
262 uint32_t get_nh(uint32_t ip, uint32_t *port)
265 for (i = 0; i < MAX_ARP_RT_ENTRY; i++) {
266 if (((lib_arp_route_table[i].
267 ip & lib_arp_route_table[i].mask) ==
268 (ip & lib_arp_route_table[i].mask))) {
270 *port = lib_arp_route_table[i].port;
272 return lib_arp_route_table[i].nh;
275 printf("No nh match ip 0x%x, port %u, t_ip "
276 "0x%x, t_port %u, mask 0x%x, r1 %x, r2 %x\n",
277 ip, *port, lib_arp_route_table[i].ip,
278 lib_arp_route_table[i].port,
279 lib_arp_route_table[i].mask,
280 (lib_arp_route_table[i].ip &
281 lib_arp_route_table[i].mask),
282 (ip & lib_arp_route_table[i].mask));
285 printf("No NH - ip 0x%x, port %u\n", ip, *port);
286 lib_arp_no_nh_found++;
291 void get_nh_ipv6(uint8_t ipv6[], uint32_t *port, uint8_t nhipv6[])
294 uint8_t netmask_ipv6[16], netip_nd[16], netip_in[16];
295 uint8_t k = 0, l = 0, depthflags = 0, depthflags1 = 0;
296 memset(netmask_ipv6, 0, sizeof(netmask_ipv6));
297 memset(netip_nd, 0, sizeof(netip_nd));
298 memset(netip_in, 0, sizeof(netip_in));
301 for (i = 0; i < MAX_ARP_RT_ENTRY; i++) {
303 convert_prefixlen_to_netmask_ipv6(lib_nd_route_table[i].depth,
306 for (k = 0; k < 16; k++) {
307 if (lib_nd_route_table[i].ipv6[k] & netmask_ipv6[k]) {
309 netip_nd[k] = lib_nd_route_table[i].ipv6[k];
313 for (l = 0; l < 16; l++) {
314 if (ipv6[l] & netmask_ipv6[l]) {
316 netip_in[l] = ipv6[l];
320 if ((depthflags == depthflags1)
321 && (memcmp(netip_nd, netip_in, sizeof(netip_nd)) == 0)) {
322 //&& (lib_nd_route_table[i].port == port))
323 *port = lib_nd_route_table[i].port;
326 for (j = 0; j < 16; j++)
327 nhipv6[j] = lib_nd_route_table[i].nhipv6[j];
333 printf("No nh match\n");
338 printf("No NH - ip 0x%x, port %u\n", ipv6[0], *port);
339 lib_nd_no_nh_found++;
342 /* Added for Multiport changes*/
343 int get_dest_mac_addr_port(const uint32_t ipaddr,
344 uint32_t *phy_port, struct ether_addr *hw_addr)
346 lib_arp_get_mac_req++;
349 nhip = get_nh(ipaddr, phy_port);
352 printf("ARPICMP no nh found for ip %x, port %d\n",
358 struct arp_entry_data *ret_arp_data = NULL;
359 struct arp_key_ipv4 tmp_arp_key;
360 tmp_arp_key.port_id = *phy_port; /* Changed for Multi Port */
361 tmp_arp_key.ip = nhip;
364 printf("%s: nhip: %x, phyport: %d\n", __FUNCTION__, nhip,
367 ret_arp_data = retrieve_arp_entry(tmp_arp_key);
368 if (ret_arp_data == NULL) {
371 ("ARPICMP no arp entry found for ip %x, port %d\n",
377 printf("CG-NAPT requesting ARP for ip %x, "
378 "port %d\n", nhip, *phy_port);
379 request_arp(*phy_port, nhip); //Changed for Multiport
382 lib_arp_no_arp_entry_found++;
383 return ARP_NOT_FOUND;
385 ether_addr_copy(&ret_arp_data->eth_addr, hw_addr);
386 lib_arp_arp_entry_found++;
388 printf("%s: ARPICMP hwaddr found\n", __FUNCTION__);
392 int get_dest_mac_address(const uint32_t ipaddr, uint32_t *phy_port,
393 struct ether_addr *hw_addr, uint32_t *nhip)
395 lib_arp_get_mac_req++;
397 *nhip = get_nh(ipaddr, phy_port);
399 if (ARPICMP_DEBUG && ipaddr)
400 RTE_LOG(INFO, LIBARP,
401 "ARPICMP no nh found for ip %x, port %d\n",
406 struct arp_entry_data *ret_arp_data = NULL;
407 struct arp_key_ipv4 tmp_arp_key;
408 tmp_arp_key.port_id = *phy_port;
409 tmp_arp_key.ip = *nhip;
411 ret_arp_data = retrieve_arp_entry(tmp_arp_key);
412 if (ret_arp_data == NULL) {
413 if (ARPICMP_DEBUG && ipaddr) {
414 RTE_LOG(INFO, LIBARP,
415 "ARPICMP no arp entry found for ip %x, port %d\n",
419 lib_arp_no_arp_entry_found++;
422 ether_addr_copy(&ret_arp_data->eth_addr, hw_addr);
423 lib_arp_arp_entry_found++;
428 int get_dest_mac_addr(const uint32_t ipaddr,
429 uint32_t *phy_port, struct ether_addr *hw_addr)
431 lib_arp_get_mac_req++;
434 nhip = get_nh(ipaddr, phy_port);
436 if (ARPICMP_DEBUG && ipaddr)
437 RTE_LOG(INFO, LIBARP,
438 "ARPICMP no nh found for ip %x, port %d\n",
443 struct arp_entry_data *ret_arp_data = NULL;
444 struct arp_key_ipv4 tmp_arp_key;
445 tmp_arp_key.port_id = *phy_port;
446 tmp_arp_key.ip = nhip;
448 ret_arp_data = retrieve_arp_entry(tmp_arp_key);
449 if (ret_arp_data == NULL) {
450 if (ARPICMP_DEBUG && ipaddr) {
452 ("ARPICMP no arp entry found for ip %x, port %d\n",
458 if (ARPICMP_DEBUG > 4)
460 ("CG-NAPT requesting ARP for ip %x, port %d\n",
462 if (ifm_chk_port_ipv4_enabled(*phy_port)) {
463 request_arp(*phy_port, nhip);
466 RTE_LOG(INFO, LIBARP,
467 "%s: IP is not enabled on port %u, not sending ARP REQ\n\r",
468 __FUNCTION__, *phy_port);
472 lib_arp_no_arp_entry_found++;
475 ether_addr_copy(&ret_arp_data->eth_addr, hw_addr);
476 lib_arp_arp_entry_found++;
480 int get_dest_mac_address_ipv6_port(uint8_t ipv6addr[], uint32_t *phy_port,
481 struct ether_addr *hw_addr, uint8_t nhipv6[])
483 int i = 0, j = 0, flag = 0;
484 lib_nd_get_mac_req++;
486 get_nh_ipv6(ipv6addr, phy_port, nhipv6);
487 for (j = 0; j < 16; j++) {
493 printf("NDIPV6 no nh found for ipv6 "
494 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
495 "%02x%02x%02x%02x%02x%02x, port %d\n",
496 ipv6addr[0], ipv6addr[1], ipv6addr[2],
497 ipv6addr[3], ipv6addr[4], ipv6addr[5],
498 ipv6addr[6], ipv6addr[7], ipv6addr[8],
499 ipv6addr[9], ipv6addr[10], ipv6addr[11],
500 ipv6addr[12], ipv6addr[13], ipv6addr[14],
501 ipv6addr[15], *phy_port);
505 struct nd_entry_data *ret_nd_data = NULL;
506 struct nd_key_ipv6 tmp_nd_key;
507 tmp_nd_key.port_id = *phy_port;
509 for (i = 0; i < 16; i++)
510 tmp_nd_key.ipv6[i] = nhipv6[i];
512 ret_nd_data = retrieve_nd_entry(tmp_nd_key);
513 if (ret_nd_data == NULL) {
515 printf("NDIPV6 no nd entry found for ip %x, port %d\n",
516 ipv6addr[0], *phy_port);
518 lib_nd_no_arp_entry_found++;
521 ether_addr_copy(&ret_nd_data->eth_addr, hw_addr);
522 lib_nd_nd_entry_found++;
526 int get_dest_mac_address_ipv6(uint8_t ipv6addr[], uint32_t *phy_port,
527 struct ether_addr *hw_addr, uint8_t nhipv6[])
529 int i = 0, j = 0, flag = 0;
530 lib_nd_get_mac_req++;
532 get_nh_ipv6(ipv6addr, phy_port, nhipv6);
533 for (j = 0; j < 16; j++) {
539 if (NDIPV6_DEBUG && ipv6addr)
540 RTE_LOG(INFO, LIBARP,
541 "NDIPV6 no nh found for ipv6 %x, port %d\n",
542 ipv6addr[0], *phy_port);
546 struct nd_entry_data *ret_nd_data = NULL;
547 struct nd_key_ipv6 tmp_nd_key;
548 tmp_nd_key.port_id = *phy_port;
550 for (i = 0; i < 16; i++) {
551 tmp_nd_key.ipv6[i] = nhipv6[i];
554 ret_nd_data = retrieve_nd_entry(tmp_nd_key);
555 if (ret_nd_data == NULL) {
556 if (NDIPV6_DEBUG && ipv6addr) {
557 RTE_LOG(INFO, LIBARP,
558 "NDIPV6 no nd entry found for ip %x, port %d\n",
559 ipv6addr[0], *phy_port);
562 if (ARPICMP_DEBUG > 4)
564 ("Requesting ARP for ipv6 addr and port %d\n",
566 request_nd(&nhipv6[0], ifm_get_port(*phy_port));
570 lib_nd_no_arp_entry_found++;
573 ether_addr_copy(&ret_nd_data->eth_addr, hw_addr);
574 lib_nd_nd_entry_found++;
579 * A structure for arp entries in Arp table
582 struct lib_arp_arp_table_entry {
583 struct rte_pipeline_table_entry head;
584 uint64_t macaddr; /**< Mac address */
587 static const char *arp_op_name(uint16_t arp_op)
589 switch (CHECK_ENDIAN_16(arp_op)) {
590 case (ARP_OP_REQUEST):
591 return "ARP Request";
594 case (ARP_OP_REVREQUEST):
595 return "Reverse ARP Request";
596 case (ARP_OP_REVREPLY):
597 return "Reverse ARP Reply";
598 case (ARP_OP_INVREQUEST):
599 return "Peer Identify Request";
600 case (ARP_OP_INVREPLY):
601 return "Peer Identify Reply";
605 return "Unkwown ARP op";
608 static void print_icmp_packet(struct icmp_hdr *icmp_h)
610 RTE_LOG(INFO, LIBARP, " ICMP: type=%d (%s) code=%d id=%d seqnum=%d\n",
612 (icmp_h->icmp_type == IP_ICMP_ECHO_REPLY ? "Reply" :
613 (icmp_h->icmp_type ==
614 IP_ICMP_ECHO_REQUEST ? "Reqest" : "Undef")),
615 icmp_h->icmp_code, CHECK_ENDIAN_16(icmp_h->icmp_ident),
616 CHECK_ENDIAN_16(icmp_h->icmp_seq_nb));
619 static void print_ipv4_h(struct ipv4_hdr *ip_h)
621 struct icmp_hdr *icmp_h =
622 (struct icmp_hdr *)((char *)ip_h + sizeof(struct ipv4_hdr));
623 RTE_LOG(INFO, LIBARP, " IPv4: Version=%d HLEN=%d Type=%d Length=%d\n",
624 (ip_h->version_ihl & 0xf0) >> 4, (ip_h->version_ihl & 0x0f),
625 ip_h->type_of_service, rte_cpu_to_be_16(ip_h->total_length));
626 if (ip_h->next_proto_id == IPPROTO_ICMP) {
627 print_icmp_packet(icmp_h);
631 static void print_arp_packet(struct arp_hdr *arp_h)
633 RTE_LOG(INFO, LIBARP, " ARP: hrd=%d proto=0x%04x hln=%d "
634 "pln=%d op=%u (%s)\n",
635 CHECK_ENDIAN_16(arp_h->arp_hrd),
636 CHECK_ENDIAN_16(arp_h->arp_pro), arp_h->arp_hln,
637 arp_h->arp_pln, CHECK_ENDIAN_16(arp_h->arp_op),
638 arp_op_name(arp_h->arp_op));
640 if (CHECK_ENDIAN_16(arp_h->arp_hrd) != ARP_HRD_ETHER) {
641 RTE_LOG(INFO, LIBARP,
642 "incorrect arp_hrd format for IPv4 ARP (%d)\n",
644 } else if (CHECK_ENDIAN_16(arp_h->arp_pro) != ETHER_TYPE_IPv4) {
645 RTE_LOG(INFO, LIBARP,
646 "incorrect arp_pro format for IPv4 ARP (%d)\n",
648 } else if (arp_h->arp_hln != 6) {
649 RTE_LOG(INFO, LIBARP,
650 "incorrect arp_hln format for IPv4 ARP (%d)\n",
652 } else if (arp_h->arp_pln != 4) {
653 RTE_LOG(INFO, LIBARP,
654 "incorrect arp_pln format for IPv4 ARP (%d)\n",
657 RTE_LOG(INFO, LIBARP,
658 " sha=%02X:%02X:%02X:%02X:%02X:%02X",
659 arp_h->arp_data.arp_sha.addr_bytes[0],
660 arp_h->arp_data.arp_sha.addr_bytes[1],
661 arp_h->arp_data.arp_sha.addr_bytes[2],
662 arp_h->arp_data.arp_sha.addr_bytes[3],
663 arp_h->arp_data.arp_sha.addr_bytes[4],
664 arp_h->arp_data.arp_sha.addr_bytes[5]);
665 RTE_LOG(INFO, LIBARP, " sip=%d.%d.%d.%d\n",
666 (CHECK_ENDIAN_32(arp_h->arp_data.arp_sip) >> 24) & 0xFF,
667 (CHECK_ENDIAN_32(arp_h->arp_data.arp_sip) >> 16) & 0xFF,
668 (CHECK_ENDIAN_32(arp_h->arp_data.arp_sip) >> 8) & 0xFF,
669 CHECK_ENDIAN_32(arp_h->arp_data.arp_sip) & 0xFF);
670 RTE_LOG(INFO, LIBARP,
671 " tha=%02X:%02X:%02X:%02X:%02X:%02X",
672 arp_h->arp_data.arp_tha.addr_bytes[0],
673 arp_h->arp_data.arp_tha.addr_bytes[1],
674 arp_h->arp_data.arp_tha.addr_bytes[2],
675 arp_h->arp_data.arp_tha.addr_bytes[3],
676 arp_h->arp_data.arp_tha.addr_bytes[4],
677 arp_h->arp_data.arp_tha.addr_bytes[5]);
678 RTE_LOG(INFO, LIBARP, " tip=%d.%d.%d.%d\n",
679 (CHECK_ENDIAN_32(arp_h->arp_data.arp_tip) >> 24) & 0xFF,
680 (CHECK_ENDIAN_32(arp_h->arp_data.arp_tip) >> 16) & 0xFF,
681 (CHECK_ENDIAN_32(arp_h->arp_data.arp_tip) >> 8) & 0xFF,
682 CHECK_ENDIAN_32(arp_h->arp_data.arp_tip) & 0xFF);
686 static void print_eth(struct ether_hdr *eth_h)
688 RTE_LOG(INFO, LIBARP, " ETH: src=%02X:%02X:%02X:%02X:%02X:%02X",
689 eth_h->s_addr.addr_bytes[0],
690 eth_h->s_addr.addr_bytes[1],
691 eth_h->s_addr.addr_bytes[2],
692 eth_h->s_addr.addr_bytes[3],
693 eth_h->s_addr.addr_bytes[4], eth_h->s_addr.addr_bytes[5]);
694 RTE_LOG(INFO, LIBARP, " dst=%02X:%02X:%02X:%02X:%02X:%02X\n",
695 eth_h->d_addr.addr_bytes[0],
696 eth_h->d_addr.addr_bytes[1],
697 eth_h->d_addr.addr_bytes[2],
698 eth_h->d_addr.addr_bytes[3],
699 eth_h->d_addr.addr_bytes[4], eth_h->d_addr.addr_bytes[5]);
704 print_mbuf(const char *rx_tx, uint8_t portid, struct rte_mbuf *mbuf,
707 struct ether_hdr *eth_h = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
708 struct arp_hdr *arp_h =
709 (struct arp_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
710 struct ipv4_hdr *ipv4_h =
711 (struct ipv4_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
713 RTE_LOG(INFO, LIBARP, "%s(%d): on port %d pkt-len=%u nb-segs=%u\n",
714 rx_tx, line, portid, mbuf->pkt_len, mbuf->nb_segs);
716 switch (rte_cpu_to_be_16(eth_h->ether_type)) {
717 case ETHER_TYPE_IPv4:
718 print_ipv4_h(ipv4_h);
721 print_arp_packet(arp_h);
724 RTE_LOG(INFO, LIBARP, " unknown packet type\n");
730 struct arp_entry_data *retrieve_arp_entry(struct arp_key_ipv4 arp_key)
732 struct arp_entry_data *ret_arp_data = NULL;
737 int ret = rte_hash_lookup_data(arp_hash_handle, &arp_key,
738 (void **)&ret_arp_data);
740 // RTE_LOG(INFO, LIBARP,"arp-hash lookup failed ret %d, EINVAL %d, ENOENT %d\n", ret, EINVAL, ENOENT);
743 if (ret_arp_data->mode == DYNAMIC_ARP) {
744 struct arp_timer_key callback_key;
745 callback_key.port_id = ret_arp_data->port;
746 callback_key.ip = ret_arp_data->ip;
747 /*lcore need to check which parameter need to be put */
748 if (rte_timer_reset(ret_arp_data->timer,
749 (arp_timeout * rte_get_tsc_hz()),
754 RTE_LOG(INFO, LIBARP,
755 "Err : Timer already running\n");
764 struct nd_entry_data *retrieve_nd_entry(struct nd_key_ipv6 nd_key)
766 struct nd_entry_data *ret_nd_data = NULL;
772 /*Find a nd IPv6 key-data pair in the hash table for ND IPv6 */
773 int ret = rte_hash_lookup_data(nd_hash_handle, &nd_key,
774 (void **)&ret_nd_data);
776 /* RTE_LOG(INFO, LIBARP,"nd-hash: no lookup Entry Found - ret %d, EINVAL %d, ENOENT %d\n",
777 ret, EINVAL, ENOENT);*/
779 if (ret_nd_data->mode == DYNAMIC_ND) {
780 struct nd_timer_key callback_key;
781 callback_key.port_id = ret_nd_data->port;
783 for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
784 callback_key.ipv6[i] = ret_nd_data->ipv6[i];
790 (arp_timeout * rte_get_tsc_hz()), SINGLE,
791 timer_lcore, nd_timer_callback, &callback_key) < 0)
793 RTE_LOG(INFO, LIBARP,
794 "Err : Timer already running\n");
802 void print_arp_table(void)
804 const void *next_key;
809 ("------------------------ ARP CACHE -----------------------------------------\n");
811 ("----------------------------------------------------------------------------\n");
812 printf("\tport hw addr status ip addr\n");
814 ("----------------------------------------------------------------------------\n");
816 while (rte_hash_iterate(arp_hash_handle, &next_key, &next_data, &iter)
819 struct arp_entry_data *tmp_arp_data =
820 (struct arp_entry_data *)next_data;
821 struct arp_key_ipv4 tmp_arp_key;
822 memcpy(&tmp_arp_key, next_key, sizeof(struct arp_key_ipv4));
824 ("\t%4d %02X:%02X:%02X:%02X:%02X:%02X %10s %d.%d.%d.%d\n",
825 tmp_arp_data->port, tmp_arp_data->eth_addr.addr_bytes[0],
826 tmp_arp_data->eth_addr.addr_bytes[1],
827 tmp_arp_data->eth_addr.addr_bytes[2],
828 tmp_arp_data->eth_addr.addr_bytes[3],
829 tmp_arp_data->eth_addr.addr_bytes[4],
830 tmp_arp_data->eth_addr.addr_bytes[5],
831 tmp_arp_data->status ==
832 COMPLETE ? "COMPLETE" : "INCOMPLETE",
833 (tmp_arp_data->ip >> 24),
834 ((tmp_arp_data->ip & 0x00ff0000) >> 16),
835 ((tmp_arp_data->ip & 0x0000ff00) >> 8),
836 ((tmp_arp_data->ip & 0x000000ff)));
840 printf("\nARP routing table has %d entries\n", arp_route_tbl_index);
841 printf("\nIP_Address Mask Port NH_IP_Address\n");
842 for (i = 0; i < arp_route_tbl_index; i++) {
843 printf("0x%x 0x%x %d 0x%x\n",
844 lib_arp_route_table[i].ip,
845 lib_arp_route_table[i].mask,
846 lib_arp_route_table[i].port, lib_arp_route_table[i].nh);
850 ("\nARP Stats: Total Queries %u, ok_NH %u, no_NH %u, ok_Entry %u, no_Entry %u, PopulateCall %u, Del %u, Dup %u\n",
851 lib_arp_get_mac_req, lib_arp_nh_found, lib_arp_no_nh_found,
852 lib_arp_arp_entry_found, lib_arp_no_arp_entry_found,
853 lib_arp_populate_called, lib_arp_delete_called,
854 lib_arp_duplicate_found);
856 printf("ARP table key len is %lu\n", sizeof(struct arp_key_ipv4));
860 void print_nd_table(void)
862 const void *next_key;
865 uint8_t ii = 0, j = 0, k = 0;
867 ("------------------------------------------------------------------------------------------------------\n");
868 printf("\tport hw addr status ip addr\n");
871 ("------------------------------------------------------------------------------------------------------\n");
872 while (rte_hash_iterate(nd_hash_handle, &next_key, &next_data, &iter) >=
875 struct nd_entry_data *tmp_nd_data =
876 (struct nd_entry_data *)next_data;
877 struct nd_key_ipv6 tmp_nd_key;
878 memcpy(&tmp_nd_key, next_key, sizeof(struct nd_key_ipv6));
879 printf("\t%4d %02X:%02X:%02X:%02X:%02X:%02X %10s\n",
881 tmp_nd_data->eth_addr.addr_bytes[0],
882 tmp_nd_data->eth_addr.addr_bytes[1],
883 tmp_nd_data->eth_addr.addr_bytes[2],
884 tmp_nd_data->eth_addr.addr_bytes[3],
885 tmp_nd_data->eth_addr.addr_bytes[4],
886 tmp_nd_data->eth_addr.addr_bytes[5],
887 tmp_nd_data->status ==
888 COMPLETE ? "COMPLETE" : "INCOMPLETE");
889 printf("\t\t\t\t\t\t");
890 for (ii = 0; ii < ND_IPV6_ADDR_SIZE; ii += 2) {
891 printf("%02X%02X ", tmp_nd_data->ipv6[ii],
892 tmp_nd_data->ipv6[ii + 1]);
898 printf("\n\nND IPV6 routing table has %d entries\n",
901 ("\nIP_Address Depth Port NH_IP_Address\n");
902 for (i = 0; i < nd_route_tbl_index; i++) {
905 for (j = 0; j < ND_IPV6_ADDR_SIZE; j += 2) {
906 RTE_LOG(INFO, LIBARP, "%02X%02X ",
907 lib_nd_route_table[i].ipv6[j],
908 lib_nd_route_table[i].ipv6[j + 1]);
912 ("\n\t\t\t %d %d \n",
913 lib_nd_route_table[i].depth, lib_nd_route_table[i].port);
914 printf("\t\t\t\t\t\t\t\t\t");
915 for (k = 0; k < ND_IPV6_ADDR_SIZE; k += 2) {
916 printf("%02X%02X ", lib_nd_route_table[i].nhipv6[k],
917 lib_nd_route_table[i].ipv6[k + 1]);
921 ("\nND IPV6 Stats: \nTotal Queries %u, ok_NH %u, no_NH %u, ok_Entry %u, no_Entry %u, PopulateCall %u, Del %u, Dup %u\n",
922 lib_nd_get_mac_req, lib_nd_nh_found, lib_nd_no_nh_found,
923 lib_nd_nd_entry_found, lib_nd_no_arp_entry_found,
924 lib_nd_populate_called, lib_nd_delete_called,
925 lib_nd_duplicate_found);
926 printf("ND table key len is %lu\n\n", sizeof(struct nd_key_ipv6));
929 void remove_arp_entry(uint32_t ipaddr, uint8_t portid, void *arg)
932 struct arp_key_ipv4 arp_key;
933 arp_key.port_id = portid;
939 lib_arp_delete_called++;
941 struct arp_entry_data *ret_arp_data = NULL;
943 int ret = rte_hash_lookup_data(arp_hash_handle, &arp_key,
944 (void **)&ret_arp_data);
946 // RTE_LOG(INFO, LIBARP,"arp-hash lookup failed ret %d, EINVAL %d, ENOENT %d\n", ret, EINVAL, ENOENT);
949 if (ret_arp_data->mode == DYNAMIC_ARP) {
950 if (ret_arp_data->retry_count == 3) {
951 rte_timer_stop(ret_arp_data->timer);
952 rte_free(ret_arp_data->timer_key);
954 RTE_LOG(INFO, LIBARP,
955 "ARP Entry Deleted for IP :%d.%d.%d.%d , port %d\n",
957 ((arp_key.ip & 0x00ff0000) >>
959 ((arp_key.ip & 0x0000ff00) >>
961 ((arp_key.ip & 0x000000ff)),
964 rte_hash_del_key(arp_hash_handle, &arp_key);
967 ret_arp_data->retry_count++;
969 RTE_LOG(INFO, LIBARP,
970 "RETRY ARP..retry count : %u\n",
971 ret_arp_data->retry_count);
974 RTE_LOG(INFO, LIBARP,
975 "TIMER STARTED FOR %u seconds\n",
977 if (ifm_chk_port_ipv4_enabled
978 (ret_arp_data->port)) {
979 request_arp(ret_arp_data->port,
983 RTE_LOG(INFO, LIBARP,
984 "%s: IP is not enabled on port %u, not sending GARP\n\r",
988 if (rte_timer_reset(ret_arp_data->timer,
990 rte_get_tsc_hz()), SINGLE,
995 RTE_LOG(INFO, LIBARP,
996 "Err : Timer already running\n");
1000 rte_hash_del_key(arp_hash_handle, &arp_key);
1006 void remove_nd_entry_ipv6(uint8_t ipv6addr[], uint8_t portid)
1009 struct nd_entry_data *ret_nd_data = NULL;
1010 struct nd_key_ipv6 nd_key;
1011 nd_key.port_id = portid;
1013 for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
1014 nd_key.ipv6[i] = ipv6addr[i];
1021 lib_nd_delete_called++;
1024 RTE_LOG(INFO, LIBARP,
1025 "Deletes rte hash table nd entry for port %d ipv6=",
1027 for (i = 0; i < ND_IPV6_ADDR_SIZE; i += 2) {
1028 RTE_LOG(INFO, LIBARP, "%02X%02X ", nd_key.ipv6[i],
1029 nd_key.ipv6[i + 1]);
1032 struct nd_timer_key callback_key;
1033 callback_key.port_id = portid;
1035 for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
1036 callback_key.ipv6[i] = ipv6addr[i];
1039 int ret = rte_hash_lookup_data(arp_hash_handle, &callback_key,
1040 (void **)&ret_nd_data);
1042 // RTE_LOG(INFO, LIBARP,"arp-hash lookup failed ret %d, EINVAL %d, ENOENT %d\n", ret, EINVAL, ENOENT);
1044 if (ret_nd_data->mode == DYNAMIC_ND) {
1045 rte_timer_stop(ret_nd_data->timer);
1046 rte_free(ret_nd_data->timer);
1049 rte_hash_del_key(nd_hash_handle, &nd_key);
1053 populate_arp_entry(const struct ether_addr *hw_addr, uint32_t ipaddr,
1054 uint8_t portid, uint8_t mode)
1056 struct arp_key_ipv4 arp_key;
1057 arp_key.port_id = portid;
1058 arp_key.ip = ipaddr;
1059 arp_key.filler1 = 0;
1060 arp_key.filler2 = 0;
1061 arp_key.filler3 = 0;
1063 lib_arp_populate_called++;
1066 RTE_LOG(INFO, LIBARP, "populate_arp_entry ip %x, port %d\n",
1067 arp_key.ip, arp_key.port_id);
1069 struct arp_entry_data *new_arp_data = retrieve_arp_entry(arp_key);
1070 if (new_arp_data && ((new_arp_data->mode == STATIC_ARP
1071 && mode == DYNAMIC_ARP) || (new_arp_data->mode == DYNAMIC_ARP
1072 && mode == STATIC_ARP))) {
1074 RTE_LOG(INFO, LIBARP,"populate_arp_entry: ARP entry already exists(%d %d)\n",
1075 new_arp_data->mode, mode);
1080 if (mode == DYNAMIC_ARP) {
1082 && is_same_ether_addr(&new_arp_data->eth_addr, hw_addr)) {
1083 if (ARPICMP_DEBUG) {
1084 RTE_LOG(INFO, LIBARP,
1085 "arp_entry exists ip :%d.%d.%d.%d , port %d\n",
1087 ((arp_key.ip & 0x00ff0000) >> 16),
1088 ((arp_key.ip & 0x0000ff00) >> 8),
1089 ((arp_key.ip & 0x000000ff)),
1092 lib_arp_duplicate_found++;
1093 new_arp_data->retry_count = 0; // Reset
1094 if (rte_timer_reset(new_arp_data->timer,
1095 (arp_timeout * rte_get_tsc_hz()),
1096 SINGLE, timer_lcore,
1098 new_arp_data->timer_key) < 0)
1100 RTE_LOG(INFO, LIBARP,
1101 "Err : Timer already running\n");
1106 RTE_CACHE_LINE_ROUNDUP(sizeof(struct arp_entry_data));
1107 new_arp_data = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
1108 new_arp_data->eth_addr = *hw_addr;
1109 new_arp_data->status = COMPLETE;
1110 new_arp_data->port = portid;
1111 new_arp_data->ip = ipaddr;
1112 new_arp_data->mode = mode;
1114 (timer_mempool_arp, (void **)&(new_arp_data->timer)) < 0) {
1115 RTE_LOG(INFO, LIBARP,
1116 "TIMER - Error in getting timer alloc buffer\n");
1120 rte_hash_add_key_data(arp_hash_handle, &arp_key, new_arp_data);
1121 if (ARPICMP_DEBUG) {
1122 RTE_LOG(INFO, LIBARP,
1123 "arp_entry exists ip :%d.%d.%d.%d , port %d\n",
1125 ((arp_key.ip & 0x00ff0000) >> 16),
1126 ((arp_key.ip & 0x0000ff00) >> 8),
1127 ((arp_key.ip & 0x000000ff)), arp_key.port_id);
1129 // Call l3fwd module for resolving 2_adj structure.
1130 resolve_l2_adj(ipaddr, portid, hw_addr);
1132 rte_timer_init(new_arp_data->timer);
1133 struct arp_timer_key *callback_key =
1134 (struct arp_timer_key *)rte_malloc(NULL,
1137 RTE_CACHE_LINE_SIZE);
1138 callback_key->port_id = portid;
1139 callback_key->ip = ipaddr;
1142 RTE_LOG(INFO, LIBARP, "TIMER STARTED FOR %u seconds\n",
1145 (new_arp_data->timer, (arp_timeout * rte_get_tsc_hz()),
1146 SINGLE, timer_lcore, arp_timer_callback, callback_key) < 0)
1148 RTE_LOG(INFO, LIBARP,
1149 "Err : Timer already running\n");
1151 new_arp_data->timer_key = callback_key;
1154 && is_same_ether_addr(&new_arp_data->eth_addr, hw_addr)) {
1155 if (ARPICMP_DEBUG) {
1156 RTE_LOG(INFO, LIBARP,
1157 "arp_entry exists ip :%d.%d.%d.%d , port %d\n",
1159 ((arp_key.ip & 0x00ff0000) >> 16),
1160 ((arp_key.ip & 0x0000ff00) >> 8),
1161 ((arp_key.ip & 0x000000ff)),
1164 lib_arp_duplicate_found++;
1167 RTE_CACHE_LINE_ROUNDUP(sizeof
1168 (struct arp_entry_data));
1170 rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
1171 new_arp_data->eth_addr = *hw_addr;
1172 new_arp_data->status = COMPLETE;
1173 new_arp_data->port = portid;
1174 new_arp_data->ip = ipaddr;
1175 new_arp_data->mode = mode;
1177 rte_hash_add_key_data(arp_hash_handle, &arp_key,
1179 if (ARPICMP_DEBUG) {
1180 RTE_LOG(INFO, LIBARP,
1181 "arp_entry exists ip :%d.%d.%d.%d , port %d\n",
1183 ((arp_key.ip & 0x00ff0000) >> 16),
1184 ((arp_key.ip & 0x0000ff00) >> 8),
1185 ((arp_key.ip & 0x000000ff)),
1188 // Call l3fwd module for resolving 2_adj structure.
1189 resolve_l2_adj(ipaddr, portid, hw_addr);
1192 if (ARPICMP_DEBUG) {
1193 /* print entire hash table */
1194 RTE_LOG(INFO, LIBARP,
1195 "\tARP: table update - hwaddr=%02x:%02x:%02x:%02x:%02x:%02x ip=%d.%d.%d.%d on port=%d\n",
1196 new_arp_data->eth_addr.addr_bytes[0],
1197 new_arp_data->eth_addr.addr_bytes[1],
1198 new_arp_data->eth_addr.addr_bytes[2],
1199 new_arp_data->eth_addr.addr_bytes[3],
1200 new_arp_data->eth_addr.addr_bytes[4],
1201 new_arp_data->eth_addr.addr_bytes[5],
1202 (arp_key.ip >> 24), ((arp_key.ip & 0x00ff0000) >> 16),
1203 ((arp_key.ip & 0x0000ff00) >> 8),
1204 ((arp_key.ip & 0x000000ff)), portid);
1212 * Install key - data pair in Hash table - From Pipeline Configuration
1216 void populate_nd_entry(const struct ether_addr *hw_addr, uint8_t ipv6[],
1217 uint8_t portid, uint8_t mode)
1220 /* need to lock here if multi-threaded */
1221 /* rte_hash_add_key_data is not thread safe */
1223 struct nd_key_ipv6 nd_key;
1224 nd_key.port_id = portid;
1226 for (i = 0; i < ND_IPV6_ADDR_SIZE; i++)
1227 nd_key.ipv6[i] = ipv6[i];
1229 // RTE_LOG(INFO, LIBARP,"\n");
1234 lib_nd_populate_called++;
1236 /* Validate if key-value pair already exists in the hash table for ND IPv6 */
1237 struct nd_entry_data *new_nd_data = retrieve_nd_entry(nd_key);
1239 if (mode == DYNAMIC_ND) {
1241 && is_same_ether_addr(&new_nd_data->eth_addr, hw_addr)) {
1244 RTE_LOG(INFO, LIBARP,
1245 "nd_entry exists port %d ipv6 = ",
1247 for (i = 0; i < ND_IPV6_ADDR_SIZE; i += 2) {
1249 RTE_LOG(INFO, LIBARP, "%02X%02X ",
1251 nd_key.ipv6[i + 1]);
1255 lib_nd_duplicate_found++;
1256 RTE_LOG(INFO, LIBARP, "nd_entry exists\n");
1260 RTE_CACHE_LINE_ROUNDUP(sizeof(struct nd_entry_data));
1261 new_nd_data = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
1263 //new_nd_data = (struct nd_entry_data *)rte_malloc(NULL, sizeof(struct nd_entry_data *),RTE_CACHE_LINE_SIZE);
1264 new_nd_data->eth_addr = *hw_addr;
1265 new_nd_data->status = COMPLETE;
1266 new_nd_data->port = portid;
1267 new_nd_data->mode = mode;
1269 (timer_mempool_arp, (void **)&(new_nd_data->timer)) < 0) {
1270 RTE_LOG(INFO, LIBARP,
1271 "TIMER - Error in getting timer alloc buffer\n");
1276 RTE_LOG(INFO, LIBARP, "populate_nd_entry ipv6=");
1278 for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
1279 new_nd_data->ipv6[i] = ipv6[i];
1283 for (i = 0; i < ND_IPV6_ADDR_SIZE; i += 2) {
1285 RTE_LOG(INFO, LIBARP, "%02X%02X ",
1286 new_nd_data->ipv6[i],
1287 new_nd_data->ipv6[i + 1]);
1291 /*Add a key-data pair at hash table for ND IPv6 static routing */
1292 rte_hash_add_key_data(nd_hash_handle, &nd_key, new_nd_data);
1293 /* need to check the return value of the hash add */
1295 /* after the hash is created then time is started */
1296 rte_timer_init(new_nd_data->timer);
1297 struct nd_timer_key *callback_key =
1298 (struct nd_timer_key *)rte_malloc(NULL,
1299 sizeof(struct nd_timer_key
1301 RTE_CACHE_LINE_SIZE);
1302 callback_key->port_id = portid;
1304 for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
1305 callback_key->ipv6[i] = ipv6[i];
1308 (new_nd_data->timer, (arp_timeout * rte_get_tsc_hz()),
1309 SINGLE, timer_lcore, nd_timer_callback, callback_key) < 0)
1310 RTE_LOG(INFO, LIBARP, "Err : Timer already running\n");
1313 && is_same_ether_addr(&new_nd_data->eth_addr, hw_addr)) {
1315 RTE_LOG(INFO, LIBARP,
1316 "nd_entry exists port %d ipv6 = ",
1318 for (i = 0; i < ND_IPV6_ADDR_SIZE; i += 2) {
1320 RTE_LOG(INFO, LIBARP, "%02X%02X ",
1322 nd_key.ipv6[i + 1]);
1326 lib_nd_duplicate_found++;
1329 RTE_CACHE_LINE_ROUNDUP(sizeof
1330 (struct nd_entry_data));
1332 rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
1334 //new_nd_data = (struct nd_entry_data *)rte_malloc(NULL, sizeof(struct nd_entry_data *),RTE_CACHE_LINE_SIZE);
1335 new_nd_data->eth_addr = *hw_addr;
1336 new_nd_data->status = COMPLETE;
1337 new_nd_data->port = portid;
1338 new_nd_data->mode = mode;
1339 for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
1340 new_nd_data->ipv6[i] = ipv6[i];
1343 /*Add a key-data pair at hash table for ND IPv6 static routing */
1344 rte_hash_add_key_data(nd_hash_handle, &nd_key,
1346 /* need to check the return value of the hash add */
1351 ("\n....Added a key-data pair at rte hash table for ND IPv6 static routing\n");
1354 /* print entire hash table */
1356 ("\tND: table update - hwaddr=%02x:%02x:%02x:%02x:%02x:%02x on port=%d\n",
1357 new_nd_data->eth_addr.addr_bytes[0],
1358 new_nd_data->eth_addr.addr_bytes[1],
1359 new_nd_data->eth_addr.addr_bytes[2],
1360 new_nd_data->eth_addr.addr_bytes[3],
1361 new_nd_data->eth_addr.addr_bytes[4],
1362 new_nd_data->eth_addr.addr_bytes[5], portid);
1363 RTE_LOG(INFO, LIBARP, "\tipv6=");
1364 for (i = 0; i < ND_IPV6_ADDR_SIZE; i += 2) {
1365 new_nd_data->ipv6[i] = ipv6[i];
1366 RTE_LOG(INFO, LIBARP, "%02X%02X ", new_nd_data->ipv6[i],
1367 new_nd_data->ipv6[i + 1]);
1370 RTE_LOG(INFO, LIBARP, "\n");
1376 void print_pkt1(struct rte_mbuf *pkt)
1378 uint8_t *rd = RTE_MBUF_METADATA_UINT8_PTR(pkt, 0);
1380 RTE_LOG(INFO, LIBARP, "\nPacket Contents...\n");
1381 for (i = 0; i < 20; i++) {
1382 for (j = 0; j < 20; j++)
1383 RTE_LOG(INFO, LIBARP, "%02x ", rd[(20 * i) + j]);
1384 RTE_LOG(INFO, LIBARP, "\n");
1388 struct ether_addr broadcast_ether_addr = {
1389 .addr_bytes[0] = 0xFF,
1390 .addr_bytes[1] = 0xFF,
1391 .addr_bytes[2] = 0xFF,
1392 .addr_bytes[3] = 0xFF,
1393 .addr_bytes[4] = 0xFF,
1394 .addr_bytes[5] = 0xFF,
1397 static const struct ether_addr null_ether_addr = {
1398 .addr_bytes[0] = 0x00,
1399 .addr_bytes[1] = 0x00,
1400 .addr_bytes[2] = 0x00,
1401 .addr_bytes[3] = 0x00,
1402 .addr_bytes[4] = 0x00,
1403 .addr_bytes[5] = 0x00,
1406 #define MAX_NUM_MAC_ADDRESS 16
1407 struct ether_addr link_hw_addr[MAX_NUM_MAC_ADDRESS] = {
1408 {.addr_bytes = {0x90, 0xe2, 0xba, 0x54, 0x67, 0xc8} },
1409 {.addr_bytes = {0x90, 0xe2, 0xba, 0x54, 0x67, 0xc9} },
1410 {.addr_bytes = {0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11} },
1411 {.addr_bytes = {0x12, 0x13, 0x14, 0x15, 0x16, 0x17} },
1412 {.addr_bytes = {0x22, 0x33, 0x44, 0x55, 0x66, 0x77} },
1413 {.addr_bytes = {0x12, 0x13, 0x14, 0x15, 0x16, 0x17} },
1414 {.addr_bytes = {0x22, 0x33, 0x44, 0x55, 0x66, 0x77} },
1415 {.addr_bytes = {0x90, 0xe2, 0xba, 0x54, 0x67, 0xc9} },
1416 {.addr_bytes = {0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11} },
1417 {.addr_bytes = {0x12, 0x13, 0x14, 0x15, 0x16, 0x17} },
1418 {.addr_bytes = {0x22, 0x33, 0x44, 0x55, 0x66, 0x77} },
1419 {.addr_bytes = {0x12, 0x13, 0x14, 0x15, 0x16, 0x17} },
1420 {.addr_bytes = {0x22, 0x33, 0x44, 0x55, 0x66, 0x77} },
1421 {.addr_bytes = {0x12, 0x13, 0x14, 0x15, 0x16, 0x17} },
1422 {.addr_bytes = {0x22, 0x33, 0x44, 0x55, 0x66, 0x77} },
1423 {.addr_bytes = {0x18, 0x19, 0x1a, 0x1b, 0xcd, 0xef} }
1426 struct ether_addr *get_link_hw_addr(uint8_t out_port)
1428 return &link_hw_addr[out_port];
1431 void request_arp(uint8_t port_id, uint32_t ip)
1434 struct ether_hdr *eth_h;
1435 struct arp_hdr *arp_h;
1437 l2_phy_interface_t *link;
1438 link = ifm_get_port(port_id);
1439 struct rte_mbuf *arp_pkt = lib_arp_pkt;
1441 if (arp_pkt == NULL) {
1443 RTE_LOG(INFO, LIBARP,
1444 "Error allocating arp_pkt rte_mbuf\n");
1448 eth_h = rte_pktmbuf_mtod(arp_pkt, struct ether_hdr *);
1450 ether_addr_copy(&broadcast_ether_addr, ð_h->d_addr);
1451 ether_addr_copy((struct ether_addr *)
1452 &link->macaddr[0], ð_h->s_addr);
1453 eth_h->ether_type = CHECK_ENDIAN_16(ETHER_TYPE_ARP);
1455 arp_h = (struct arp_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
1456 arp_h->arp_hrd = CHECK_ENDIAN_16(ARP_HRD_ETHER);
1457 arp_h->arp_pro = CHECK_ENDIAN_16(ETHER_TYPE_IPv4);
1458 arp_h->arp_hln = ETHER_ADDR_LEN;
1459 arp_h->arp_pln = sizeof(uint32_t);
1460 arp_h->arp_op = CHECK_ENDIAN_16(ARP_OP_REQUEST);
1462 ether_addr_copy((struct ether_addr *)
1463 &link->macaddr[0], &arp_h->arp_data.arp_sha);
1464 if (link && link->ipv4_list) {
1465 arp_h->arp_data.arp_sip =
1466 (((ipv4list_t *) (link->ipv4_list))->ipaddr);
1468 ether_addr_copy(&null_ether_addr, &arp_h->arp_data.arp_tha);
1469 arp_h->arp_data.arp_tip = rte_cpu_to_be_32(ip);
1471 RTE_LOG(INFO, LIBARP, "arp tip:%x arp sip :%x\n",
1472 arp_h->arp_data.arp_tip, arp_h->arp_data.arp_sip);
1473 // mmcd changed length from 60 to 42 - real length of arp request, no padding on ethernet needed - looks now like linux arp
1474 arp_pkt->pkt_len = 42;
1475 arp_pkt->data_len = 42;
1477 if (ARPICMP_DEBUG) {
1478 RTE_LOG(INFO, LIBARP, "Sending arp request\n");
1479 print_mbuf("TX", port_id, arp_pkt, __LINE__);
1482 link->transmit_single_pkt(link, arp_pkt);
1485 struct rte_mbuf *request_echo(uint32_t port_id, uint32_t ip)
1487 struct ether_hdr *eth_h;
1488 struct ipv4_hdr *ip_h;
1489 struct icmp_hdr *icmp_h;
1490 l2_phy_interface_t *port = ifm_get_port(port_id);
1492 struct rte_mbuf *icmp_pkt = lib_arp_pkt;
1493 if (icmp_pkt == NULL) {
1495 RTE_LOG(INFO, LIBARP,
1496 "Error allocating icmp_pkt rte_mbuf\n");
1500 eth_h = rte_pktmbuf_mtod(icmp_pkt, struct ether_hdr *);
1502 ip_h = (struct ipv4_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
1503 icmp_h = (struct icmp_hdr *)((char *)ip_h + sizeof(struct ipv4_hdr));
1505 ip_h->version_ihl = IP_VHL_DEF;
1506 ip_h->type_of_service = 0;
1507 ip_h->total_length =
1508 rte_cpu_to_be_16(sizeof(struct ipv4_hdr) + sizeof(struct icmp_hdr));
1509 ip_h->packet_id = 0xaabb;
1510 ip_h->fragment_offset = 0x0000;
1511 ip_h->time_to_live = 64;
1512 ip_h->next_proto_id = IPPROTO_ICMP;
1513 if (port && port->ipv4_list)
1515 rte_cpu_to_be_32(((ipv4list_t *) port->ipv4_list)->ipaddr);
1516 ip_h->dst_addr = rte_cpu_to_be_32(ip);
1518 ip_h->hdr_checksum = 0;
1519 ip_h->hdr_checksum = rte_ipv4_cksum(ip_h);
1521 icmp_h->icmp_type = IP_ICMP_ECHO_REQUEST;
1522 icmp_h->icmp_code = 0;
1523 icmp_h->icmp_ident = 0xdead;
1524 icmp_h->icmp_seq_nb = 0xbeef;
1526 icmp_h->icmp_cksum = ~rte_raw_cksum(icmp_h, sizeof(struct icmp_hdr));
1529 sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) +
1530 sizeof(struct icmp_hdr);
1531 icmp_pkt->data_len = icmp_pkt->pkt_len;
1533 print_mbuf("TX", 0, icmp_pkt, __LINE__);
1540 * Function to send ICMP dest unreachable msg
1543 struct rte_mbuf *send_icmp_dest_unreachable_msg(uint32_t src_ip,
1546 struct ether_hdr *eth_h;
1547 struct ipv4_hdr *ip_h;
1548 struct icmp_hdr *icmp_h;
1549 struct rte_mbuf *icmp_pkt = lib_arp_pkt;
1551 if (icmp_pkt == NULL) {
1553 RTE_LOG(INFO, LIBARP,
1554 "Error allocating icmp_pkt rte_mbuf\n");
1558 eth_h = rte_pktmbuf_mtod(icmp_pkt, struct ether_hdr *);
1559 ip_h = (struct ipv4_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
1560 icmp_h = (struct icmp_hdr *)((char *)ip_h + sizeof(struct ipv4_hdr));
1562 ip_h->version_ihl = IP_VHL_DEF;
1563 ip_h->type_of_service = 0;
1564 ip_h->total_length =
1565 rte_cpu_to_be_16(sizeof(struct ipv4_hdr) + sizeof(struct icmp_hdr));
1566 ip_h->packet_id = 0xaabb;
1567 ip_h->fragment_offset = 0x0000;
1568 ip_h->time_to_live = 64;
1569 ip_h->next_proto_id = 1;
1571 ip_h->dst_addr = rte_bswap32(dest_ip);
1572 ip_h->src_addr = rte_bswap32(src_ip);
1574 ip_h->hdr_checksum = 0;
1575 ip_h->hdr_checksum = rte_ipv4_cksum(ip_h);
1577 icmp_h->icmp_type = 3; /* Destination Unreachable */
1578 icmp_h->icmp_code = 13; /* Communication administratively prohibited */
1580 icmp_h->icmp_cksum = ~rte_raw_cksum(icmp_h, sizeof(struct icmp_hdr));
1582 icmp_pkt->pkt_len = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) +
1583 sizeof(struct icmp_hdr);
1584 icmp_pkt->data_len = icmp_pkt->pkt_len;
1590 process_arpicmp_pkt_parse(struct rte_mbuf **pkt, uint16_t pkt_num,
1591 uint64_t pkt_mask, l2_phy_interface_t *port)
1593 RTE_SET_USED(pkt_num);
1595 RTE_LOG(INFO, LIBARP,
1596 "============ARP ENTRY================\n");
1599 RTE_LOG(INFO, LIBARP,
1600 "============ARP PROCESS================\n");
1603 uint64_t pkts_for_process = pkt_mask;
1604 for (; pkts_for_process;) {
1605 /**< process only valid packets. */
1606 uint8_t pos = (uint8_t) __builtin_ctzll(pkts_for_process);
1607 uint64_t pkts_mask = 1LLU << pos; /** <bitmask representing only this packet. */
1608 pkts_for_process &= ~pkts_mask; /** <remove this packet from the mask. */
1609 process_arpicmp_pkt(pkt[pos], port);
1614 void process_arpicmp_pkt(struct rte_mbuf *pkt, l2_phy_interface_t *port)
1616 uint8_t in_port_id = pkt->port;
1617 struct ether_hdr *eth_h;
1618 struct arp_hdr *arp_h;
1619 struct ipv4_hdr *ip_h;
1620 struct icmp_hdr *icmp_h;
1627 eth_h = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1629 if (eth_h->ether_type == rte_cpu_to_be_16(ETHER_TYPE_ARP)) {
1631 RTE_LOG(INFO, LIBARP, "%s, portid %u. Line %d\n\r",
1632 __FUNCTION__, port->pmdid, __LINE__);
1634 (struct arp_hdr *)((char *)eth_h +
1635 sizeof(struct ether_hdr));
1636 if (CHECK_ENDIAN_16(arp_h->arp_hrd) != ARP_HRD_ETHER)
1637 RTE_LOG(INFO, LIBARP,
1638 "Invalid hardware format of hardware address - not processing ARP req\n");
1639 else if (CHECK_ENDIAN_16(arp_h->arp_pro) != ETHER_TYPE_IPv4)
1640 RTE_LOG(INFO, LIBARP,
1641 "Invalid protocol address format - not processing ARP req\n");
1642 else if (arp_h->arp_hln != 6)
1643 RTE_LOG(INFO, LIBARP,
1644 "Invalid hardware address length - not processing ARP req\n");
1645 else if (arp_h->arp_pln != 4)
1646 RTE_LOG(INFO, LIBARP,
1647 "Invalid protocol address length - not processing ARP req\n");
1649 if (port->ipv4_list == NULL) {
1650 RTE_LOG(INFO, LIBARP,
1651 "Ports IPV4 List is NULL.. Unable to Process\n");
1655 if (arp_h->arp_data.arp_tip !=
1656 ((ipv4list_t *) (port->ipv4_list))->ipaddr) {
1657 if (arp_h->arp_data.arp_tip == arp_h->arp_data.arp_sip) {
1659 (struct ether_addr *)&arp_h->arp_data.arp_sha,
1660 rte_cpu_to_be_32(arp_h->arp_data.arp_sip),
1665 RTE_LOG(INFO, LIBARP,"ARP requested IP address mismatches interface IP - discarding\n");
1668 /// revise conditionals to allow processing of requests with target ip = this ip and
1669 // processing of replies to destination ip = this ip
1670 else if (arp_h->arp_op ==
1671 rte_cpu_to_be_16(ARP_OP_REQUEST)) {
1672 if (ARPICMP_DEBUG) {
1673 RTE_LOG(INFO, LIBARP,
1674 "%s, portid %u. Line %d\n\r",
1675 __FUNCTION__, port->pmdid,
1678 RTE_LOG(INFO, LIBARP,
1679 "arp_op %d, ARP_OP_REQUEST %d\n",
1683 print_mbuf("RX", in_port_id, pkt,
1687 populate_arp_entry((struct ether_addr *)
1688 &arp_h->arp_data.arp_sha,
1690 (arp_h->arp_data.arp_sip),
1691 in_port_id, DYNAMIC_ARP);
1694 req_tip = arp_h->arp_data.arp_tip;
1695 ether_addr_copy(ð_h->s_addr, ð_h->d_addr);
1696 ether_addr_copy((struct ether_addr *)&port->macaddr[0], ð_h->s_addr); /**< set sender mac address*/
1697 arp_h->arp_op = rte_cpu_to_be_16(ARP_OP_REPLY);
1698 ether_addr_copy(ð_h->s_addr,
1699 &arp_h->arp_data.arp_sha);
1700 arp_h->arp_data.arp_tip =
1701 arp_h->arp_data.arp_sip;
1702 arp_h->arp_data.arp_sip = req_tip;
1703 ether_addr_copy(ð_h->d_addr,
1704 &arp_h->arp_data.arp_tha);
1707 print_mbuf("TX ARP REPLY PKT",
1708 port->pmdid, pkt, __LINE__);
1709 port->transmit_bulk_pkts(port, &pkt, 1);
1711 print_mbuf("TX", port->pmdid, pkt,
1715 } else if (arp_h->arp_op ==
1716 rte_cpu_to_be_16(ARP_OP_REPLY)) {
1717 if (ARPICMP_DEBUG) {
1718 RTE_LOG(INFO, LIBARP,
1719 "ARP_OP_REPLY received");
1720 print_mbuf("RX", port->pmdid, pkt,
1723 populate_arp_entry((struct ether_addr *)
1724 &arp_h->arp_data.arp_sha,
1727 in_port_id, DYNAMIC_ARP);
1732 RTE_LOG(INFO, LIBARP,
1733 "Invalid ARP opcode - not processing ARP req %x\n",
1738 rte_pktmbuf_free(pkt);
1741 (struct ipv4_hdr *)((char *)eth_h +
1742 sizeof(struct ether_hdr));
1744 (struct icmp_hdr *)((char *)ip_h + sizeof(struct ipv4_hdr));
1746 if (eth_h->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
1748 if (ip_h->next_proto_id != IPPROTO_ICMP) {
1749 if (ARPICMP_DEBUG) {
1750 RTE_LOG(INFO, LIBARP,
1751 "IP protocol ID is not set to ICMP - discarding\n");
1753 } else if ((ip_h->version_ihl & 0xf0) != IP_VERSION_4) {
1754 if (ARPICMP_DEBUG) {
1755 RTE_LOG(INFO, LIBARP,
1756 "IP version other than 4 - discarding\n");
1758 } else if ((ip_h->version_ihl & 0x0f) != IP_HDRLEN) {
1759 if (ARPICMP_DEBUG) {
1760 RTE_LOG(INFO, LIBARP,
1761 "Unknown IHL - discarding\n");
1764 if (icmp_h->icmp_type == IP_ICMP_ECHO_REQUEST
1765 && icmp_h->icmp_code == 0) {
1767 print_mbuf("RX", in_port_id,
1770 ip_addr = ip_h->src_addr;
1771 ether_addr_copy(ð_h->s_addr,
1773 ether_addr_copy((struct ether_addr *)
1777 RTE_LOG(INFO, LIBARP,
1778 "%s, portid %u. Line %d\n\r",
1780 port->pmdid, __LINE__);
1782 if (is_multicast_ipv4_addr
1787 rte_be_to_cpu_32(ip_addr);
1788 if ((ip_src & 0x00000003) == 1)
1799 rte_cpu_to_be_32(ip_src);
1800 ip_h->dst_addr = ip_addr;
1802 ip_h->hdr_checksum = 0;
1803 ip_h->hdr_checksum =
1804 ~rte_raw_cksum(ip_h,
1809 RTE_LOG(INFO, LIBARP,
1810 "%s, portid %u. Line %d\n\r",
1814 ip_h->src_addr = ip_h->dst_addr;
1815 ip_h->dst_addr = ip_addr;
1818 icmp_h->icmp_type = IP_ICMP_ECHO_REPLY;
1819 cksum = ~icmp_h->icmp_cksum & 0xffff;
1821 ~htons(IP_ICMP_ECHO_REQUEST << 8) &
1823 cksum += htons(IP_ICMP_ECHO_REPLY << 8);
1825 (cksum & 0xffff) + (cksum >> 16);
1827 (cksum & 0xffff) + (cksum >> 16);
1828 icmp_h->icmp_cksum = ~cksum;
1832 ("TX ICMP ECHO REPLY PKT",
1833 in_port_id, pkt, __LINE__);
1834 port->transmit_bulk_pkts(port, &pkt, 1);
1836 print_mbuf("TX", port->pmdid,
1840 } else if (icmp_h->icmp_type ==
1842 && icmp_h->icmp_code == 0) {
1844 print_mbuf("RX", in_port_id,
1847 struct arp_key_ipv4 arp_key;
1848 arp_key.port_id = in_port_id;
1850 rte_bswap32(ip_h->src_addr);
1851 arp_key.filler1 = 0;
1852 arp_key.filler2 = 0;
1853 arp_key.filler3 = 0;
1855 struct arp_entry_data *arp_entry =
1856 retrieve_arp_entry(arp_key);
1857 if (arp_entry == NULL) {
1859 RTE_LOG(INFO, LIBARP,
1860 "Received unsolicited ICMP echo reply from ip%x, port %d\n",
1865 arp_entry->status = COMPLETE;
1870 rte_pktmbuf_free(pkt);
1875 * inet_pton(af, src, dst)
1876 * convert from presentation format (which usually means ASCII printable)
1877 * to network format (which is usually some kind of binary format).
1879 * 1 if the address was valid for the specified address family
1880 * 0 if the address wasn't valid (`dst' is untouched in this case)
1881 * -1 if some other error occurred (`dst' is untouched in this case, too)
1885 static int my_inet_pton_ipv6(int af, const char *src, void *dst)
1889 return inet_pton_ipv4(src, dst);
1891 return inet_pton_ipv6(src, dst);
1893 errno = EAFNOSUPPORT;
1900 * inet_pton_ipv4(src, dst)
1901 * like inet_aton() but without all the hexadecimal and shorthand.
1903 * 1 if `src' is a valid dotted quad, else 0.
1905 * does not touch `dst' unless it's returning 1.
1909 static int inet_pton_ipv4(const char *src, unsigned char *dst)
1911 static const char digits[] = "0123456789";
1912 int saw_digit, octets, ch;
1913 unsigned char tmp[INADDRSZ], *tp;
1918 while ((ch = *src++) != '\0') {
1921 pch = strchr(digits, ch);
1923 unsigned int new = *tp * 10 + (pch - digits);
1932 *tp = (unsigned char)new;
1933 } else if (ch == '.' && saw_digit) {
1944 memcpy(dst, tmp, INADDRSZ);
1949 * inet_pton_ipv6(src, dst)
1950 * convert presentation level address to network order binary form.
1952 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
1954 * (1) does not touch `dst' unless it's returning 1.
1955 * (2) :: in a full address is silently ignored.
1957 * inspired by Mark Andrews.
1961 static int inet_pton_ipv6(const char *src, unsigned char *dst)
1963 static const char xdigits_l[] = "0123456789abcdef",
1964 xdigits_u[] = "0123456789ABCDEF";
1965 unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
1966 const char *xdigits = 0, *curtok = 0;
1967 int ch = 0, saw_xdigit = 0, count_xdigit = 0;
1968 unsigned int val = 0;
1969 unsigned int dbloct_count = 0;
1971 memset((tp = tmp), '\0', IN6ADDRSZ);
1972 endp = tp + IN6ADDRSZ;
1974 /* Leading :: requires some special handling. */
1979 saw_xdigit = count_xdigit = 0;
1982 while ((ch = *src++) != '\0') {
1985 pch = strchr((xdigits = xdigits_l), ch);
1987 pch = strchr((xdigits = xdigits_u), ch);
1989 if (count_xdigit >= 4)
1992 val |= (pch - xdigits);
2006 } else if (*src == '\0') {
2009 if (tp + sizeof(int16_t) > endp)
2011 *tp++ = (unsigned char)((val >> 8) & 0xff);
2012 *tp++ = (unsigned char)(val & 0xff);
2019 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
2020 inet_pton_ipv4(curtok, tp) > 0) {
2024 break; /* '\0' was seen by inet_pton4(). */
2029 if (tp + sizeof(int16_t) > endp)
2031 *tp++ = (unsigned char)((val >> 8) & 0xff);
2032 *tp++ = (unsigned char)(val & 0xff);
2035 if (colonp != NULL) {
2036 /* if we already have 8 double octets, having a colon means error */
2037 if (dbloct_count == 8)
2041 * Since some memmove()'s erroneously fail to handle
2042 * overlapping regions, we'll do the shift by hand.
2044 const int n = tp - colonp;
2047 for (i = 1; i <= n; i++) {
2048 endp[-i] = colonp[n - i];
2055 memcpy(dst, tmp, IN6ADDRSZ);
2059 static int arp_parse_args(struct pipeline_params *params)
2061 uint32_t arp_route_tbl_present = 0;
2062 uint32_t nd_route_tbl_present = 0;
2063 uint32_t ports_mac_list_present = 0;
2065 uint32_t n_vnf_threads_present = 0;
2067 uint32_t pktq_in_prv_present = 0;
2068 uint32_t prv_to_pub_map_present = 0;
2070 uint8_t n_prv_in_port = 0;
2072 for (i = 0; i < PIPELINE_MAX_PORT_IN; i++) {
2073 in_port_dir_a[i] = 0; //make all RX ports ingress initially
2074 prv_to_pub_map[i] = 0xff;
2075 pub_to_prv_map[i] = 0xff;
2078 RTE_SET_USED(ports_mac_list_present);
2079 RTE_SET_USED(nd_route_tbl_present);
2080 RTE_SET_USED(arp_route_tbl_present);
2081 for (numArg = 0; numArg < params->n_args; numArg++) {
2082 char *arg_name = params->args_name[numArg];
2083 char *arg_value = params->args_value[numArg];
2085 /* arp timer expiry */
2086 if (strcmp(arg_name, "arp_timer_expiry") == 0) {
2087 arp_timeout = atoi(arg_value);
2091 if (strcmp(arg_name, "pktq_in_prv") == 0) {
2092 if (pktq_in_prv_present) {
2094 ("Duplicate pktq_in_prv ... parse failed..\n\n");
2097 pktq_in_prv_present = 1;
2099 int rxport = 0, j = 0;
2100 char phy_port_num[5];
2101 char *token = strtok(arg_value, "RXQ");
2104 while ((j < 4) && (token[j] != '.')) {
2105 phy_port_num[j] = token[j];
2108 phy_port_num[j] = '\0';
2109 rxport = atoi(phy_port_num);
2110 prv_in_port_a[n_prv_in_port++] = rxport;
2114 ("token: %s, phy_port_str: %s, phy_port_num %d\n",
2115 token, phy_port_num, rxport);
2116 prv_in_port_a[n_prv_in_port++] = rxport;
2117 if(rxport < PIPELINE_MAX_PORT_IN)
2118 in_port_dir_a[rxport] = 1; // set rxport egress
2119 token = strtok(NULL, "RXQ");
2122 if (n_prv_in_port == 0) {
2124 ("VNF common parse error - no prv RX phy port\n");
2130 /* prv_to_pub_map */
2131 if (strcmp(arg_name, "prv_to_pub_map") == 0) {
2132 if (prv_to_pub_map_present) {
2134 ("Duplicated prv_to_pub_map ... parse failed ...\n");
2137 prv_to_pub_map_present = 1;
2139 int rxport = 0, txport = 0, j = 0, k = 0;
2140 char rx_phy_port_num[5];
2141 char tx_phy_port_num[5];
2142 char *token = strtok(arg_value, "(");
2145 while ((j < 4) && (token[j] != ',')) {
2146 rx_phy_port_num[j] = token[j];
2149 rx_phy_port_num[j] = '\0';
2150 rxport = atoi(rx_phy_port_num);
2156 while ((k < 4) && (token[j + k] != ')')) {
2157 tx_phy_port_num[k] = token[j + k];
2160 tx_phy_port_num[k] = '\0';
2161 txport = atoi(tx_phy_port_num);
2165 RTE_LOG(INFO, LIBARP, "token: %s,"
2166 "rx_phy_port_str: %s, phy_port_num %d,"
2167 "tx_phy_port_str: %s, tx_phy_port_num %d\n",
2168 token, rx_phy_port_num, rxport,
2169 tx_phy_port_num, txport);
2171 if ((rxport >= PIPELINE_MAX_PORT_IN) ||
2172 (txport >= PIPELINE_MAX_PORT_IN) ||
2173 (in_port_dir_a[rxport] != 1)) {
2175 ("CG-NAPT parse error - incorrect prv-pub translation. Rx %d, Tx %d, Rx Dir %d\n",
2177 in_port_dir_a[rxport]);
2181 prv_to_pub_map[rxport] = txport;
2182 pub_to_prv_map[txport] = rxport;
2183 token = strtok(NULL, "(");
2189 if (strcmp(arg_name, "n_vnf_threads") == 0) {
2190 if (n_vnf_threads_present)
2192 n_vnf_threads_present = 1;
2194 num_vnf_threads = atoi(arg_value);
2195 if (num_vnf_threads <= 0) {
2196 RTE_LOG(INFO, LIBARP,
2197 "n_vnf_threads is invalid\n");
2200 RTE_LOG(INFO, LIBARP, "n_vnf_threads: 0x%x\n",
2205 if (strcmp(arg_name, "lib_arp_debug") == 0) {
2206 ARPICMP_DEBUG = atoi(arg_value);
2211 /* ports_mac_list */
2212 if (strcmp(arg_name, "ports_mac_list") == 0) {
2213 ports_mac_list_present = 1;
2215 uint32_t i = 0, j = 0, k = 0, MAC_NUM_BYTES = 6;
2217 char byteStr[MAC_NUM_BYTES][3];
2218 uint32_t byte[MAC_NUM_BYTES];
2220 char *token = strtok(arg_value, " ");
2223 for (i = 0; i < MAC_NUM_BYTES; i++) {
2224 for (j = 0; j < 2; j++) {
2225 byteStr[i][j] = token[k++];
2227 byteStr[i][j] = '\0';
2231 for (i = 0; i < MAC_NUM_BYTES; i++) {
2232 byte[i] = strtoul(byteStr[i], NULL, 16);
2235 if (ARPICMP_DEBUG) {
2236 RTE_LOG(INFO, LIBARP, "token: %s",
2238 for (i = 0; i < MAC_NUM_BYTES; i++)
2239 RTE_LOG(INFO, LIBARP,
2242 RTE_LOG(INFO, LIBARP, "\n");
2244 //Populate the static arp_route_table
2245 for (i = 0; i < MAC_NUM_BYTES; i++)
2247 [link_hw_addr_array_idx].addr_bytes
2250 link_hw_addr_array_idx++;
2251 token = strtok(NULL, " ");
2258 if (strcmp(arg_name, "arp_route_tbl") == 0) {
2259 arp_route_tbl_present = 1;
2261 uint32_t dest_ip = 0, mask = 0, tx_port = 0, nh_ip =
2262 0, i = 0, j = 0, k = 0, l = 0;
2263 uint32_t arp_route_tbl_str_max_len = 10;
2264 char dest_ip_str[arp_route_tbl_str_max_len];
2265 char mask_str[arp_route_tbl_str_max_len];
2266 char tx_port_str[arp_route_tbl_str_max_len];
2267 char nh_ip_str[arp_route_tbl_str_max_len];
2268 char *token = strtok(arg_value, "(");
2271 while ((i < (arp_route_tbl_str_max_len - 1))
2272 && (token[i] != ',')) {
2273 dest_ip_str[i] = token[i];
2276 dest_ip_str[i] = '\0';
2277 dest_ip = strtoul(dest_ip_str, NULL, 16);
2281 while ((j < (arp_route_tbl_str_max_len - 1))
2282 && (token[i + j] != ',')) {
2283 mask_str[j] = token[i + j];
2287 mask = strtoul(mask_str, NULL, 16);
2291 while ((k < (arp_route_tbl_str_max_len - 1))
2292 && (token[i + j + k] != ',')) {
2293 tx_port_str[k] = token[i + j + k];
2296 tx_port_str[k] = '\0';
2297 tx_port = strtoul(tx_port_str, NULL, 16); //atoi(tx_port_str);
2301 while ((l < (arp_route_tbl_str_max_len - 1))
2302 && (token[i + j + k + l] != ')')) {
2303 nh_ip_str[l] = token[i + j + k + l];
2306 nh_ip_str[l] = '\0';
2307 nh_ip = strtoul(nh_ip_str, NULL, 16); //atoi(nh_ip_str);
2310 RTE_LOG(INFO, LIBARP, "token: %s, "
2311 "dest_ip_str: %s, dest_ip %u, "
2312 "mask_str: %s, mask %u, "
2313 "tx_port_str: %s, tx_port %u, "
2314 "nh_ip_str: %s, nh_ip %u\n",
2315 token, dest_ip_str, dest_ip,
2316 mask_str, mask, tx_port_str,
2317 tx_port, nh_ip_str, nh_ip);
2320 /* if (tx_port >= params->n_ports_out)
2322 RTE_LOG(INFO, LIBARP,"ARP-ICMP parse error - incorrect tx_port %d, max %d\n",
2323 tx_port, params->n_ports_out);
2327 //Populate the static arp_route_table
2328 lib_arp_route_table[arp_route_tbl_index].ip =
2330 lib_arp_route_table[arp_route_tbl_index].mask =
2332 lib_arp_route_table[arp_route_tbl_index].port =
2334 lib_arp_route_table[arp_route_tbl_index].nh =
2336 arp_route_tbl_index++;
2337 token = strtok(NULL, "(");
2344 if (strcmp(arg_name, "nd_route_tbl") == 0) {
2345 nd_route_tbl_present = 1;
2347 uint8_t dest_ipv6[16], depth = 0, tx_port =
2348 0, nh_ipv6[16], i = 0, j = 0, k = 0, l = 0;
2349 uint8_t nd_route_tbl_str_max_len = 128; //64;
2350 char dest_ipv6_str[nd_route_tbl_str_max_len];
2351 char depth_str[nd_route_tbl_str_max_len];
2352 char tx_port_str[nd_route_tbl_str_max_len];
2353 char nh_ipv6_str[nd_route_tbl_str_max_len];
2354 char *token = strtok(arg_value, "(");
2357 while ((i < (nd_route_tbl_str_max_len - 1))
2358 && (token[i] != ',')) {
2359 dest_ipv6_str[i] = token[i];
2362 dest_ipv6_str[i] = '\0';
2363 my_inet_pton_ipv6(AF_INET6, dest_ipv6_str,
2368 while ((j < (nd_route_tbl_str_max_len - 1))
2369 && (token[i + j] != ',')) {
2370 depth_str[j] = token[i + j];
2373 depth_str[j] = '\0';
2374 //converting string char to integer
2376 for (s = 0; depth_str[s] != '\0'; ++s)
2377 depth = depth * 10 + depth_str[s] - '0';
2381 while ((k < (nd_route_tbl_str_max_len - 1))
2382 && (token[i + j + k] != ',')) {
2383 tx_port_str[k] = token[i + j + k];
2386 tx_port_str[k] = '\0';
2387 tx_port = strtoul(tx_port_str, NULL, 16); //atoi(tx_port_str);
2391 while ((l < (nd_route_tbl_str_max_len - 1))
2392 && (token[i + j + k + l] != ')')) {
2393 nh_ipv6_str[l] = token[i + j + k + l];
2396 nh_ipv6_str[l] = '\0';
2397 my_inet_pton_ipv6(AF_INET6, nh_ipv6_str,
2400 //Populate the static arp_route_table
2401 for (i = 0; i < 16; i++) {
2403 [nd_route_tbl_index].ipv6[i] =
2406 [nd_route_tbl_index].nhipv6[i] =
2409 lib_nd_route_table[nd_route_tbl_index].depth =
2411 lib_nd_route_table[nd_route_tbl_index].port =
2414 nd_route_tbl_index++;
2415 token = strtok(NULL, "(");
2423 /* Check that mandatory arguments are present */
2425 if ((arp_route_tbl_present == 0) || (ports_mac_list_present == 0)) {
2426 RTE_LOG(INFO, LIBARP,"VNF common not all mandatory arguments are present\n");
2427 RTE_LOG(INFO, LIBARP,"%d, %d \n",
2428 arp_route_tbl_present, ports_mac_list_present);
2436 void lib_arp_init(struct pipeline_params *params,
2437 __rte_unused struct app_params *app)
2440 RTE_LOG(INFO, LIBARP, "ARP initialization ...\n");
2442 /* Parse arguments */
2443 if (arp_parse_args(params)) {
2444 RTE_LOG(INFO, LIBARP, "arp_parse_args failed ...\n");
2448 /* create the arp_icmp mbuf rx pool */
2449 lib_arp_pktmbuf_tx_pool =
2450 rte_pktmbuf_pool_create("lib_arp_mbuf_tx_pool", NB_ARPICMP_MBUF, 32,
2451 0, RTE_MBUF_DEFAULT_BUF_SIZE,
2454 if (lib_arp_pktmbuf_tx_pool == NULL) {
2455 RTE_LOG(INFO, LIBARP, "ARP mbuf pool create failed.\n");
2459 lib_arp_pkt = rte_pktmbuf_alloc(lib_arp_pktmbuf_tx_pool);
2460 if (lib_arp_pkt == NULL) {
2461 RTE_LOG(INFO, LIBARP, "ARP lib_arp_pkt alloc failed.\n");
2465 arp_hash_params.socket_id = rte_socket_id();
2466 arp_hash_params.entries = MAX_NUM_ARP_ENTRIES;
2467 arp_hash_params.key_len = sizeof(struct arp_key_ipv4);
2468 arp_hash_handle = rte_hash_create(&arp_hash_params);
2470 if (arp_hash_handle == NULL) {
2471 RTE_LOG(INFO, LIBARP,
2472 "ARP rte_hash_create failed. socket %d ... \n",
2473 arp_hash_params.socket_id);
2475 RTE_LOG(INFO, LIBARP, "arp_hash_handle %p\n\n",
2476 (void *)arp_hash_handle);
2479 /* Create port alloc buffer */
2481 timer_mempool_arp = rte_mempool_create("timer_mempool_arp",
2482 timer_objs_mempool_count,
2483 sizeof(struct rte_timer),
2486 NULL, NULL, rte_socket_id(), 0);
2487 if (timer_mempool_arp == NULL) {
2488 rte_panic("timer_mempool create error\n");
2490 rte_timer_subsystem_init();
2491 list_add_type(ETHER_TYPE_ARP, process_arpicmp_pkt_parse);
2494 nd_hash_params.socket_id = rte_socket_id();
2495 nd_hash_params.entries = MAX_NUM_ND_ENTRIES;
2496 nd_hash_params.key_len = sizeof(struct nd_key_ipv6);
2497 nd_hash_handle = rte_hash_create(&nd_hash_params);
2498 if (nd_hash_handle == NULL) {
2499 RTE_LOG(INFO, LIBARP,
2500 "ND rte_hash_create failed. socket %d ... \n",
2501 nd_hash_params.socket_id);
2503 RTE_LOG(INFO, LIBARP, "nd_hash_handle %p\n\n",
2504 (void *)nd_hash_handle);
2510 void arp_timer_callback(struct rte_timer *timer, void *arg)
2512 struct arp_timer_key *remove_key = (struct arp_timer_key *)arg;
2514 RTE_LOG(INFO, LIBARP, "ARP TIMER callback : expire :%d\n",
2515 (int)timer->expire);
2517 RTE_LOG(INFO, LIBARP,
2518 "Remove ARP Entry for IP :%d.%d.%d.%d , port %d\n",
2519 (remove_key->ip >> 24),
2520 ((remove_key->ip & 0x00ff0000) >> 16),
2521 ((remove_key->ip & 0x0000ff00) >> 8),
2522 ((remove_key->ip & 0x000000ff)), remove_key->port_id);
2523 remove_arp_entry((uint32_t) remove_key->ip,
2524 (uint8_t) remove_key->port_id, arg);
2528 void nd_timer_callback(struct rte_timer *timer, void *arg)
2530 struct nd_timer_key *remove_key = (struct nd_timer_key *)arg;
2532 RTE_LOG(INFO, LIBARP, "nd time callback : expire :%d\n",
2533 (int)timer->expire);
2534 remove_nd_entry_ipv6(remove_key->ipv6, remove_key->port_id);
2538 void create_arp_table(void)
2542 for (i = 0; i < MAX_ARP_DATA_ENTRY_TABLE; i++) {
2543 populate_arp_entry((const struct ether_addr *)
2544 &arp_entry_data_table[i].eth_addr,
2545 arp_entry_data_table[i].ip,
2546 (uint8_t) arp_entry_data_table[i].port,
2553 void create_nd_table(void)
2557 for (i = 0; i < MAX_ND_DATA_ENTRY_TABLE; i++) {
2558 populate_nd_entry((const struct ether_addr *)
2559 nd_entry_data_table[i].eth_addr,
2560 nd_entry_data_table[i].ipv6,
2561 (uint8_t) nd_entry_data_table[i].port,
2568 void send_gratuitous_arp(l2_phy_interface_t *port)
2570 struct ether_hdr *eth_h;
2571 struct arp_hdr *arp_h;
2573 struct rte_mbuf *arp_pkt = lib_arp_pkt;
2576 RTE_LOG(INFO, LIBARP, "PORT ID DOWN.. %s\n", __FUNCTION__);
2581 if (arp_pkt == NULL) {
2583 RTE_LOG(INFO, LIBARP,
2584 "Error allocating arp_pkt rte_mbuf\n");
2588 eth_h = rte_pktmbuf_mtod(arp_pkt, struct ether_hdr *);
2590 ether_addr_copy(&broadcast_ether_addr, ð_h->d_addr);
2591 ether_addr_copy((struct ether_addr *)
2592 &port->macaddr[0], ð_h->s_addr);
2593 eth_h->ether_type = CHECK_ENDIAN_16(ETHER_TYPE_ARP);
2595 arp_h = (struct arp_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
2596 arp_h->arp_hrd = CHECK_ENDIAN_16(ARP_HRD_ETHER);
2597 arp_h->arp_pro = CHECK_ENDIAN_16(ETHER_TYPE_IPv4);
2598 arp_h->arp_hln = ETHER_ADDR_LEN;
2599 arp_h->arp_pln = sizeof(uint32_t);
2600 arp_h->arp_op = CHECK_ENDIAN_16(ARP_OP_REQUEST);
2602 ether_addr_copy((struct ether_addr *)
2603 &port->macaddr[0], &arp_h->arp_data.arp_sha);
2604 if (port->ipv4_list == NULL) {
2606 RTE_LOG(INFO, LIBARP, "port->ipv4_list is NULL.. %s\n",
2610 arp_h->arp_data.arp_sip = (((ipv4list_t *) (port->ipv4_list))->ipaddr);
2611 ether_addr_copy(&null_ether_addr, &arp_h->arp_data.arp_tha);
2612 //arp_h->arp_data.arp_tip = rte_cpu_to_be_32(ip);
2613 arp_h->arp_data.arp_tip = 0; //(((ipv4list_t *) (port->ipv4_list))->ipaddr);
2614 // RTE_LOG(INFO, LIBARP,"arp tip:%x arp sip :%x\n", arp_h->arp_data.arp_tip,
2615 //arp_h->arp_data.arp_sip);
2616 // mmcd changed length from 60 to 42 - real length of arp request, no padding on ethernet needed - looks now like linux arp
2617 arp_pkt->pkt_len = 42;
2618 arp_pkt->data_len = 42;
2620 if (ARPICMP_DEBUG) {
2621 RTE_LOG(INFO, LIBARP, "SENDING GRATUITOUS ARP REQUEST\n");
2622 print_mbuf("TX", port->pmdid, arp_pkt, __LINE__);
2624 port->transmit_single_pkt(port, arp_pkt);
2627 void set_arpdebug(int flag)
2630 RTE_LOG(INFO, LIBARP, "Debugs turned on\n\r");
2635 RTE_LOG(INFO, LIBARP, "Debugs turned off\n\r");
2641 void set_arptimeout(uint32_t timeout_val)
2643 if (timeout_val == 0) {
2644 RTE_LOG(INFO, LIBARP, "Cannot be zero...\n\r");
2648 RTE_LOG(INFO, LIBARP,
2649 "set_arptimeout: arp_timeout %u, timeout_val %u\n\r",
2650 arp_timeout, timeout_val);
2651 arp_timeout = timeout_val;
2653 RTE_LOG(INFO, LIBARP, "set_arptimeout: arp_timeout %u\n\r",