gateway: Created common code for routing in gateway
[samplevnf.git] / common / vnf_common / vnf_common.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_VNF_COMMON_H__
18 #define __INCLUDE_VNF_COMMON_H__
19
20 #include <rte_pipeline.h>
21 #include <rte_ether.h>
22
23 #define MBUF_HDR_ROOM 256
24 #define ETH_HDR_SIZE  14
25 #define IP_HDR_SRC_ADR_OFST 12
26 #define IP_HDR_DST_ADR_OFST 16
27 #define IP_HDR_PROTOCOL_OFST 9
28 #define IP_HDR_SIZE  20
29 #define IPV6_HDR_SRC_ADR_OFST 8
30 #define IPV6_HDR_DST_ADR_OFST 24
31 #define IPV6_HDR_PROTOCOL_OFST 6
32 #define IPV6_HDR_SIZE  40
33
34 #define ETH_TYPE_ARP     0x0806
35 #define ETH_TYPE_IPV4    0x0800
36
37 #define IP_PROTOCOL_ICMP 1
38 #define IP_PROTOCOL_TCP  6
39 #define IP_PROTOCOL_UDP  17
40
41 #define ETH_TYPE_IPV6    0x86DD
42 #define IP_PROTOCOL_ICMPV6 58
43
44 #define PKT_ING_DIR 0
45 #define PKT_EGR_DIR 1
46
47 #ifndef PIPELINE_MAX_PORT_IN
48 #define PIPELINE_MAX_PORT_IN 64
49 #endif
50
51 #define RTE_PIPELINE_MAX_NAME_SZ 124
52
53 #define INVALID_DESTIF 255
54
55 enum {
56         VNF_PRV_PORT_ID,
57         VNF_PUB_PORT_ID,
58 };
59 void show_ports_info(void);
60 void trim(char *input);
61 uint8_t get_in_port_dir(uint8_t in_port_id);
62 uint8_t is_phy_port_privte(uint16_t phy_port);
63 uint32_t get_prv_to_pub_port(uint32_t *ip_addr, uint8_t type);
64 uint32_t get_pub_to_prv_port(uint32_t *ip_addr, uint8_t type);
65
66 static inline void drop_pkt(uint32_t pkt_num, uint64_t *mask)
67 {
68         *mask ^= 1LLU << pkt_num;
69 }
70
71 extern uint8_t in_port_dir_a[PIPELINE_MAX_PORT_IN];
72 extern uint8_t prv_to_pub_map[PIPELINE_MAX_PORT_IN];
73 extern uint8_t pub_to_prv_map[PIPELINE_MAX_PORT_IN];
74 extern uint8_t prv_in_port_a[PIPELINE_MAX_PORT_IN];
75
76 extern uint32_t link_hw_addr_array_idx;
77
78 struct rte_port_in {
79         /* Input parameters */
80         struct rte_port_in_ops ops;
81         rte_pipeline_port_in_action_handler f_action;
82         void *arg_ah;
83         uint32_t burst_size;
84
85         /* The table to which this port is connected */
86         uint32_t table_id;
87
88         /* Handle to low-level port */
89         void *h_port;
90
91         /* List of enabled ports */
92         struct rte_port_in *next;
93
94         /* Statistics */
95         uint64_t n_pkts_dropped_by_ah;
96 };
97
98 struct rte_port_out {
99         /* Input parameters */
100         struct rte_port_out_ops ops;
101         rte_pipeline_port_out_action_handler f_action;
102         void *arg_ah;
103
104         /* Handle to low-level port */
105         void *h_port;
106
107         /* Statistics */
108         uint64_t n_pkts_dropped_by_ah;
109 };
110
111 struct rte_table {
112         /* Input parameters */
113         struct rte_table_ops ops;
114         rte_pipeline_table_action_handler_hit f_action_hit;
115         rte_pipeline_table_action_handler_miss f_action_miss;
116         void *arg_ah;
117         struct rte_pipeline_table_entry *default_entry;
118         uint32_t entry_size;
119
120         uint32_t table_next_id;
121         uint32_t table_next_id_valid;
122
123         /* Handle to the low-level table object */
124         void *h_table;
125
126         /* Statistics */
127         uint64_t n_pkts_dropped_by_lkp_hit_ah;
128         uint64_t n_pkts_dropped_by_lkp_miss_ah;
129         uint64_t n_pkts_dropped_lkp_hit;
130         uint64_t n_pkts_dropped_lkp_miss;
131 };
132
133
134 struct rte_pipeline {
135         /* Input parameters */
136         char name[RTE_PIPELINE_MAX_NAME_SZ];
137         int socket_id;
138         uint32_t offset_port_id;
139
140         /* Internal tables */
141         struct rte_port_in ports_in[RTE_PIPELINE_PORT_IN_MAX];
142         struct rte_port_out ports_out[RTE_PIPELINE_PORT_OUT_MAX];
143         struct rte_table tables[RTE_PIPELINE_TABLE_MAX];
144
145         /* Occupancy of internal tables */
146         uint32_t num_ports_in;
147         uint32_t num_ports_out;
148         uint32_t num_tables;
149
150         /* List of enabled ports */
151         uint64_t enabled_port_in_mask;
152         struct rte_port_in *port_in_next;
153
154         /* Pipeline run structures */
155         struct rte_mbuf *pkts[RTE_PORT_IN_BURST_SIZE_MAX];
156         struct rte_pipeline_table_entry *entries[RTE_PORT_IN_BURST_SIZE_MAX];
157         uint64_t action_mask0[RTE_PIPELINE_ACTIONS];
158         uint64_t action_mask1[RTE_PIPELINE_ACTIONS];
159         uint64_t pkts_mask;
160         uint64_t n_pkts_ah_drop;
161         uint64_t pkts_drop_mask;
162 } __rte_cache_aligned;
163
164 /* RTE_ DPDK LIB structures to get HWQ & SWQ info */
165 struct rte_port_ethdev_writer {
166          struct rte_port_out_stats stats;
167
168          struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
169          uint32_t tx_burst_sz;
170          uint16_t tx_buf_count;
171          uint64_t bsz_mask;
172          uint16_t queue_id;
173          uint8_t port_id;
174 };
175 struct rte_port_ethdev_reader {
176          struct rte_port_in_stats stats;
177
178          uint16_t queue_id;
179          uint8_t port_id;
180 };
181 struct rte_port_ring_writer {
182          struct rte_port_out_stats stats;
183
184          struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
185          struct rte_ring *ring;
186          uint32_t tx_burst_sz;
187          uint32_t tx_buf_count;
188          uint64_t bsz_mask;
189          uint32_t is_multi;
190 };
191 struct rte_port_ring_reader {
192          struct rte_port_in_stats stats;
193
194          struct rte_ring *ring;
195 };
196
197 uint8_t get_in_port_dir(uint8_t in_port_id);
198 uint8_t is_phy_port_privte(uint16_t phy_port);
199 uint8_t is_port_index_privte(uint16_t phy_port);
200 #endif