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