blob: e3311c436322f76b542daaf23dd96ffa9b6508d3 [file] [log] [blame]
Damjan Marion22311502016-10-28 20:30:15 +02001/*
2 * Copyright (c) 2015 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
Damjan Marion7dc41462016-11-15 19:47:58 +010016#include <vnet/vnet.h>
Damjan Marion51327ac2016-11-09 11:59:42 +010017#include <vnet/devices/devices.h>
Damjan Marion22311502016-10-28 20:30:15 +020018#include <vnet/feature/feature.h>
Damjan Marion7dc41462016-11-15 19:47:58 +010019#include <vnet/ip/ip.h>
20#include <vnet/ethernet/ethernet.h>
Damjan Marion22311502016-10-28 20:30:15 +020021
Damjan Marionb3bb1012017-02-28 21:55:28 +010022vnet_device_main_t vnet_device_main;
23
Damjan Marion51327ac2016-11-09 11:59:42 +010024static uword
25device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
26 vlib_frame_t * frame)
27{
28 return 0;
29}
30
Damjan Marion22311502016-10-28 20:30:15 +020031/* *INDENT-OFF* */
Damjan Marion51327ac2016-11-09 11:59:42 +010032VLIB_REGISTER_NODE (device_input_node) = {
33 .function = device_input_fn,
34 .name = "device-input",
Damjan Marioneb743fa2017-03-20 16:34:15 +010035 .runtime_data_bytes = sizeof (vnet_device_input_runtime_t),
Damjan Marion51327ac2016-11-09 11:59:42 +010036 .type = VLIB_NODE_TYPE_INPUT,
37 .state = VLIB_NODE_STATE_DISABLED,
38 .n_next_nodes = VNET_DEVICE_INPUT_N_NEXT_NODES,
39 .next_nodes = VNET_DEVICE_INPUT_NEXT_NODES,
40};
41
Damjan Marion7dc41462016-11-15 19:47:58 +010042/* Table defines how much we need to advance current data pointer
43 in the buffer if we shortcut to l3 nodes */
44
45const u32 __attribute__((aligned (CLIB_CACHE_LINE_BYTES)))
46device_input_next_node_advance[((VNET_DEVICE_INPUT_N_NEXT_NODES /
47 CLIB_CACHE_LINE_BYTES) +1) * CLIB_CACHE_LINE_BYTES] =
48{
49 [VNET_DEVICE_INPUT_NEXT_IP4_INPUT] = sizeof (ethernet_header_t),
Sergio Gonzalez Monroy767b2f52016-12-20 15:20:32 +000050 [VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT] = sizeof (ethernet_header_t),
Damjan Marion7dc41462016-11-15 19:47:58 +010051 [VNET_DEVICE_INPUT_NEXT_IP6_INPUT] = sizeof (ethernet_header_t),
52 [VNET_DEVICE_INPUT_NEXT_MPLS_INPUT] = sizeof (ethernet_header_t),
53};
54
Dave Barach9f6186e2016-11-08 12:12:12 -050055VNET_FEATURE_ARC_INIT (device_input, static) =
56{
Damjan Marion22311502016-10-28 20:30:15 +020057 .arc_name = "device-input",
Damjan Marion51327ac2016-11-09 11:59:42 +010058 .start_nodes = VNET_FEATURES ("device-input"),
Damjan Marion8b3191e2016-11-09 19:54:20 +010059 .arc_index_ptr = &feature_main.device_input_feature_arc_index,
Damjan Marion22311502016-10-28 20:30:15 +020060};
61
62VNET_FEATURE_INIT (l2_patch, static) = {
63 .arc_name = "device-input",
64 .node_name = "l2-patch",
65 .runs_before = VNET_FEATURES ("ethernet-input"),
66};
67
68VNET_FEATURE_INIT (worker_handoff, static) = {
69 .arc_name = "device-input",
70 .node_name = "worker-handoff",
71 .runs_before = VNET_FEATURES ("ethernet-input"),
72};
73
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010074VNET_FEATURE_INIT (span_input, static) = {
75 .arc_name = "device-input",
76 .node_name = "span-input",
77 .runs_before = VNET_FEATURES ("ethernet-input"),
78};
79
Damjan Marion22311502016-10-28 20:30:15 +020080VNET_FEATURE_INIT (ethernet_input, static) = {
81 .arc_name = "device-input",
82 .node_name = "ethernet-input",
83 .runs_before = 0, /* not before any other features */
84};
85/* *INDENT-ON* */
86
Damjan Marioneb743fa2017-03-20 16:34:15 +010087static int
88vnet_device_queue_sort (void *a1, void *a2)
89{
90 vnet_device_and_queue_t *dq1 = a1;
91 vnet_device_and_queue_t *dq2 = a2;
92
93 if (dq1->dev_instance > dq2->dev_instance)
94 return 1;
95 else if (dq1->dev_instance < dq2->dev_instance)
96 return -1;
97 else if (dq1->queue_id > dq2->queue_id)
98 return 1;
99 else if (dq1->queue_id < dq2->queue_id)
100 return -1;
101 else
102 return 0;
103}
104
Damjan Marion153646e2017-04-05 18:15:45 +0200105static void
106vnet_device_queue_update (vnet_main_t * vnm, vnet_device_input_runtime_t * rt)
107{
108 vnet_device_and_queue_t *dq;
109 vnet_hw_interface_t *hw;
110
111 vec_sort_with_function (rt->devices_and_queues, vnet_device_queue_sort);
112
113 vec_foreach (dq, rt->devices_and_queues)
114 {
115 hw = vnet_get_hw_interface (vnm, dq->hw_if_index);
116 vec_validate (hw->dq_runtime_index_by_queue, dq->queue_id);
117 hw->dq_runtime_index_by_queue[dq->queue_id] = dq - rt->devices_and_queues;
118 }
119}
120
Damjan Marioneb743fa2017-03-20 16:34:15 +0100121void
Damjan Marion44036902017-04-28 12:29:15 +0200122vnet_hw_interface_assign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
123 u16 queue_id, uword thread_index)
Damjan Marioneb743fa2017-03-20 16:34:15 +0100124{
Damjan Marioneb743fa2017-03-20 16:34:15 +0100125 vnet_device_main_t *vdm = &vnet_device_main;
Damjan Marion6f9ac652017-06-15 19:01:31 +0200126 vlib_main_t *vm, *vm0;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100127 vnet_device_input_runtime_t *rt;
128 vnet_device_and_queue_t *dq;
129 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
130
131 ASSERT (hw->input_node_index > 0);
132
Damjan Marion586afd72017-04-05 19:18:20 +0200133 if (vdm->first_worker_thread_index == 0)
134 thread_index = 0;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100135
Damjan Marion586afd72017-04-05 19:18:20 +0200136 if (thread_index != 0 &&
137 (thread_index < vdm->first_worker_thread_index ||
138 thread_index > vdm->last_worker_thread_index))
Damjan Marioneb743fa2017-03-20 16:34:15 +0100139 {
Damjan Marion586afd72017-04-05 19:18:20 +0200140 thread_index = vdm->next_worker_thread_index++;
141 if (vdm->next_worker_thread_index > vdm->last_worker_thread_index)
142 vdm->next_worker_thread_index = vdm->first_worker_thread_index;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100143 }
144
Damjan Marion586afd72017-04-05 19:18:20 +0200145 vm = vlib_mains[thread_index];
Damjan Marion6f9ac652017-06-15 19:01:31 +0200146 vm0 = vlib_get_main ();
147
148 vlib_worker_thread_barrier_sync (vm0);
149
Damjan Marioneb743fa2017-03-20 16:34:15 +0100150 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
151
152 vec_add2 (rt->devices_and_queues, dq, 1);
153 dq->hw_if_index = hw_if_index;
154 dq->dev_instance = hw->dev_instance;
155 dq->queue_id = queue_id;
Damjan Marion44036902017-04-28 12:29:15 +0200156 dq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
Stevenf3b53642017-05-01 14:03:02 -0700157 rt->enabled_node_state = VLIB_NODE_STATE_POLLING;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100158
Damjan Marion153646e2017-04-05 18:15:45 +0200159 vnet_device_queue_update (vnm, rt);
Damjan Marion586afd72017-04-05 19:18:20 +0200160 vec_validate (hw->input_node_thread_index_by_queue, queue_id);
Damjan Marion44036902017-04-28 12:29:15 +0200161 vec_validate (hw->rx_mode_by_queue, queue_id);
Damjan Marion586afd72017-04-05 19:18:20 +0200162 hw->input_node_thread_index_by_queue[queue_id] = thread_index;
Damjan Marion44036902017-04-28 12:29:15 +0200163 hw->rx_mode_by_queue[queue_id] = VNET_HW_INTERFACE_RX_MODE_POLLING;
Damjan Marion6f9ac652017-06-15 19:01:31 +0200164
165 vlib_worker_thread_barrier_release (vm0);
166
Damjan Marion153646e2017-04-05 18:15:45 +0200167 vlib_node_set_state (vm, hw->input_node_index, rt->enabled_node_state);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100168}
169
Damjan Marion153646e2017-04-05 18:15:45 +0200170int
Damjan Marion44036902017-04-28 12:29:15 +0200171vnet_hw_interface_unassign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
172 u16 queue_id)
Damjan Marioneb743fa2017-03-20 16:34:15 +0100173{
Damjan Marion6f9ac652017-06-15 19:01:31 +0200174 vlib_main_t *vm, *vm0;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100175 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
176 vnet_device_input_runtime_t *rt;
177 vnet_device_and_queue_t *dq;
Damjan Marion586afd72017-04-05 19:18:20 +0200178 uword old_thread_index;
Stevenf3b53642017-05-01 14:03:02 -0700179 vnet_hw_interface_rx_mode mode;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100180
Damjan Marion586afd72017-04-05 19:18:20 +0200181 if (hw->input_node_thread_index_by_queue == 0)
Damjan Marioneb743fa2017-03-20 16:34:15 +0100182 return VNET_API_ERROR_INVALID_INTERFACE;
183
Damjan Marion586afd72017-04-05 19:18:20 +0200184 if (vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1)
Damjan Marioneb743fa2017-03-20 16:34:15 +0100185 return VNET_API_ERROR_INVALID_INTERFACE;
186
Damjan Marion586afd72017-04-05 19:18:20 +0200187 old_thread_index = hw->input_node_thread_index_by_queue[queue_id];
Damjan Marioneb743fa2017-03-20 16:34:15 +0100188
Damjan Marion153646e2017-04-05 18:15:45 +0200189 vm = vlib_mains[old_thread_index];
190
191 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100192
193 vec_foreach (dq, rt->devices_and_queues)
194 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
195 {
Stevenf3b53642017-05-01 14:03:02 -0700196 mode = dq->mode;
Damjan Marion6f9ac652017-06-15 19:01:31 +0200197 goto delete;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100198 }
199
200 return VNET_API_ERROR_INVALID_INTERFACE;
201
Damjan Marion6f9ac652017-06-15 19:01:31 +0200202delete:
Damjan Marion153646e2017-04-05 18:15:45 +0200203
Damjan Marion6f9ac652017-06-15 19:01:31 +0200204 vm0 = vlib_get_main ();
205 vlib_worker_thread_barrier_sync (vm0);
206 vec_del1 (rt->devices_and_queues, dq - rt->devices_and_queues);
Damjan Marion153646e2017-04-05 18:15:45 +0200207 vnet_device_queue_update (vnm, rt);
Damjan Marion44036902017-04-28 12:29:15 +0200208 hw->rx_mode_by_queue[queue_id] = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
Damjan Marion6f9ac652017-06-15 19:01:31 +0200209 vlib_worker_thread_barrier_release (vm0);
Damjan Marion153646e2017-04-05 18:15:45 +0200210
211 if (vec_len (rt->devices_and_queues) == 0)
212 vlib_node_set_state (vm, hw->input_node_index, VLIB_NODE_STATE_DISABLED);
Stevenf3b53642017-05-01 14:03:02 -0700213 else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
214 {
215 /*
216 * if the deleted interface is polling, we may need to set the node state
217 * to interrupt if there is no more polling interface for this device's
218 * corresponding thread. This is because mixed interfaces
219 * (polling and interrupt), assigned to the same thread, set the
220 * thread to polling prior to the deletion.
221 */
222 vec_foreach (dq, rt->devices_and_queues)
223 {
224 if (dq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
225 return 0;
226 }
227 rt->enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
228 vlib_node_set_state (vm, hw->input_node_index, rt->enabled_node_state);
229 }
Damjan Marioneb743fa2017-03-20 16:34:15 +0100230
231 return 0;
232}
233
Damjan Marion153646e2017-04-05 18:15:45 +0200234
235int
Damjan Marion44036902017-04-28 12:29:15 +0200236vnet_hw_interface_set_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
237 u16 queue_id, vnet_hw_interface_rx_mode mode)
Damjan Marion153646e2017-04-05 18:15:45 +0200238{
239 vlib_main_t *vm;
240 uword thread_index;
241 vnet_device_and_queue_t *dq;
242 vlib_node_state_t enabled_node_state;
Damjan Marion44036902017-04-28 12:29:15 +0200243 ASSERT (mode < VNET_HW_INTERFACE_NUM_RX_MODES);
Damjan Marion153646e2017-04-05 18:15:45 +0200244 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
245 vnet_device_input_runtime_t *rt;
246 int is_polling = 0;
247
Damjan Marion44036902017-04-28 12:29:15 +0200248 if (hw->input_node_thread_index_by_queue == 0 || hw->rx_mode_by_queue == 0)
Damjan Marion153646e2017-04-05 18:15:45 +0200249 return VNET_API_ERROR_INVALID_INTERFACE;
250
Damjan Marion44036902017-04-28 12:29:15 +0200251 if (hw->rx_mode_by_queue[queue_id] == mode)
252 return 0;
253
254 if (mode != VNET_HW_INTERFACE_RX_MODE_POLLING &&
255 (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE) == 0)
256 return VNET_API_ERROR_UNSUPPORTED;
257
Stevene3a395c2017-05-09 16:19:50 -0700258 hw->rx_mode_by_queue[queue_id] = mode;
Damjan Marion153646e2017-04-05 18:15:45 +0200259 thread_index = hw->input_node_thread_index_by_queue[queue_id];
260 vm = vlib_mains[thread_index];
261
262 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
263
264 vec_foreach (dq, rt->devices_and_queues)
265 {
266 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
267 dq->mode = mode;
Damjan Marion44036902017-04-28 12:29:15 +0200268 if (dq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
Damjan Marion153646e2017-04-05 18:15:45 +0200269 is_polling = 1;
270 }
271
272 if (is_polling)
273 enabled_node_state = VLIB_NODE_STATE_POLLING;
274 else
275 enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
276
277 if (rt->enabled_node_state != enabled_node_state)
278 {
279 rt->enabled_node_state = enabled_node_state;
280 if (vlib_node_get_state (vm, hw->input_node_index) !=
281 VLIB_NODE_STATE_DISABLED)
282 vlib_node_set_state (vm, hw->input_node_index, enabled_node_state);
283 }
284
285 return 0;
286}
287
288int
Damjan Marion44036902017-04-28 12:29:15 +0200289vnet_hw_interface_get_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
290 u16 queue_id, vnet_hw_interface_rx_mode * mode)
Damjan Marion153646e2017-04-05 18:15:45 +0200291{
292 vlib_main_t *vm;
293 uword thread_index;
294 vnet_device_and_queue_t *dq;
295 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
296 vnet_device_input_runtime_t *rt;
297
298 if (hw->input_node_thread_index_by_queue == 0)
299 return VNET_API_ERROR_INVALID_INTERFACE;
300
301 thread_index = hw->input_node_thread_index_by_queue[queue_id];
302 vm = vlib_mains[thread_index];
303
304 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
305
306 vec_foreach (dq, rt->devices_and_queues)
307 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
308 {
309 *mode = dq->mode;
310 return 0;
311 }
312
313 return VNET_API_ERROR_INVALID_INTERFACE;
314}
315
Damjan Marioneb743fa2017-03-20 16:34:15 +0100316
Damjan Marioneb743fa2017-03-20 16:34:15 +0100317
Damjan Marionb3bb1012017-02-28 21:55:28 +0100318static clib_error_t *
319vnet_device_init (vlib_main_t * vm)
320{
321 vnet_device_main_t *vdm = &vnet_device_main;
322 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marioneb743fa2017-03-20 16:34:15 +0100323 vlib_thread_registration_t *tr;
324 uword *p;
Damjan Marionb3bb1012017-02-28 21:55:28 +0100325
326 vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1,
327 CLIB_CACHE_LINE_BYTES);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100328
329 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
330 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
331 if (tr && tr->count > 0)
332 {
Damjan Marion586afd72017-04-05 19:18:20 +0200333 vdm->first_worker_thread_index = tr->first_index;
334 vdm->next_worker_thread_index = tr->first_index;
335 vdm->last_worker_thread_index = tr->first_index + tr->count - 1;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100336 }
Damjan Marionb3bb1012017-02-28 21:55:28 +0100337 return 0;
338}
339
340VLIB_INIT_FUNCTION (vnet_device_init);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100341
Damjan Marion22311502016-10-28 20:30:15 +0200342/*
343 * fd.io coding-style-patch-verification: ON
344 *
345 * Local Variables:
346 * eval: (c-set-style "gnu")
347 * End:
348 */