f64c6e0dea556e923b32dfd1b730676103c91ecd
[vpp.git] / src / vnet / devices / devices.c
1 /*
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
16 #include <vnet/vnet.h>
17 #include <vnet/devices/devices.h>
18 #include <vnet/feature/feature.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ethernet/ethernet.h>
21
22 vnet_device_main_t vnet_device_main;
23
24 static uword
25 device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
26                  vlib_frame_t * frame)
27 {
28   return 0;
29 }
30
31 /* *INDENT-OFF* */
32 VLIB_REGISTER_NODE (device_input_node) = {
33   .function = device_input_fn,
34   .name = "device-input",
35   .runtime_data_bytes = sizeof (vnet_device_input_runtime_t),
36   .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
42 /* Table defines how much we need to advance current data pointer
43    in the buffer if we shortcut to l3 nodes */
44
45 const u32 __attribute__((aligned (CLIB_CACHE_LINE_BYTES)))
46 device_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),
50       [VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT] = sizeof (ethernet_header_t),
51       [VNET_DEVICE_INPUT_NEXT_IP6_INPUT] = sizeof (ethernet_header_t),
52       [VNET_DEVICE_INPUT_NEXT_MPLS_INPUT] = sizeof (ethernet_header_t),
53 };
54
55 VNET_FEATURE_ARC_INIT (device_input, static) =
56 {
57   .arc_name  = "device-input",
58   .start_nodes = VNET_FEATURES ("device-input"),
59   .arc_index_ptr = &feature_main.device_input_feature_arc_index,
60 };
61
62 VNET_FEATURE_INIT (l2_patch, static) = {
63   .arc_name = "device-input",
64   .node_name = "l2-patch",
65   .runs_before = VNET_FEATURES ("ethernet-input"),
66 };
67
68 VNET_FEATURE_INIT (worker_handoff, static) = {
69   .arc_name = "device-input",
70   .node_name = "worker-handoff",
71   .runs_before = VNET_FEATURES ("ethernet-input"),
72 };
73
74 VNET_FEATURE_INIT (span_input, static) = {
75   .arc_name = "device-input",
76   .node_name = "span-input",
77   .runs_before = VNET_FEATURES ("ethernet-input"),
78 };
79
80 VNET_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
87 static int
88 vnet_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
105 static void
106 vnet_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
121 void
122 vnet_hw_interface_assign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
123                                     u16 queue_id, uword thread_index)
124 {
125   vnet_device_main_t *vdm = &vnet_device_main;
126   vlib_main_t *vm, *vm0;
127   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
133   if (vdm->first_worker_thread_index == 0)
134     thread_index = 0;
135
136   if (thread_index != 0 &&
137       (thread_index < vdm->first_worker_thread_index ||
138        thread_index > vdm->last_worker_thread_index))
139     {
140       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;
143     }
144
145   vm = vlib_mains[thread_index];
146   vm0 = vlib_get_main ();
147
148   vlib_worker_thread_barrier_sync (vm0);
149
150   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;
156   dq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
157   rt->enabled_node_state = VLIB_NODE_STATE_POLLING;
158
159   vnet_device_queue_update (vnm, rt);
160   vec_validate (hw->input_node_thread_index_by_queue, queue_id);
161   vec_validate (hw->rx_mode_by_queue, queue_id);
162   hw->input_node_thread_index_by_queue[queue_id] = thread_index;
163   hw->rx_mode_by_queue[queue_id] = VNET_HW_INTERFACE_RX_MODE_POLLING;
164
165   vlib_worker_thread_barrier_release (vm0);
166
167   vlib_node_set_state (vm, hw->input_node_index, rt->enabled_node_state);
168 }
169
170 int
171 vnet_hw_interface_unassign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
172                                       u16 queue_id)
173 {
174   vlib_main_t *vm, *vm0;
175   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;
178   uword old_thread_index;
179   vnet_hw_interface_rx_mode mode;
180
181   if (hw->input_node_thread_index_by_queue == 0)
182     return VNET_API_ERROR_INVALID_INTERFACE;
183
184   if (vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1)
185     return VNET_API_ERROR_INVALID_INTERFACE;
186
187   old_thread_index = hw->input_node_thread_index_by_queue[queue_id];
188
189   vm = vlib_mains[old_thread_index];
190
191   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
192
193   vec_foreach (dq, rt->devices_and_queues)
194     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
195     {
196       mode = dq->mode;
197       goto delete;
198     }
199
200   return VNET_API_ERROR_INVALID_INTERFACE;
201
202 delete:
203
204   vm0 = vlib_get_main ();
205   vlib_worker_thread_barrier_sync (vm0);
206   vec_del1 (rt->devices_and_queues, dq - rt->devices_and_queues);
207   vnet_device_queue_update (vnm, rt);
208   hw->rx_mode_by_queue[queue_id] = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
209   vlib_worker_thread_barrier_release (vm0);
210
211   if (vec_len (rt->devices_and_queues) == 0)
212     vlib_node_set_state (vm, hw->input_node_index, VLIB_NODE_STATE_DISABLED);
213   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     }
230
231   return 0;
232 }
233
234
235 int
236 vnet_hw_interface_set_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
237                                u16 queue_id, vnet_hw_interface_rx_mode mode)
238 {
239   vlib_main_t *vm;
240   uword thread_index;
241   vnet_device_and_queue_t *dq;
242   vlib_node_state_t enabled_node_state;
243   ASSERT (mode < VNET_HW_INTERFACE_NUM_RX_MODES);
244   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
248   if (mode == VNET_HW_INTERFACE_RX_MODE_DEFAULT)
249     mode = hw->default_rx_mode;
250
251   if (hw->input_node_thread_index_by_queue == 0 || hw->rx_mode_by_queue == 0)
252     return VNET_API_ERROR_INVALID_INTERFACE;
253
254   if (hw->rx_mode_by_queue[queue_id] == mode)
255     return 0;
256
257   if (mode != VNET_HW_INTERFACE_RX_MODE_POLLING &&
258       (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE) == 0)
259     return VNET_API_ERROR_UNSUPPORTED;
260
261   hw->rx_mode_by_queue[queue_id] = mode;
262   thread_index = hw->input_node_thread_index_by_queue[queue_id];
263   vm = vlib_mains[thread_index];
264
265   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
266
267   vec_foreach (dq, rt->devices_and_queues)
268   {
269     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
270       dq->mode = mode;
271     if (dq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
272       is_polling = 1;
273   }
274
275   if (is_polling)
276     enabled_node_state = VLIB_NODE_STATE_POLLING;
277   else
278     enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
279
280   if (rt->enabled_node_state != enabled_node_state)
281     {
282       rt->enabled_node_state = enabled_node_state;
283       if (vlib_node_get_state (vm, hw->input_node_index) !=
284           VLIB_NODE_STATE_DISABLED)
285         vlib_node_set_state (vm, hw->input_node_index, enabled_node_state);
286     }
287
288   return 0;
289 }
290
291 int
292 vnet_hw_interface_get_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
293                                u16 queue_id, vnet_hw_interface_rx_mode * mode)
294 {
295   vlib_main_t *vm;
296   uword thread_index;
297   vnet_device_and_queue_t *dq;
298   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
299   vnet_device_input_runtime_t *rt;
300
301   if (hw->input_node_thread_index_by_queue == 0)
302     return VNET_API_ERROR_INVALID_INTERFACE;
303
304   thread_index = hw->input_node_thread_index_by_queue[queue_id];
305   vm = vlib_mains[thread_index];
306
307   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
308
309   vec_foreach (dq, rt->devices_and_queues)
310     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
311     {
312       *mode = dq->mode;
313       return 0;
314     }
315
316   return VNET_API_ERROR_INVALID_INTERFACE;
317 }
318
319
320
321 static clib_error_t *
322 vnet_device_init (vlib_main_t * vm)
323 {
324   vnet_device_main_t *vdm = &vnet_device_main;
325   vlib_thread_main_t *tm = vlib_get_thread_main ();
326   vlib_thread_registration_t *tr;
327   uword *p;
328
329   vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1,
330                         CLIB_CACHE_LINE_BYTES);
331
332   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
333   tr = p ? (vlib_thread_registration_t *) p[0] : 0;
334   if (tr && tr->count > 0)
335     {
336       vdm->first_worker_thread_index = tr->first_index;
337       vdm->next_worker_thread_index = tr->first_index;
338       vdm->last_worker_thread_index = tr->first_index + tr->count - 1;
339     }
340   return 0;
341 }
342
343 VLIB_INIT_FUNCTION (vnet_device_init);
344
345 /*
346  * fd.io coding-style-patch-verification: ON
347  *
348  * Local Variables:
349  * eval: (c-set-style "gnu")
350  * End:
351  */