Line data Source code
1 : /*
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:
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 : #include <vnet/vnet.h>
17 : #include <vnet/ip/ip.h>
18 : #include <vnet/ethernet/ethernet.h>
19 : #include <vnet/flow/flow.h>
20 :
21 : vnet_flow_main_t flow_main;
22 :
23 : int
24 1118 : vnet_flow_get_range (vnet_main_t * vnm, char *owner, u32 count, u32 * start)
25 : {
26 1118 : vnet_flow_main_t *fm = &flow_main;
27 : vnet_flow_range_t *r;
28 :
29 : /* skip 0 */
30 1118 : if (fm->flows_used == 0)
31 559 : fm->flows_used = 1;
32 :
33 1118 : *start = fm->flows_used;
34 1118 : fm->flows_used += count;
35 1118 : vec_add2 (fm->ranges, r, 1);
36 1118 : r->start = *start;
37 1118 : r->count = count;
38 1118 : r->owner = format (0, "%s%c", owner, 0);
39 1118 : return 0;
40 : }
41 :
42 : int
43 0 : vnet_flow_add (vnet_main_t * vnm, vnet_flow_t * flow, u32 * flow_index)
44 : {
45 0 : vnet_flow_main_t *fm = &flow_main;
46 : vnet_flow_t *f;
47 :
48 0 : pool_get (fm->global_flow_pool, f);
49 0 : *flow_index = f - fm->global_flow_pool;
50 0 : clib_memcpy_fast (f, flow, sizeof (vnet_flow_t));
51 0 : f->private_data = 0;
52 0 : f->index = *flow_index;
53 0 : return 0;
54 : }
55 :
56 : vnet_flow_t *
57 0 : vnet_get_flow (u32 flow_index)
58 : {
59 0 : vnet_flow_main_t *fm = &flow_main;
60 0 : if (pool_is_free_index (fm->global_flow_pool, flow_index))
61 0 : return 0;
62 :
63 0 : return pool_elt_at_index (fm->global_flow_pool, flow_index);
64 : }
65 :
66 : int
67 0 : vnet_flow_del (vnet_main_t * vnm, u32 flow_index)
68 : {
69 0 : vnet_flow_main_t *fm = &flow_main;
70 0 : vnet_flow_t *f = vnet_get_flow (flow_index);
71 : uword hw_if_index;
72 : uword private_data;
73 :
74 0 : if (f == 0)
75 0 : return VNET_FLOW_ERROR_NO_SUCH_ENTRY;
76 :
77 : /* *INDENT-OFF* */
78 0 : hash_foreach (hw_if_index, private_data, f->private_data,
79 : ({
80 : vnet_flow_disable (vnm, flow_index, hw_if_index);
81 : }));
82 : /* *INDENT-ON* */
83 :
84 0 : hash_free (f->private_data);
85 0 : clib_memset (f, 0, sizeof (*f));
86 0 : pool_put (fm->global_flow_pool, f);
87 0 : return 0;
88 : }
89 :
90 : int
91 0 : vnet_flow_enable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index)
92 : {
93 0 : vnet_flow_t *f = vnet_get_flow (flow_index);
94 : vnet_hw_interface_t *hi;
95 : vnet_device_class_t *dev_class;
96 : uword private_data;
97 : int rv;
98 :
99 0 : if (f == 0)
100 0 : return VNET_FLOW_ERROR_NO_SUCH_ENTRY;
101 :
102 0 : if (!vnet_hw_interface_is_valid (vnm, hw_if_index))
103 0 : return VNET_FLOW_ERROR_NO_SUCH_INTERFACE;
104 :
105 : /* don't enable flow twice */
106 0 : if (hash_get (f->private_data, hw_if_index) != 0)
107 0 : return VNET_FLOW_ERROR_ALREADY_DONE;
108 :
109 0 : hi = vnet_get_hw_interface (vnm, hw_if_index);
110 0 : dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
111 :
112 0 : if (dev_class->flow_ops_function == 0)
113 0 : return VNET_FLOW_ERROR_NOT_SUPPORTED;
114 :
115 0 : if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
116 : {
117 0 : vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
118 0 : f->redirect_device_input_next_index =
119 0 : vlib_node_add_next (vnm->vlib_main, hw->input_node_index,
120 0 : f->redirect_node_index);
121 : }
122 :
123 0 : rv = dev_class->flow_ops_function (vnm, VNET_FLOW_DEV_OP_ADD_FLOW,
124 : hi->dev_instance, flow_index,
125 : &private_data);
126 :
127 0 : if (rv)
128 0 : return rv;
129 :
130 0 : hash_set (f->private_data, hw_if_index, private_data);
131 0 : return 0;
132 : }
133 :
134 : int
135 0 : vnet_flow_disable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index)
136 : {
137 0 : vnet_flow_t *f = vnet_get_flow (flow_index);
138 : vnet_hw_interface_t *hi;
139 : vnet_device_class_t *dev_class;
140 : uword *p;
141 : int rv;
142 :
143 0 : if (f == 0)
144 0 : return VNET_FLOW_ERROR_NO_SUCH_ENTRY;
145 :
146 0 : if (!vnet_hw_interface_is_valid (vnm, hw_if_index))
147 0 : return VNET_FLOW_ERROR_NO_SUCH_INTERFACE;
148 :
149 : /* don't disable if not enabled */
150 0 : if ((p = hash_get (f->private_data, hw_if_index)) == 0)
151 0 : return VNET_FLOW_ERROR_ALREADY_DONE;
152 :
153 0 : hi = vnet_get_hw_interface (vnm, hw_if_index);
154 0 : dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
155 :
156 0 : rv = dev_class->flow_ops_function (vnm, VNET_FLOW_DEV_OP_DEL_FLOW,
157 : hi->dev_instance, flow_index, p);
158 :
159 0 : if (rv)
160 0 : return rv;
161 :
162 0 : hash_unset (f->private_data, hw_if_index);
163 0 : return 0;
164 : }
165 :
166 : /*
167 : * fd.io coding-style-patch-verification: ON
168 : *
169 : * Local Variables:
170 : * eval: (c-set-style "gnu")
171 : * End:
172 : */
|