5def01a08ef40d9372b09ad52b2cd2bab96b2e78
[vpp.git] / src / vnet / flow / flow.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef included_vnet_flow_flow_h
17 #define included_vnet_flow_flow_h
18
19 #include <vppinfra/clib.h>
20 #include <vppinfra/pcap.h>
21 #include <vnet/l3_types.h>
22 #include <vnet/ip/ip4_packet.h>
23 #include <vnet/ip/ip6_packet.h>
24 #include <vnet/ethernet/packet.h>
25
26 #define foreach_flow_type \
27   /* l2 flow*/ \
28   _(ETHERNET, ethernet, "ethernet") \
29   /* l4 flow*/ \
30   _(IP4_N_TUPLE, ip4_n_tuple, "ipv4-n-tuple") \
31   _(IP6_N_TUPLE, ip6_n_tuple, "ipv6-n-tuple") \
32   /* L4 tunnel flow*/ \
33   _(IP4_VXLAN, ip4_vxlan, "ipv4-vxlan") \
34   _(IP6_VXLAN, ip6_vxlan, "ipv6-vxlan") \
35   _(IP4_GTPC, ip4_gtpc, "ipv4-gtpc") \
36   _(IP4_GTPU, ip4_gtpu, "ipv4-gtpu") \
37   _(IP4_GTPU_IP4, ip4_gtpu_ip4, "ipv4-gtpu-ipv4") \
38   _(IP4_GTPU_IP6, ip4_gtpu_ip6, "ipv4-gtpu-ipv6") \
39   _(IP6_GTPC, ip6_gtpc, "ipv6-gtpc") \
40   _(IP6_GTPU, ip6_gtpu, "ipv6-gtpu") \
41   _(IP6_GTPU_IP4, ip6_gtpu_ip4, "ipv6-gtpu-ipv4") \
42   _(IP6_GTPU_IP6, ip6_gtpu_ip6, "ipv6-gtpu-ipv6")
43
44 #define foreach_flow_entry_ethernet \
45   _fe(ethernet_header_t, eth_hdr)
46
47 #define foreach_flow_entry_ip4_n_tuple \
48   _fe(ip4_address_and_mask_t, src_addr) \
49   _fe(ip4_address_and_mask_t, dst_addr) \
50   _fe(ip_port_and_mask_t, src_port) \
51   _fe(ip_port_and_mask_t, dst_port) \
52   _fe(ip_protocol_t, protocol)
53
54 #define foreach_flow_entry_ip6_n_tuple \
55   _fe(ip6_address_and_mask_t, src_addr) \
56   _fe(ip6_address_and_mask_t, dst_addr) \
57   _fe(ip_port_and_mask_t, src_port) \
58   _fe(ip_port_and_mask_t, dst_port) \
59   _fe(ip_protocol_t, protocol)
60
61 #define foreach_flow_entry_ip4_vxlan \
62   _fe(ip4_address_t, src_addr) \
63   _fe(ip4_address_t, dst_addr) \
64   _fe(u16, dst_port) \
65   _fe(u16, vni)
66
67 #define foreach_flow_entry_ip6_vxlan \
68   _fe(ip6_address_t, src_addr) \
69   _fe(ip6_address_t, dst_addr) \
70   _fe(u16, dst_port) \
71   _fe(u16, vni)
72
73 #define foreach_flow_entry_ip4_gtpc \
74   foreach_flow_entry_ip4_n_tuple \
75   _fe(u32, teid)
76
77 #define foreach_flow_entry_ip4_gtpu \
78   foreach_flow_entry_ip4_n_tuple \
79   _fe(u32, teid)
80
81 #define foreach_flow_entry_ip4_gtpu_ip4 \
82   foreach_flow_entry_ip4_gtpu \
83   _fe(ip4_address_and_mask_t, inner_src_addr) \
84   _fe(ip4_address_and_mask_t, inner_dst_addr)
85
86 #define foreach_flow_entry_ip4_gtpu_ip6 \
87   foreach_flow_entry_ip4_gtpu \
88   _fe(ip6_address_and_mask_t, inner_src_addr) \
89   _fe(ip6_address_and_mask_t, inner_dst_addr)
90
91 #define foreach_flow_entry_ip6_gtpc \
92   foreach_flow_entry_ip6_n_tuple \
93   _fe(u32, teid)
94
95 #define foreach_flow_entry_ip6_gtpu \
96   foreach_flow_entry_ip6_n_tuple \
97   _fe(u32, teid)
98
99 #define foreach_flow_entry_ip6_gtpu_ip4 \
100   foreach_flow_entry_ip6_gtpu \
101   _fe(ip4_address_and_mask_t, inner_src_addr) \
102   _fe(ip4_address_and_mask_t, inner_dst_addr)
103
104 #define foreach_flow_entry_ip6_gtpu_ip6 \
105   foreach_flow_entry_ip6_gtpu \
106   _fe(ip6_address_and_mask_t, inner_src_addr) \
107   _fe(ip6_address_and_mask_t, inner_dst_addr)
108
109 #define foreach_flow_action \
110   _(0, COUNT, "count") \
111   _(1, MARK, "mark") \
112   _(2, BUFFER_ADVANCE, "buffer-advance") \
113   _(3, REDIRECT_TO_NODE, "redirect-to-node") \
114   _(4, REDIRECT_TO_QUEUE, "redirect-to-queue") \
115   _(5, DROP, "drop")
116
117 typedef enum
118 {
119 #define _(v,n,s)  VNET_FLOW_ACTION_##n = (1 << v),
120   foreach_flow_action
121 #undef _
122 } vnet_flow_action_t;
123
124
125 #define foreach_flow_error \
126   _( -1, NOT_SUPPORTED, "not supported")                        \
127   _( -2, ALREADY_DONE, "already done")                          \
128   _( -3, ALREADY_EXISTS, "already exists")                      \
129   _( -4, NO_SUCH_ENTRY, "no such entry")                        \
130   _( -5, NO_SUCH_INTERFACE, "no such interface")                \
131   _( -6, INTERNAL, "internal error")
132
133 typedef enum
134 {
135   VNET_FLOW_NO_ERROR = 0,
136 #define _(v,n,s)  VNET_FLOW_ERROR_##n = v,
137   foreach_flow_error
138 #undef _
139 } vnet_flow_error_t;
140
141 typedef struct
142 {
143   u16 port, mask;
144 } ip_port_and_mask_t;
145
146 typedef enum
147 {
148   VNET_FLOW_TYPE_UNKNOWN,
149 #define _(a,b,c) VNET_FLOW_TYPE_##a,
150   foreach_flow_type
151 #undef _
152     VNET_FLOW_N_TYPES,
153 } vnet_flow_type_t;
154
155
156 /*
157  * Create typedef struct vnet_flow_XXX_t
158  */
159 #define _fe(a, b) a b;
160 #define _(a,b,c) \
161 typedef struct { \
162 int foo; \
163 foreach_flow_entry_##b \
164 } vnet_flow_##b##_t;
165 foreach_flow_type;
166 #undef _
167 #undef _fe
168
169 /* main flow struct */
170 typedef struct
171 {
172   /* flow type */
173   vnet_flow_type_t type;
174
175   /* flow index */
176   u32 index;
177
178   /* bitmap of flow actions (VNET_FLOW_ACTION_*) */
179   u32 actions;
180
181   /* flow id for VNET_FLOW_ACTION_MARK */
182   u32 mark_flow_id;
183
184   /* node index and next index for VNET_FLOW_ACTION_REDIRECT_TO_NODE */
185   u32 redirect_node_index;
186   u32 redirect_device_input_next_index;
187
188   /* queue for VNET_FLOW_ACTION_REDIRECT_TO_QUEUE */
189   u32 redirect_queue;
190
191   /* buffer offset for VNET_FLOW_ACTION_BUFFER_ADVANCE */
192   i32 buffer_advance;
193
194   union
195   {
196 #define _(a,b,c) vnet_flow_##b##_t b;
197     foreach_flow_type
198 #undef _
199   };
200
201   /* per-interface private data */
202   uword *private_data;
203 } vnet_flow_t;
204
205 int vnet_flow_get_range (vnet_main_t * vnm, char *owner, u32 count,
206                          u32 * start);
207 int vnet_flow_add (vnet_main_t * vnm, vnet_flow_t * flow, u32 * flow_index);
208 int vnet_flow_enable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index);
209 int vnet_flow_disable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index);
210 int vnet_flow_del (vnet_main_t * vnm, u32 flow_index);
211 vnet_flow_t *vnet_get_flow (u32 flow_index);
212
213 typedef struct
214 {
215   u32 start;
216   u32 count;
217   u8 *owner;
218 } vnet_flow_range_t;
219
220 typedef struct
221 {
222   /* pool of device flow entries */
223   vnet_flow_t *global_flow_pool;
224
225   /* flow ids allocated */
226   u32 flows_used;
227
228   /* vector of flow ranges */
229   vnet_flow_range_t *ranges;
230
231 } vnet_flow_main_t;
232
233 extern vnet_flow_main_t flow_main;
234
235 format_function_t format_flow_actions;
236 format_function_t format_flow_enabled_hw;
237
238 #endif /* included_vnet_flow_flow_h */
239
240 /*
241  * fd.io coding-style-patch-verification: ON
242  *
243  * Local Variables:
244  * eval: (c-set-style "gnu")
245  * End:
246  */