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