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