e1b262dc991a78bb982e88c03fca4d642ebc0d53
[samplevnf.git] / VNFs / DPPD-PROX / packet_utils.h
1 /*
2 // Copyright (c) 2010-2020 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 #ifndef _PACKET_UTILS_H_
17 #define _PACKET_UTILS_H_
18
19 #include <rte_cycles.h>
20
21 #include "prox_compat.h"
22 #include "arp.h"
23 #include "quit.h"
24 #include "prox_malloc.h"
25 #include "defaults.h"
26 #include "prox_cfg.h"
27 #include "etypes.h"
28
29 #define FLAG_DST_MAC_KNOWN      1
30 #define MAX_ARP_ENTRIES 65536
31
32 #define IP4(x) x & 0xff, (x >> 8) & 0xff, (x >> 16) & 0xff, x >> 24     // From Network (BE)
33 enum {
34         SEND_MBUF_AND_ARP_ND,
35         SEND_MBUF,
36         SEND_ARP_ND,
37         DROP_MBUF
38 };
39 #define DEFAULT_ARP_TIMEOUT     (1000 * 3600 * 24 * 15) // ~15 days = disabled by default
40 #define DEFAULT_ARP_UPDATE_TIME (1000)                  // 1 second
41
42 struct task_base;
43 struct task_args;
44 struct task_master;
45 struct arp_table {
46         uint64_t arp_ndp_retransmit_timeout;
47         uint64_t reachable_timeout;
48         uint32_t ip;
49         uint32_t nh;
50         prox_rte_ether_addr mac;
51         struct ipv6_addr ip6;
52 };
53 struct l3_base {
54         struct rte_ring *ctrl_plane_ring;
55         struct task_base *tmaster;
56         uint32_t flags;
57         uint32_t n_pkts;
58         uint32_t local_ipv4;
59         uint8_t reachable_port_id;
60         uint8_t core_id;
61         uint8_t task_id;
62         uint seed;
63         prox_next_hop_index_type nb_gws;
64         uint32_t arp_ndp_retransmit_timeout;
65         uint32_t reachable_timeout;
66         struct arp_table gw;
67         struct arp_table optimized_arp_table[4];
68         struct rte_hash *ip_hash;
69         struct rte_hash *ip6_hash;
70         struct arp_table *arp_table;
71         struct rte_lpm *ipv4_lpm;
72         struct arp_table *next_hops;
73         struct rte_mempool *arp_nd_pool;
74         struct ipv6_addr local_ipv6;
75         struct ipv6_addr global_ipv6;
76         uint8_t prefix_printed;
77 };
78
79 void task_init_l3(struct task_base *tbase, struct task_args *targ);
80 void task_start_l3(struct task_base *tbase, struct task_args *targ);
81 int write_dst_mac(struct task_base *tbase, struct rte_mbuf *mbuf, uint32_t *ip_dst, uint16_t *vlan, uint64_t **time, uint64_t tsc);
82 int write_ip6_dst_mac(struct task_base *tbase, struct rte_mbuf *mbuf, struct ipv6_addr *ip_dst, uint16_t *vlan);
83 void task_set_gateway_ip(struct task_base *tbase, uint32_t ip);
84 void task_set_local_ip(struct task_base *tbase, uint32_t ip);
85 void handle_ctrl_plane_pkts(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts);
86
87 static inline void update_arp_ndp_retransmit_timeout(struct l3_base *l3, uint64_t *ptr, uint32_t base)
88 {
89         // randomize timers - from 0.5 to 1.5 * configured time
90         const uint64_t hz = rte_get_tsc_hz();
91         uint64_t tsc = rte_rdtsc();
92         uint64_t rand = 500 + (1000L * rand_r(&l3->seed)) / RAND_MAX;
93         *ptr = tsc + (base * rand / 1000) * hz / 1000;
94 }
95 static inline uint8_t get_port(struct rte_mbuf *mbuf)
96 {
97         return mbuf->port;
98 }
99
100 #endif /* _PACKET_UTILS_H_ */