blob: a38ecd2d1bbbcb4f57e39b00fc6ecbeead76dec1 [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
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020080VNET_FEATURE_INIT (p2p_ethernet_node, static) = {
81 .arc_name = "device-input",
82 .node_name = "p2p-ethernet-input",
83 .runs_before = VNET_FEATURES ("ethernet-input"),
84};
85
Damjan Marion22311502016-10-28 20:30:15 +020086VNET_FEATURE_INIT (ethernet_input, static) = {
87 .arc_name = "device-input",
88 .node_name = "ethernet-input",
89 .runs_before = 0, /* not before any other features */
90};
91/* *INDENT-ON* */
92
Damjan Marioneb743fa2017-03-20 16:34:15 +010093static int
94vnet_device_queue_sort (void *a1, void *a2)
95{
96 vnet_device_and_queue_t *dq1 = a1;
97 vnet_device_and_queue_t *dq2 = a2;
98
99 if (dq1->dev_instance > dq2->dev_instance)
100 return 1;
101 else if (dq1->dev_instance < dq2->dev_instance)
102 return -1;
103 else if (dq1->queue_id > dq2->queue_id)
104 return 1;
105 else if (dq1->queue_id < dq2->queue_id)
106 return -1;
107 else
108 return 0;
109}
110
Damjan Marion153646e2017-04-05 18:15:45 +0200111static void
112vnet_device_queue_update (vnet_main_t * vnm, vnet_device_input_runtime_t * rt)
113{
114 vnet_device_and_queue_t *dq;
115 vnet_hw_interface_t *hw;
116
117 vec_sort_with_function (rt->devices_and_queues, vnet_device_queue_sort);
118
119 vec_foreach (dq, rt->devices_and_queues)
120 {
121 hw = vnet_get_hw_interface (vnm, dq->hw_if_index);
122 vec_validate (hw->dq_runtime_index_by_queue, dq->queue_id);
123 hw->dq_runtime_index_by_queue[dq->queue_id] = dq - rt->devices_and_queues;
124 }
125}
126
Damjan Marioneb743fa2017-03-20 16:34:15 +0100127void
Damjan Marion44036902017-04-28 12:29:15 +0200128vnet_hw_interface_assign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
129 u16 queue_id, uword thread_index)
Damjan Marioneb743fa2017-03-20 16:34:15 +0100130{
Damjan Marioneb743fa2017-03-20 16:34:15 +0100131 vnet_device_main_t *vdm = &vnet_device_main;
Damjan Marion6f9ac652017-06-15 19:01:31 +0200132 vlib_main_t *vm, *vm0;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100133 vnet_device_input_runtime_t *rt;
134 vnet_device_and_queue_t *dq;
135 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
136
137 ASSERT (hw->input_node_index > 0);
138
Damjan Marion586afd72017-04-05 19:18:20 +0200139 if (vdm->first_worker_thread_index == 0)
140 thread_index = 0;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100141
Damjan Marion586afd72017-04-05 19:18:20 +0200142 if (thread_index != 0 &&
143 (thread_index < vdm->first_worker_thread_index ||
144 thread_index > vdm->last_worker_thread_index))
Damjan Marioneb743fa2017-03-20 16:34:15 +0100145 {
Damjan Marion586afd72017-04-05 19:18:20 +0200146 thread_index = vdm->next_worker_thread_index++;
147 if (vdm->next_worker_thread_index > vdm->last_worker_thread_index)
148 vdm->next_worker_thread_index = vdm->first_worker_thread_index;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100149 }
150
Damjan Marion586afd72017-04-05 19:18:20 +0200151 vm = vlib_mains[thread_index];
Damjan Marion6f9ac652017-06-15 19:01:31 +0200152 vm0 = vlib_get_main ();
153
154 vlib_worker_thread_barrier_sync (vm0);
155
Damjan Marioneb743fa2017-03-20 16:34:15 +0100156 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
157
158 vec_add2 (rt->devices_and_queues, dq, 1);
159 dq->hw_if_index = hw_if_index;
160 dq->dev_instance = hw->dev_instance;
161 dq->queue_id = queue_id;
Damjan Marion44036902017-04-28 12:29:15 +0200162 dq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
Stevenf3b53642017-05-01 14:03:02 -0700163 rt->enabled_node_state = VLIB_NODE_STATE_POLLING;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100164
Damjan Marion153646e2017-04-05 18:15:45 +0200165 vnet_device_queue_update (vnm, rt);
Damjan Marion586afd72017-04-05 19:18:20 +0200166 vec_validate (hw->input_node_thread_index_by_queue, queue_id);
Damjan Marion44036902017-04-28 12:29:15 +0200167 vec_validate (hw->rx_mode_by_queue, queue_id);
Damjan Marion586afd72017-04-05 19:18:20 +0200168 hw->input_node_thread_index_by_queue[queue_id] = thread_index;
Damjan Marion44036902017-04-28 12:29:15 +0200169 hw->rx_mode_by_queue[queue_id] = VNET_HW_INTERFACE_RX_MODE_POLLING;
Damjan Marion6f9ac652017-06-15 19:01:31 +0200170
171 vlib_worker_thread_barrier_release (vm0);
172
Damjan Marion153646e2017-04-05 18:15:45 +0200173 vlib_node_set_state (vm, hw->input_node_index, rt->enabled_node_state);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100174}
175
Damjan Marion153646e2017-04-05 18:15:45 +0200176int
Damjan Marion44036902017-04-28 12:29:15 +0200177vnet_hw_interface_unassign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
178 u16 queue_id)
Damjan Marioneb743fa2017-03-20 16:34:15 +0100179{
Damjan Marion6f9ac652017-06-15 19:01:31 +0200180 vlib_main_t *vm, *vm0;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100181 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
182 vnet_device_input_runtime_t *rt;
183 vnet_device_and_queue_t *dq;
Damjan Marion586afd72017-04-05 19:18:20 +0200184 uword old_thread_index;
Stevenf3b53642017-05-01 14:03:02 -0700185 vnet_hw_interface_rx_mode mode;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100186
Damjan Marion586afd72017-04-05 19:18:20 +0200187 if (hw->input_node_thread_index_by_queue == 0)
Damjan Marioneb743fa2017-03-20 16:34:15 +0100188 return VNET_API_ERROR_INVALID_INTERFACE;
189
Damjan Marion586afd72017-04-05 19:18:20 +0200190 if (vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1)
Damjan Marioneb743fa2017-03-20 16:34:15 +0100191 return VNET_API_ERROR_INVALID_INTERFACE;
192
Damjan Marion586afd72017-04-05 19:18:20 +0200193 old_thread_index = hw->input_node_thread_index_by_queue[queue_id];
Damjan Marioneb743fa2017-03-20 16:34:15 +0100194
Damjan Marion153646e2017-04-05 18:15:45 +0200195 vm = vlib_mains[old_thread_index];
196
197 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100198
199 vec_foreach (dq, rt->devices_and_queues)
200 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
201 {
Stevenf3b53642017-05-01 14:03:02 -0700202 mode = dq->mode;
Damjan Marion6f9ac652017-06-15 19:01:31 +0200203 goto delete;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100204 }
205
206 return VNET_API_ERROR_INVALID_INTERFACE;
207
Damjan Marion6f9ac652017-06-15 19:01:31 +0200208delete:
Damjan Marion153646e2017-04-05 18:15:45 +0200209
Damjan Marion6f9ac652017-06-15 19:01:31 +0200210 vm0 = vlib_get_main ();
211 vlib_worker_thread_barrier_sync (vm0);
212 vec_del1 (rt->devices_and_queues, dq - rt->devices_and_queues);
Damjan Marion153646e2017-04-05 18:15:45 +0200213 vnet_device_queue_update (vnm, rt);
Damjan Marion44036902017-04-28 12:29:15 +0200214 hw->rx_mode_by_queue[queue_id] = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
Damjan Marion6f9ac652017-06-15 19:01:31 +0200215 vlib_worker_thread_barrier_release (vm0);
Damjan Marion153646e2017-04-05 18:15:45 +0200216
217 if (vec_len (rt->devices_and_queues) == 0)
218 vlib_node_set_state (vm, hw->input_node_index, VLIB_NODE_STATE_DISABLED);
Stevenf3b53642017-05-01 14:03:02 -0700219 else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
220 {
221 /*
222 * if the deleted interface is polling, we may need to set the node state
223 * to interrupt if there is no more polling interface for this device's
224 * corresponding thread. This is because mixed interfaces
225 * (polling and interrupt), assigned to the same thread, set the
226 * thread to polling prior to the deletion.
227 */
228 vec_foreach (dq, rt->devices_and_queues)
229 {
230 if (dq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
231 return 0;
232 }
233 rt->enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
234 vlib_node_set_state (vm, hw->input_node_index, rt->enabled_node_state);
235 }
Damjan Marioneb743fa2017-03-20 16:34:15 +0100236
237 return 0;
238}
239
Damjan Marion153646e2017-04-05 18:15:45 +0200240
241int
Damjan Marion44036902017-04-28 12:29:15 +0200242vnet_hw_interface_set_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
243 u16 queue_id, vnet_hw_interface_rx_mode mode)
Damjan Marion153646e2017-04-05 18:15:45 +0200244{
245 vlib_main_t *vm;
246 uword thread_index;
247 vnet_device_and_queue_t *dq;
248 vlib_node_state_t enabled_node_state;
Damjan Marion44036902017-04-28 12:29:15 +0200249 ASSERT (mode < VNET_HW_INTERFACE_NUM_RX_MODES);
Damjan Marion153646e2017-04-05 18:15:45 +0200250 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
251 vnet_device_input_runtime_t *rt;
252 int is_polling = 0;
253
Damjan Marion4e53a0d2017-06-21 14:29:44 +0200254 if (mode == VNET_HW_INTERFACE_RX_MODE_DEFAULT)
255 mode = hw->default_rx_mode;
256
Damjan Marion44036902017-04-28 12:29:15 +0200257 if (hw->input_node_thread_index_by_queue == 0 || hw->rx_mode_by_queue == 0)
Damjan Marion153646e2017-04-05 18:15:45 +0200258 return VNET_API_ERROR_INVALID_INTERFACE;
259
Damjan Marion44036902017-04-28 12:29:15 +0200260 if (hw->rx_mode_by_queue[queue_id] == mode)
261 return 0;
262
263 if (mode != VNET_HW_INTERFACE_RX_MODE_POLLING &&
264 (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE) == 0)
265 return VNET_API_ERROR_UNSUPPORTED;
266
Stevenbd8a6112017-07-30 10:29:26 -0700267 if ((vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1) ||
268 (vec_len (hw->rx_mode_by_queue) < queue_id + 1))
269 return VNET_API_ERROR_INVALID_QUEUE;
270
Stevene3a395c2017-05-09 16:19:50 -0700271 hw->rx_mode_by_queue[queue_id] = mode;
Damjan Marion153646e2017-04-05 18:15:45 +0200272 thread_index = hw->input_node_thread_index_by_queue[queue_id];
273 vm = vlib_mains[thread_index];
274
275 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
276
277 vec_foreach (dq, rt->devices_and_queues)
278 {
279 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
280 dq->mode = mode;
Damjan Marion44036902017-04-28 12:29:15 +0200281 if (dq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
Damjan Marion153646e2017-04-05 18:15:45 +0200282 is_polling = 1;
283 }
284
285 if (is_polling)
286 enabled_node_state = VLIB_NODE_STATE_POLLING;
287 else
288 enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
289
290 if (rt->enabled_node_state != enabled_node_state)
291 {
292 rt->enabled_node_state = enabled_node_state;
293 if (vlib_node_get_state (vm, hw->input_node_index) !=
294 VLIB_NODE_STATE_DISABLED)
295 vlib_node_set_state (vm, hw->input_node_index, enabled_node_state);
296 }
297
298 return 0;
299}
300
301int
Damjan Marion44036902017-04-28 12:29:15 +0200302vnet_hw_interface_get_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
303 u16 queue_id, vnet_hw_interface_rx_mode * mode)
Damjan Marion153646e2017-04-05 18:15:45 +0200304{
305 vlib_main_t *vm;
306 uword thread_index;
307 vnet_device_and_queue_t *dq;
308 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
309 vnet_device_input_runtime_t *rt;
310
311 if (hw->input_node_thread_index_by_queue == 0)
312 return VNET_API_ERROR_INVALID_INTERFACE;
313
Stevenbd8a6112017-07-30 10:29:26 -0700314 if ((vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1) ||
315 (vec_len (hw->rx_mode_by_queue) < queue_id + 1))
316 return VNET_API_ERROR_INVALID_QUEUE;
317
Damjan Marion153646e2017-04-05 18:15:45 +0200318 thread_index = hw->input_node_thread_index_by_queue[queue_id];
319 vm = vlib_mains[thread_index];
320
321 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
322
323 vec_foreach (dq, rt->devices_and_queues)
324 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
325 {
326 *mode = dq->mode;
327 return 0;
328 }
329
330 return VNET_API_ERROR_INVALID_INTERFACE;
331}
332
Damjan Marioneb743fa2017-03-20 16:34:15 +0100333
Damjan Marioneb743fa2017-03-20 16:34:15 +0100334
Damjan Marionb3bb1012017-02-28 21:55:28 +0100335static clib_error_t *
336vnet_device_init (vlib_main_t * vm)
337{
338 vnet_device_main_t *vdm = &vnet_device_main;
339 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marioneb743fa2017-03-20 16:34:15 +0100340 vlib_thread_registration_t *tr;
341 uword *p;
Damjan Marionb3bb1012017-02-28 21:55:28 +0100342
343 vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1,
344 CLIB_CACHE_LINE_BYTES);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100345
346 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
347 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
348 if (tr && tr->count > 0)
349 {
Damjan Marion586afd72017-04-05 19:18:20 +0200350 vdm->first_worker_thread_index = tr->first_index;
351 vdm->next_worker_thread_index = tr->first_index;
352 vdm->last_worker_thread_index = tr->first_index + tr->count - 1;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100353 }
Damjan Marionb3bb1012017-02-28 21:55:28 +0100354 return 0;
355}
356
357VLIB_INIT_FUNCTION (vnet_device_init);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100358
Damjan Marion22311502016-10-28 20:30:15 +0200359/*
360 * fd.io coding-style-patch-verification: ON
361 *
362 * Local Variables:
363 * eval: (c-set-style "gnu")
364 * End:
365 */