blob: eda15356958b80667e9fd8ee216761cf38d8e58b [file] [log] [blame]
Damjan Mariona35cc142018-03-16 01:25:27 +01001/*
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
21vnet_flow_main_t flow_main;
22
23int
24vnet_flow_get_range (vnet_main_t * vnm, char *owner, u32 count, u32 * start)
25{
26 vnet_flow_main_t *fm = &flow_main;
27 vnet_flow_range_t *r;
28
29 /* skip 0 */
30 if (fm->flows_used == 0)
31 fm->flows_used = 1;
32
33 *start = fm->flows_used;
34 fm->flows_used += count;
35 vec_add2 (fm->ranges, r, 1);
36 r->start = *start;
37 r->count = count;
38 r->owner = format (0, "%s%c", owner, 0);
39 return 0;
40}
41
42int
43vnet_flow_add (vnet_main_t * vnm, vnet_flow_t * flow, u32 * flow_index)
44{
45 vnet_flow_main_t *fm = &flow_main;
46 vnet_flow_t *f;
47
48 pool_get (fm->global_flow_pool, f);
49 *flow_index = f - fm->global_flow_pool;
Dave Barach178cf492018-11-13 16:34:13 -050050 clib_memcpy_fast (f, flow, sizeof (vnet_flow_t));
Damjan Mariona35cc142018-03-16 01:25:27 +010051 f->private_data = 0;
52 f->index = *flow_index;
53 return 0;
54}
55
56vnet_flow_t *
57vnet_get_flow (u32 flow_index)
58{
59 vnet_flow_main_t *fm = &flow_main;
60 if (pool_is_free_index (fm->global_flow_pool, flow_index))
61 return 0;
62
63 return pool_elt_at_index (fm->global_flow_pool, flow_index);
64}
65
66int
67vnet_flow_del (vnet_main_t * vnm, u32 flow_index)
68{
69 vnet_flow_main_t *fm = &flow_main;
70 vnet_flow_t *f = vnet_get_flow (flow_index);
71 uword hw_if_index;
72 uword private_data;
73
74 if (f == 0)
75 return VNET_FLOW_ERROR_NO_SUCH_ENTRY;
76
Damjan Mariona35cc142018-03-16 01:25:27 +010077 hash_foreach (hw_if_index, private_data, f->private_data,
78 ({
79 vnet_flow_disable (vnm, flow_index, hw_if_index);
80 }));
Damjan Mariona35cc142018-03-16 01:25:27 +010081
82 hash_free (f->private_data);
Dave Barachb7b92992018-10-17 10:38:51 -040083 clib_memset (f, 0, sizeof (*f));
Damjan Mariona35cc142018-03-16 01:25:27 +010084 pool_put (fm->global_flow_pool, f);
85 return 0;
86}
87
88int
89vnet_flow_enable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index)
90{
91 vnet_flow_t *f = vnet_get_flow (flow_index);
92 vnet_hw_interface_t *hi;
93 vnet_device_class_t *dev_class;
94 uword private_data;
95 int rv;
96
Chenmin Sunbe2ad0b2019-11-22 05:33:40 +080097 if (f == 0)
98 return VNET_FLOW_ERROR_NO_SUCH_ENTRY;
99
Damjan Mariona35cc142018-03-16 01:25:27 +0100100 if (!vnet_hw_interface_is_valid (vnm, hw_if_index))
101 return VNET_FLOW_ERROR_NO_SUCH_INTERFACE;
102
103 /* don't enable flow twice */
104 if (hash_get (f->private_data, hw_if_index) != 0)
105 return VNET_FLOW_ERROR_ALREADY_DONE;
106
107 hi = vnet_get_hw_interface (vnm, hw_if_index);
108 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
109
110 if (dev_class->flow_ops_function == 0)
111 return VNET_FLOW_ERROR_NOT_SUPPORTED;
112
Eyal Bari4b11d032018-05-16 12:15:32 +0300113 if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
114 {
115 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
116 f->redirect_device_input_next_index =
117 vlib_node_add_next (vnm->vlib_main, hw->input_node_index,
118 f->redirect_node_index);
119 }
120
Damjan Mariona35cc142018-03-16 01:25:27 +0100121 rv = dev_class->flow_ops_function (vnm, VNET_FLOW_DEV_OP_ADD_FLOW,
122 hi->dev_instance, flow_index,
123 &private_data);
124
125 if (rv)
126 return rv;
127
128 hash_set (f->private_data, hw_if_index, private_data);
129 return 0;
130}
131
132int
133vnet_flow_disable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index)
134{
135 vnet_flow_t *f = vnet_get_flow (flow_index);
136 vnet_hw_interface_t *hi;
137 vnet_device_class_t *dev_class;
138 uword *p;
139 int rv;
140
Chenmin Sunbe2ad0b2019-11-22 05:33:40 +0800141 if (f == 0)
142 return VNET_FLOW_ERROR_NO_SUCH_ENTRY;
143
Damjan Mariona35cc142018-03-16 01:25:27 +0100144 if (!vnet_hw_interface_is_valid (vnm, hw_if_index))
145 return VNET_FLOW_ERROR_NO_SUCH_INTERFACE;
146
147 /* don't disable if not enabled */
148 if ((p = hash_get (f->private_data, hw_if_index)) == 0)
149 return VNET_FLOW_ERROR_ALREADY_DONE;
150
151 hi = vnet_get_hw_interface (vnm, hw_if_index);
152 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
153
154 rv = dev_class->flow_ops_function (vnm, VNET_FLOW_DEV_OP_DEL_FLOW,
155 hi->dev_instance, flow_index, p);
156
157 if (rv)
158 return rv;
159
160 hash_unset (f->private_data, hw_if_index);
161 return 0;
162}
163
164/*
165 * fd.io coding-style-patch-verification: ON
166 *
167 * Local Variables:
168 * eval: (c-set-style "gnu")
169 * End:
170 */