2 * Copyright (c) 2018 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <vnet/ip/ip.h>
18 #include <vnet/ethernet/ethernet.h>
19 #include <vnet/flow/flow.h>
21 vnet_flow_main_t flow_main;
24 vnet_flow_get_range (vnet_main_t * vnm, char *owner, u32 count, u32 * start)
26 vnet_flow_main_t *fm = &flow_main;
30 if (fm->flows_used == 0)
33 *start = fm->flows_used;
34 fm->flows_used += count;
35 vec_add2 (fm->ranges, r, 1);
38 r->owner = format (0, "%s%c", owner, 0);
43 vnet_flow_add (vnet_main_t * vnm, vnet_flow_t * flow, u32 * flow_index)
45 vnet_flow_main_t *fm = &flow_main;
48 pool_get (fm->global_flow_pool, f);
49 *flow_index = f - fm->global_flow_pool;
50 clib_memcpy (f, flow, sizeof (vnet_flow_t));
52 f->index = *flow_index;
57 vnet_get_flow (u32 flow_index)
59 vnet_flow_main_t *fm = &flow_main;
60 if (pool_is_free_index (fm->global_flow_pool, flow_index))
63 return pool_elt_at_index (fm->global_flow_pool, flow_index);
67 vnet_flow_del (vnet_main_t * vnm, u32 flow_index)
69 vnet_flow_main_t *fm = &flow_main;
70 vnet_flow_t *f = vnet_get_flow (flow_index);
75 return VNET_FLOW_ERROR_NO_SUCH_ENTRY;
78 hash_foreach (hw_if_index, private_data, f->private_data,
80 vnet_flow_disable (vnm, flow_index, hw_if_index);
84 hash_free (f->private_data);
85 memset (f, 0, sizeof (*f));
86 pool_put (fm->global_flow_pool, f);
91 vnet_flow_enable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index)
93 vnet_flow_t *f = vnet_get_flow (flow_index);
94 vnet_hw_interface_t *hi;
95 vnet_device_class_t *dev_class;
99 if (!vnet_hw_interface_is_valid (vnm, hw_if_index))
100 return VNET_FLOW_ERROR_NO_SUCH_INTERFACE;
102 /* don't enable flow twice */
103 if (hash_get (f->private_data, hw_if_index) != 0)
104 return VNET_FLOW_ERROR_ALREADY_DONE;
106 hi = vnet_get_hw_interface (vnm, hw_if_index);
107 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
109 if (dev_class->flow_ops_function == 0)
110 return VNET_FLOW_ERROR_NOT_SUPPORTED;
112 if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
114 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
115 f->redirect_device_input_next_index =
116 vlib_node_add_next (vnm->vlib_main, hw->input_node_index,
117 f->redirect_node_index);
120 rv = dev_class->flow_ops_function (vnm, VNET_FLOW_DEV_OP_ADD_FLOW,
121 hi->dev_instance, flow_index,
127 hash_set (f->private_data, hw_if_index, private_data);
132 vnet_flow_disable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index)
134 vnet_flow_t *f = vnet_get_flow (flow_index);
135 vnet_hw_interface_t *hi;
136 vnet_device_class_t *dev_class;
140 if (!vnet_hw_interface_is_valid (vnm, hw_if_index))
141 return VNET_FLOW_ERROR_NO_SUCH_INTERFACE;
143 /* don't disable if not enabled */
144 if ((p = hash_get (f->private_data, hw_if_index)) == 0)
145 return VNET_FLOW_ERROR_ALREADY_DONE;
147 hi = vnet_get_hw_interface (vnm, hw_if_index);
148 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
150 rv = dev_class->flow_ops_function (vnm, VNET_FLOW_DEV_OP_DEL_FLOW,
151 hi->dev_instance, flow_index, p);
156 hash_unset (f->private_data, hw_if_index);
161 * fd.io coding-style-patch-verification: ON
164 * eval: (c-set-style "gnu")