e7fcb42148ce38c698b5016ff51ca1a71946b900
[samplevnf.git] / common / VIL / l2l3_stack / lib_arp.h
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
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
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #ifndef __INCLUDE_LIB_ARP_H__
18 #define __INCLUDE_LIB_ARP_H__
19
20 #include <rte_pipeline.h>
21 #include "rte_ether.h"
22 #include "l2_proto.h"
23 #include "app.h"
24
25 #define ND_IPV6_ADDR_SIZE 16    /**< 16 Byte of IPv6 Address. */
26 #define ND_IPV6_TIMER_EXPIRY 300  /**< in Seconds, Timer for ND IPv6 Expiry */
27 #define ARP_TIMER_EXPIRY 20      /**< in Seconds, TIMER for ARP Expiry */
28 #define TIMER_MILLISECOND 1
29 #define RTE_LOGTYPE_LIBARP RTE_LOGTYPE_USER1
30 #define MAX_ND_RT_ENTRY 32
31 #define MAX_ARP_RT_ENTRY 32
32 #define NUM_DESC                (get_arp_buf())
33 #define ARP_BUF_DEFAULT                30000
34 #define PROBE_TIME             500
35 #undef L3_STACK_SUPPORT
36
37 /**
38 * A structure for Route table entries of IPv4
39 */
40
41 struct lib_arp_route_table_entry {
42         uint32_t ip;    /**< Ipv4 address*/
43         uint32_t mask;  /**< mask */
44         uint32_t port;  /**< Physical port */
45         uint32_t nh;    /**< next hop */
46         uint32_t nh_mask;
47 };
48
49 #define MAX_LOCAL_MAC_ADDRESS          32
50 #define MAX_PORTS                      32
51 struct arp_cache {
52         uint32_t nhip[MAX_LOCAL_MAC_ADDRESS];
53         struct ether_addr link_hw_laddr[MAX_LOCAL_MAC_ADDRESS];
54         uint32_t num_nhip;
55 };
56
57 /**
58 * A structure for Route table entires of IPv6
59 *
60 */
61 struct lib_nd_route_table_entry {
62         uint8_t ipv6[16];       /**< Ipv6 address */
63         uint8_t depth;          /**< Depth */
64         uint32_t port;          /**< Port */
65         uint8_t nhipv6[16];     /**< next hop Ipv6 */
66 };
67
68 struct arp_data {
69         struct lib_arp_route_table_entry
70             lib_arp_route_table[MAX_ARP_RT_ENTRY];
71         uint8_t lib_arp_route_ent_cnt;
72         struct lib_nd_route_table_entry
73             lib_nd_route_table[MAX_ARP_RT_ENTRY];
74         uint8_t lib_nd_route_ent_cnt;
75         struct arp_cache arp_local_cache[MAX_PORTS];
76         struct ether_addr link_hw_addr[MAX_LOCAL_MAC_ADDRESS];
77         uint32_t link_hw_addr_array_idx;
78         uint8_t arp_cache_hw_laddr_valid[MAX_LOCAL_MAC_ADDRESS];
79 } __rte_cache_aligned;
80
81 uint8_t arp_cache_dest_mac_present(uint32_t out_port);
82
83 extern struct lib_nd_route_table_entry lib_nd_route_table[MAX_ND_RT_ENTRY];
84 extern struct lib_arp_route_table_entry lib_arp_route_table[MAX_ARP_RT_ENTRY];
85 extern struct ether_addr *get_local_link_hw_addr(uint8_t out_port, uint32_t nhip);
86 extern struct arp_cache arp_local_cache[MAX_PORTS];
87 extern void prefetch(void);
88 extern struct arp_entry_data *arp_data_ptr[16];
89 uint32_t get_arp_buf(void);
90
91 enum {
92         ARP_FOUND,
93         ARP_NOT_FOUND,
94         NH_NOT_FOUND,
95 };
96
97 enum arp_key_type {
98         ARP_IPV4,
99         ND_IPV6,
100 };
101
102 struct arp_key_ipv4 {
103         uint32_t ip;     /**< IP address */
104         uint8_t port_id; /**< Port id */
105         uint8_t filler1; /**< filler 1, for better hash key */
106         uint8_t filler2; /**< filler 2, for better hash key */
107         uint8_t filler3; /**< filler 3, for better hash key */
108 };
109
110 /**
111 * IPv6
112 */
113 struct nd_key_ipv6 {
114         uint8_t ipv6[ND_IPV6_ADDR_SIZE]; /**< 128 Bit of IPv6 Address*/
115         uint8_t port_id;                 /**< Port id */
116         uint8_t filler1;
117         uint8_t filler2;
118         uint8_t filler3;
119 };
120
121 /**
122 * Arp Key
123 */
124 struct arp_key {
125         enum arp_key_type type;
126         union {
127                 struct arp_key_ipv4 ipv4;
128         } key;  /**< Key of type arp key Ipv4 */
129 };
130
131 /**
132 * call back function parameter pair remove nd entry
133 *
134 */
135
136 struct nd_timer_key {
137         uint8_t ipv6[ND_IPV6_ADDR_SIZE];   /**< IPv6 address */
138         uint8_t port_id;                 /**< Port id */
139 } __rte_cache_aligned;
140
141 /**
142 * call back function parameter remove arp entry
143 *
144 */
145 struct arp_timer_key {
146         uint32_t ip;     /**< Ip address */
147         uint8_t port_id; /**< Port id */
148 } __rte_cache_aligned;
149
150 extern uint32_t ARPICMP_DEBUG;
151
152 enum {
153         INCOMPLETE,
154         COMPLETE,
155         PROBE,
156         STALE
157 };
158 #define USED_TIME       5
159 //#define COMPLETE   1 /**< ARP entry populated and echo reply recieved. */
160 //#define INCOMPLETE 0 /**< ARP entry populated and either awaiting echo reply or stale entry. */
161
162 extern uint32_t NDIPV6_DEBUG;  /**< ND IPv6 */
163
164 #define ICMPv6_COMPLETE   1 /**< ICMPv6 entry populated and echo reply recieved. */
165 #define ICMPv6_INCOMPLETE 0 /**< ICMPv6 entry populated and either awaiting echo reply or stale entry. */
166 #define STATIC_ARP 1                    /**< Static ARP Entry. */
167 #define DYNAMIC_ARP 0                   /**< Dynamic ARP Entry. */
168 #define STATIC_ND 1                     /**< Static ND Entry. */
169 #define DYNAMIC_ND 0                    /**< Dynamic ND Entry. */
170
171 /**
172 * A structure is used to defined the ARP entry data
173 * This structure is used as a input parameters for entry of ARP data
174 */
175
176 struct arp_entry_data {
177         struct ether_addr eth_addr; /**< ethernet address */
178         uint32_t ip;                            /**< IP address */
179         uint8_t port;                           /**< Port */
180         uint8_t status;                         /**< Status of entry */
181         uint8_t mode;                           /**< Mode */
182         uint8_t retry_count;                    /**< retry count for ARP*/
183         struct rte_timer *timer;    /**< Timer Associated with ARP*/
184         struct arp_timer_key *timer_key;
185         struct rte_ring *queue;     /** pkts queued */
186         rte_rwlock_t queue_lock;    /** queue lock */
187         struct rte_mbuf **buf_pkts;
188         struct rte_mbuf *buffered_pkt_list_head;
189         struct rte_mbuf *buffered_pkt_list_tail;
190         uint32_t num_pkts;
191         uint32_t n_confirmed;
192         uint32_t n_last_update;
193 } __attribute__ ((packed));
194
195 /**
196 * A structure is used to defined the table for arp entry data
197 * This structure is used to maintain the arp entry data
198 */
199
200 struct table_arp_entry_data {
201         uint8_t eth_addr[6];     /**< Ethernet address */
202         uint8_t port;            /**< port */
203         uint8_t status;          /**< status of entry */
204         uint32_t ip;             /**< Ip address */
205 } __attribute__ ((packed));
206
207 /**
208 * A structure is used to define the ND entry data for IPV6
209 * This structure is used as a input parameters  for ND entry data
210 */
211
212 struct nd_entry_data {
213         struct ether_addr eth_addr;             /**< Ethernet address */
214         uint8_t port;                           /**< port */
215         uint8_t status;                         /**< statusof the entry */
216         uint8_t mode;                           /**< Mode */
217         uint8_t ipv6[ND_IPV6_ADDR_SIZE];  /**< Ipv6 address */
218         struct rte_timer *timer;                /**< Timer */
219 } __attribute__ ((packed));
220
221 /**
222 * A structure is used to define the table for ND entry data
223 * This structure is used to maintain ND entry data
224 *
225 */
226
227 struct table_nd_entry_data {
228         uint8_t eth_addr[6];             /**< Ethernet address */
229         uint8_t port;                    /**< Port */
230         uint8_t status;                  /**< status of Entry */
231         uint8_t ipv6[ND_IPV6_ADDR_SIZE]; /**< IPv6 address */
232         struct rte_timer *timer;         /**< Timer */
233 } __attribute__ ((packed));
234
235 /**
236 * To get the destination MAC address andnext hop for the ip address  and outgoing port
237 * @param1 ip addr
238 * IP address for which MAC address is needed.
239 * @param2 phy_port
240 *  Physical Port
241 * @param3 ether_addr
242 * pointer to the ether_addr, This gets update with valid MAC addresss
243 * @Param4 next nhip
244 * Gets the next hop IP by Ip address and physical port
245 * @return
246 * 0 if failure, and 1 if success
247 */
248
249 struct arp_entry_data *get_dest_mac_address(const uint32_t ipaddr, uint32_t *phy_port, struct ether_addr *hw_addr, uint32_t *nhip);
250 /**
251 * To get the destination MAC address andnext hop for the ip address  and outgoing port
252 * @param1 ip addr
253 * IP address for which MAC address is needed.
254 * @param2 phy_port
255 *  Physical Port
256 * @param3 ether_addr
257 * pointer to the ether_addr, This gets update with valid MAC addresss
258 * @Param4 next nhip
259 * Gets the next hop IP by Ip address and physical port
260 * @return
261 * 0 if failure, and 1 if success
262 */
263 struct arp_entry_data *get_dest_mac_addr_port(const uint32_t ipaddr,
264                                  uint32_t *phy_port, struct ether_addr *hw_addr);
265
266 /**
267 * To get the destination mac address for  IPv4  address
268 * @param  Ipaddr
269 * IP address which need the destination mac address
270 * @param Phy_port
271 * physical port
272 * @param ether_addr
273 * pointer to the ether_addr, This gets update with valid mac address
274 * @return
275 * 0 if failure, 1 if success
276 */
277 int get_dest_mac_addr(const uint32_t ipaddr, uint32_t *phy_port,
278                                         struct ether_addr *hw_addr);
279
280 /**
281 * To get the destination mac address for IPV6 address
282 * @param ipv6addr
283 * IPv6 address which need the destination mac adress
284 * @param Phy_Port
285 * physical prt
286 * @param ether_addr
287 * pointer to the ether_address, This gets update with valid mac address
288 * @param Nhipv6[]
289 * Gets the next hop ipv6 address by ipv6 address and physical port
290 * @return
291 * 0 if failure, 1 ifsuccess
292 */
293 int get_dest_mac_address_ipv6(uint8_t ipv6addr[], uint32_t *phy_port,
294                                                 struct ether_addr *hw_addr, uint8_t nhipv6[]);
295 /**
296 * To get the destination mac address for IPV6 address
297 * @param ipv6addr
298 * IPv6 address which need the destination mac adress
299 * @param Phy_Port
300 * physical prt
301 * @param ether_addr
302 * pointer to the ether_address, This gets update with valid mac address
303 * @param Nhipv6[]
304 * Gets the next hop ipv6 address by ipv6 address and physical port
305 * @return
306 * 0 if failure, 1 ifsuccess
307 */
308
309 int get_dest_mac_address_ipv6_port(uint8_t ipv6addr[], uint32_t *phy_port,
310                                          struct ether_addr *hw_addr,
311                                          uint8_t nhipv6[]);
312 int arp_queue_unresolved_packet(struct arp_entry_data * arp_data,
313                         struct rte_mbuf * m);
314 extern void arp_send_buffered_pkts(struct arp_entry_data *ret_arp_data,struct ether_addr *hw_addr, uint8_t port_id);
315
316 /**
317 * To get hardware link address
318 * @param out_port
319 * out going  port
320 */
321
322 struct ether_addr *get_link_hw_addr(uint8_t out_port);
323
324 /**
325 * This prints the Arp Table
326 * @param void
327 *
328 */
329 void print_arp_table(void);
330
331 /**
332 * This prints the ND table
333 * @param void
334 *
335 */
336 void print_nd_table(void);
337
338 /**
339 * This removes arp entry from Table
340 * @param ipaddr
341 * Ipv4 address
342 * @param portid
343 * Port id
344 */
345 void remove_arp_entry(struct arp_entry_data *ret_arp_data, void *arg);
346
347 /**
348 * Removes ND entry from Nd Table
349 * @Param ipv6addr[]
350 * Ipv6 address
351 * @Param portid
352 * Port id
353 */
354
355 void remove_nd_entry_ipv6(uint8_t ipv6addr[], uint8_t portid);
356
357 /**
358 * Populate arp entry in arp Table
359 * @param ether_addr
360 * Ethernet address
361 * @param ipaddr
362 * Ipv4 adress
363 * @Param portid
364 * port id
365 * @Param mode
366 * Mode
367 */
368 void populate_arp_entry(const struct ether_addr *hw_addr, uint32_t ipaddr,
369                         uint8_t portid, uint8_t mode);
370
371 /**
372 * Populate ND entry in ND Table
373 * @param ether_addr
374 * Ethernet address
375 * @param ip[]
376 * Ipv6 adress
377 * @Param portid
378 * port id
379 * @Param mode
380 * Mode
381 */
382
383 void populate_nd_entry(const struct ether_addr *hw_addr, uint8_t ip[],
384                                          uint8_t portid, uint8_t mode);
385
386 /**
387 * To send ARp request
388 * @Param port_id
389 * port id
390 @ Param IP
391 * Ip address
392 */
393
394 void request_arp(uint8_t port_id, uint32_t ip);
395
396 /**
397 * TO send echo request
398 * @param port_id
399 * Port id
400 * @Param ip
401 * Ip address
402 */
403 struct rte_mbuf *request_echo(uint32_t port_id, uint32_t ip);
404
405 /**
406 * To send icmpv6 echo request
407 * @Param port_id
408 * Port id
409 * @Param ipv6
410 * ipv6 address
411 */
412 struct rte_mbuf *request_icmpv6_echo(uint8_t ipv6[], l2_phy_interface_t *port);
413
414 /**
415 * To request ND
416 * @Param ipv6
417 * ipv6 address
418 * @Param port
419 * pointer to port
420 */
421 struct rte_mbuf *request_nd(uint8_t ipv6[], l2_phy_interface_t *port);
422
423 /**
424 * To process te ARP and ICMP packets
425 * @Param Pkt
426 * Packets to be processed
427 * @Param pkt_num
428 * packet number
429 * @Param portid
430 * port id
431 */
432 void process_arpicmp_pkt(struct rte_mbuf *pkt, l2_phy_interface_t *port);
433
434 /**
435 * IPv4
436 * Validate if key-value pair already exists in the hash table for given key - IPv4
437 * @Param arp_key
438 * Arp key to validate entry
439 */
440 struct arp_entry_data *retrieve_arp_entry(const struct arp_key_ipv4 arp_key, uint8_t mode);
441
442 /**
443 * ND IPv6
444 * Validate if key-value pair already exists in the hash table for given key - ND IPv6
445 * @Param nd_key
446 * Nd key to validate Nd entry
447 */
448
449 struct nd_entry_data *retrieve_nd_entry(struct nd_key_ipv6 nd_key);
450
451 /**
452 * Setsup Arp Initilization
453 */
454 //void lib_arp_init(void);
455 void lib_arp_init(struct pipeline_params *params, struct app_params *app);
456 #if 0
457 void set_port_to_loadb_map(uint8_t pipeline_num);
458
459 /**
460 * Acts on port_to_loadb_map
461 */
462 uint8_t get_port_to_loadb_map(uint8_t phy_port_id);
463
464 void set_phy_inport_map(uint8_t pipeline_num, uint8_t *map);
465 void set_phy_outport_map(uint8_t pipeline_num, uint8_t *map);
466
467 /**
468 * Acts on lb_outport_id
469 */
470
471 uint8_t get_loadb_outport_id(uint8_t actual_phy_port);
472 uint8_t get_vnf_set_num(uint8_t pipeline_num);
473
474 void pipelines_port_info(void);
475 void pipelines_map_info(void);
476 #endif
477 /**
478 * A callback for arp Timer
479 * @Param rte_timer
480 * timer pointer
481 * @Param arg
482 * arguments to timer
483 */
484 void arp_timer_callback(struct rte_timer *, void *arg);
485
486 /**
487 * A callback for ND timer
488 * @Param rte_timer
489 * timer pointer
490 * @Param arg
491 * arguments to timer
492 */
493 void nd_timer_callback(struct rte_timer *timer, void *arg);
494
495 /**
496 * To create Arp Table
497 * @param void
498 */
499 void create_arp_table(void);
500 /**
501 * To create ND Table
502 * @param void
503 */
504 void create_nd_table(void);
505
506 /**
507 * To parse and process the Arp and icmp packets
508 * @Param pkt
509 * pkt to process
510 * @Param pkt_num
511 * pkt number
512 * @Param pkt_mask
513 * packet mask
514 * @Param port
515 * pointer to port
516 */
517 void process_arpicmp_pkt_parse(struct rte_mbuf **pkt, uint16_t pkt_num,
518                                                  uint64_t pkt_mask, l2_phy_interface_t *port);
519
520 /**
521 * Sends garp packet
522 * @Param port
523 * pointer to port
524 */
525 void send_gratuitous_arp(l2_phy_interface_t *port);
526 /**
527 * To set arp debug
528 * @Param flag
529 * set 1 unset 0
530 */
531 void set_arpdebug(int flag);
532 /**
533 * To set timer for arp entry
534 * @Param timeout_val
535 * timer val for arp entry
536 */
537 void set_arptimeout(uint32_t timeout_val);
538 /**
539 * To get nexthop for ipv4
540 * @Param ipv4
541 * ipv4 address
542 * @Param
543 * timeout_val to set
544 */
545 uint32_t get_nh(uint32_t, uint32_t *, struct ether_addr *addr);
546 /**
547 * To get nexthop for ipv6
548 * @Param ipv6
549 * ipv6 address
550 * @Param port
551 * pointer to port
552 * @Param nhipv6
553 * next hop ipv6
554 */
555 void get_nh_ipv6(uint8_t ipv6[], uint32_t *port, uint8_t nhipv6[]);
556 #endif