2 *------------------------------------------------------------------
3 * flow_api.c - flow api
5 * Copyright (c) 2020 Intel and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
22 #include <vnet/vnet.h>
23 #include <vlibmemory/api.h>
24 #include <vnet/interface.h>
25 #include <vnet/api_errno.h>
26 #include <vnet/flow/flow.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/tunnel/tunnel_types_api.h>
29 #include <vnet/ip/ip_types_api.h>
30 #include <vnet/vnet_msg_enum.h>
32 #define vl_typedefs /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
36 #define vl_endianfun /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #include <vnet/vnet_all_api_h.h>
46 #include <vlibapi/api_helper_macros.h>
48 #define foreach_vpe_api_msg \
49 _(FLOW_ADD, flow_add) \
50 _(FLOW_DEL, flow_del) \
51 _(FLOW_ENABLE, flow_enable) \
52 _(FLOW_DISABLE, flow_disable)
55 ipv4_addr_and_mask_convert (vl_api_ip4_address_and_mask_t * vl_api_addr,
56 ip4_address_and_mask_t * vnet_addr)
58 clib_memcpy (vnet_addr, vl_api_addr, sizeof (*vnet_addr));
62 ipv6_addr_and_mask_convert (vl_api_ip6_address_and_mask_t * vl_api_addr,
63 ip6_address_and_mask_t * vnet_addr)
65 clib_memcpy (vnet_addr, vl_api_addr, sizeof (*vnet_addr));
69 protocol_and_mask_convert (vl_api_ip_prot_and_mask_t * vl_api_protocol,
70 ip_prot_and_mask_t * vnet_protocol)
72 vnet_protocol->prot = (ip_protocol_t) vl_api_protocol->prot;
73 vnet_protocol->mask = vl_api_protocol->mask;
77 port_and_mask_convert (vl_api_ip_port_and_mask_t * vl_api_port,
78 ip_port_and_mask_t * vnet_port)
80 vnet_port->port = ntohs (vl_api_port->port);
81 vnet_port->mask = ntohs (vl_api_port->mask);
85 ipv4_n_tuple_flow_convert (vl_api_flow_ip4_n_tuple_t * vl_api_flow,
86 vnet_flow_ip4_n_tuple_t * f)
88 ipv4_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
89 ipv4_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
90 protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);
92 port_and_mask_convert (&vl_api_flow->src_port, &f->src_port);
93 port_and_mask_convert (&vl_api_flow->dst_port, &f->dst_port);
97 ipv6_n_tuple_flow_convert (vl_api_flow_ip6_n_tuple_t * vl_api_flow,
98 vnet_flow_ip6_n_tuple_t * f)
100 ipv6_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
101 ipv6_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
102 protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);
104 port_and_mask_convert (&vl_api_flow->src_port, &f->src_port);
105 port_and_mask_convert (&vl_api_flow->dst_port, &f->dst_port);
109 ipv4_n_tuple_tagged_flow_convert (vl_api_flow_ip4_n_tuple_tagged_t *
111 vnet_flow_ip4_n_tuple_tagged_t * f)
113 return ipv4_n_tuple_flow_convert ((vl_api_flow_ip4_n_tuple_t *) vl_api_flow,
114 (vnet_flow_ip4_n_tuple_t *) f);
118 ipv6_n_tuple_tagged_flow_convert (vl_api_flow_ip6_n_tuple_tagged_t *
120 vnet_flow_ip6_n_tuple_tagged_t * f)
122 return ipv6_n_tuple_flow_convert ((vl_api_flow_ip6_n_tuple_t *) vl_api_flow,
123 (vnet_flow_ip6_n_tuple_t *) f);
127 ipv4_l2tpv3oip_flow_convert (vl_api_flow_ip4_l2tpv3oip_t * vl_api_flow,
128 vnet_flow_ip4_l2tpv3oip_t * f)
130 ipv4_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
131 ipv4_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
133 protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);
134 f->session_id = ntohl (vl_api_flow->session_id);
138 ipv4_ipsec_esp_flow_convert (vl_api_flow_ip4_ipsec_esp_t * vl_api_flow,
139 vnet_flow_ip4_ipsec_esp_t * f)
141 ipv4_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
142 ipv4_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
144 protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);
145 f->spi = ntohl (vl_api_flow->spi);
149 ipv4_ipsec_ah_flow_convert (vl_api_flow_ip4_ipsec_ah_t * vl_api_flow,
150 vnet_flow_ip4_ipsec_ah_t * f)
152 ipv4_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
153 ipv4_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
155 protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);
156 f->spi = ntohl (vl_api_flow->spi);
160 ipv4_gtpu_flow_convert (vl_api_flow_ip4_gtpu_t * vl_api_flow,
161 vnet_flow_ip4_gtpu_t * f)
163 ipv4_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
164 ipv4_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
166 port_and_mask_convert (&vl_api_flow->src_port, &f->src_port);
167 port_and_mask_convert (&vl_api_flow->dst_port, &f->dst_port);
169 protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);
170 f->teid = ntohl (vl_api_flow->teid);
174 ipv4_gtpc_flow_convert (vl_api_flow_ip4_gtpc_t * vl_api_flow,
175 vnet_flow_ip4_gtpc_t * f)
177 ipv4_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
178 ipv4_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
180 port_and_mask_convert (&vl_api_flow->src_port, &f->src_port);
181 port_and_mask_convert (&vl_api_flow->dst_port, &f->dst_port);
183 protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);
184 f->teid = ntohl (vl_api_flow->teid);
188 vl_api_flow_add_t_handler (vl_api_flow_add_t * mp)
190 vl_api_flow_add_reply_t *rmp;
194 vl_api_flow_rule_t *f = &mp->flow;
196 vnet_main_t *vnm = vnet_get_main ();
198 flow.type = ntohl (f->type);
199 flow.actions = ntohl (f->actions);
200 flow.mark_flow_id = ntohl (f->mark_flow_id);
201 flow.redirect_node_index = ntohl (f->redirect_node_index);
202 flow.redirect_device_input_next_index =
203 ntohl (f->redirect_device_input_next_index);
204 flow.redirect_queue = ntohl (f->redirect_queue);
205 flow.buffer_advance = ntohl (f->buffer_advance);
209 case VNET_FLOW_TYPE_IP4_N_TUPLE:
210 ipv4_n_tuple_flow_convert (&f->flow.ip4_n_tuple, &flow.ip4_n_tuple);
212 case VNET_FLOW_TYPE_IP6_N_TUPLE:
213 ipv6_n_tuple_flow_convert (&f->flow.ip6_n_tuple, &flow.ip6_n_tuple);
215 case VNET_FLOW_TYPE_IP4_N_TUPLE_TAGGED:
216 ipv4_n_tuple_tagged_flow_convert (&f->flow.ip4_n_tuple_tagged,
217 &flow.ip4_n_tuple_tagged);
219 case VNET_FLOW_TYPE_IP6_N_TUPLE_TAGGED:
220 ipv6_n_tuple_tagged_flow_convert (&f->flow.ip6_n_tuple_tagged,
221 &flow.ip6_n_tuple_tagged);
223 case VNET_FLOW_TYPE_IP4_L2TPV3OIP:
224 ipv4_l2tpv3oip_flow_convert (&f->flow.ip4_l2tpv3oip,
225 &flow.ip4_l2tpv3oip);
227 case VNET_FLOW_TYPE_IP4_IPSEC_ESP:
228 ipv4_ipsec_esp_flow_convert (&f->flow.ip4_ipsec_esp,
229 &flow.ip4_ipsec_esp);
231 case VNET_FLOW_TYPE_IP4_IPSEC_AH:
232 ipv4_ipsec_ah_flow_convert (&f->flow.ip4_ipsec_ah, &flow.ip4_ipsec_ah);
234 case VNET_FLOW_TYPE_IP4_GTPU:
235 ipv4_gtpu_flow_convert (&f->flow.ip4_gtpu, &flow.ip4_gtpu);
237 case VNET_FLOW_TYPE_IP4_GTPC:
238 ipv4_gtpc_flow_convert (&f->flow.ip4_gtpc, &flow.ip4_gtpc);
241 rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
246 rv = vnet_flow_add (vnm, &flow, &flow_index);
250 REPLY_MACRO2(VL_API_FLOW_ADD_REPLY,
252 rmp->flow_index = ntohl (flow_index);
257 vl_api_flow_del_t_handler (vl_api_flow_del_t * mp)
259 vl_api_flow_add_reply_t *rmp;
262 vnet_main_t *vnm = vnet_get_main();
263 rv = vnet_flow_del(vnm, ntohl(mp->flow_index));
265 REPLY_MACRO (VL_API_FLOW_DEL_REPLY);
269 vl_api_flow_enable_t_handler (vl_api_flow_enable_t * mp)
271 vl_api_flow_add_reply_t *rmp;
274 vnet_main_t *vnm = vnet_get_main();
275 rv = vnet_flow_enable(vnm, ntohl(mp->flow_index), ntohl(mp->hw_if_index));
277 REPLY_MACRO (VL_API_FLOW_ENABLE_REPLY);
281 vl_api_flow_disable_t_handler (vl_api_flow_disable_t * mp)
283 vl_api_flow_add_reply_t *rmp;
286 vnet_main_t *vnm = vnet_get_main();
287 rv = vnet_flow_disable(vnm, ntohl(mp->flow_index), ntohl(mp->hw_if_index));
289 REPLY_MACRO (VL_API_FLOW_DISABLE_REPLY);
292 #define vl_msg_name_crc_list
293 #include <vnet/flow/flow.api.h>
294 #undef vl_msg_name_crc_list
298 * Add vpe's API message handlers to the table.
299 * vlib has already mapped shared memory and
300 * added the client registration handlers.
301 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
306 setup_message_id_table (api_main_t * am)
308 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
309 foreach_vl_msg_name_crc_flow;
313 static clib_error_t *
314 hw_flow_api_hookup (vlib_main_t * vm)
316 api_main_t *am = vlibapi_get_main ();
319 vl_msg_api_set_handlers(VL_API_##N, #n, \
320 vl_api_##n##_t_handler, \
322 vl_api_##n##_t_endian, \
323 vl_api_##n##_t_print, \
324 sizeof(vl_api_##n##_t), 1);
329 * Set up the (msg_name, crc, message-id) table
331 setup_message_id_table (am);
336 VLIB_API_INIT_FUNCTION (hw_flow_api_hookup);
339 * fd.io coding-style-patch-verification: ON
342 * eval: (c-set-style "gnu")