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