FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
rx_queue.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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>
19 #include <vlib/unix/unix.h>
20 
21 VLIB_REGISTER_LOG_CLASS (if_rxq_log, static) = {
22  .class_name = "interface",
23  .subclass_name = "rx-queue",
24 };
25 
26 #define log_debug(fmt, ...) vlib_log_debug (if_rxq_log.class, fmt, __VA_ARGS__)
27 #define log_err(fmt, ...) vlib_log_err (if_rxq_log.class, fmt, __VA_ARGS__)
28 
29 static u32
31 {
33  if (vdm->first_worker_thread_index == 0)
34  return 0;
35 
36  if (thread_index != 0 && (thread_index < vdm->first_worker_thread_index ||
38  {
42  }
43 
44  return thread_index;
45 }
46 
47 static u64
48 rx_queue_key (u32 hw_if_index, u32 queue_id)
49 {
50  return ((u64) hw_if_index << 32) | queue_id;
51 }
52 
53 u32
55  u32 queue_id)
56 {
58  u64 key = rx_queue_key (hw_if_index, queue_id);
60  return p ? p[0] : ~0;
61 }
62 
63 u32
64 vnet_hw_if_register_rx_queue (vnet_main_t *vnm, u32 hw_if_index, u32 queue_id,
66 {
68  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
70  u64 key = rx_queue_key (hw_if_index, queue_id);
71  u32 queue_index;
72 
74  clib_panic ("Trying to register already registered queue id (%u) in the "
75  "interface %v\n",
76  queue_id, hi->name);
77 
79 
81  queue_index = rxq - im->hw_if_rx_queues;
82  vec_add1 (hi->rx_queue_indices, queue_index);
84  queue_index);
85  rxq->hw_if_index = hw_if_index;
86  rxq->dev_instance = hi->dev_instance;
87  rxq->queue_id = queue_id;
90  rxq->file_index = ~0;
91 
92  log_debug ("register: interface %v queue-id %u thread %u", hi->name,
93  queue_id, thread_index);
94 
95  return queue_index;
96 }
97 
98 void
100 {
103  rxq = vnet_hw_if_get_rx_queue (vnm, queue_index);
105  u64 key;
106 
107  key = rx_queue_key (rxq->hw_if_index, rxq->queue_id);
109 
110  for (int i = 0; i < vec_len (hi->rx_queue_indices); i++)
111  if (hi->rx_queue_indices[i] == queue_index)
112  {
113  vec_del1 (hi->rx_queue_indices, i);
114  break;
115  }
116 
117  log_debug ("unregister: interface %v queue-id %u", hi->name, rxq->queue_id);
118  pool_put_index (im->hw_if_rx_queues, queue_index);
119 }
120 
121 void
123 {
124  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
127  u64 key;
128 
129  log_debug ("unregister_all: interface %v", hi->name);
130 
131  for (int i = 0; i < vec_len (hi->rx_queue_indices); i++)
132  {
133  rxq = vnet_hw_if_get_rx_queue (vnm, hi->rx_queue_indices[i]);
134  key = rx_queue_key (rxq->hw_if_index, rxq->queue_id);
136 
137  pool_put_index (im->hw_if_rx_queues, hi->rx_queue_indices[i]);
138  }
139 
140  vec_free (hi->rx_queue_indices);
141 }
142 
143 void
145  u32 file_index)
146 {
147  vnet_hw_if_rx_queue_t *rxq = vnet_hw_if_get_rx_queue (vnm, queue_index);
149 
150  rxq->file_index = file_index;
152  log_debug ("set_file_index: interface %v queue-id %u file-index %u",
153  hi->name, rxq->queue_id, file_index);
154 }
155 
156 void
158 {
160  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
161  hi->input_node_index = node_index;
162  log_debug ("set_input_node: node %U for interface %v", format_vlib_node_name,
163  vm, node_index, hi->name);
164 }
165 
166 int
169 {
170  vnet_hw_if_rx_queue_t *rxq = vnet_hw_if_get_rx_queue (vnm, queue_index);
172  vnet_device_class_t *dc = vnet_get_device_class (vnm, hi->dev_class_index);
173 
175 
177  mode = hi->default_rx_mode;
178 
179  if (rxq->mode == mode)
180  {
181  log_debug ("set_rx_queue_mode: interface %v queue-id %u mode "
182  "unchanged (%U)",
183  hi->name, rxq->queue_id, format_vnet_hw_if_rx_mode, mode);
184  return 0;
185  }
186 
187  if (dc->rx_mode_change_function)
188  {
189  clib_error_t *err = dc->rx_mode_change_function (vnm, rxq->hw_if_index,
190  rxq->queue_id, mode);
191  if (err)
192  {
193  log_err ("setting rx mode on the interface %v queue-id %u failed.\n"
194  " %U",
195  hi->name, rxq->queue_id, format_clib_error, err);
196  clib_error_free (err);
197  return VNET_API_ERROR_UNSUPPORTED;
198  }
199  }
200 
201  rxq->mode = mode;
202  log_debug ("set_rx_queue_mode: interface %v queue-id %u mode set to %U",
203  hi->name, rxq->queue_id, format_vnet_hw_if_rx_mode, mode);
204  return 0;
205 }
206 
209 {
210  vnet_hw_if_rx_queue_t *rxq = vnet_hw_if_get_rx_queue (vnm, queue_index);
211  return rxq->mode;
212 }
213 
214 void
217 {
218  vnet_hw_if_rx_queue_t *rxq = vnet_hw_if_get_rx_queue (vnm, queue_index);
220 
221  rxq->thread_index = thread_index;
222 
223  if (rxq->file_index != ~0)
225 
226  log_debug ("set_rx_queue_thread_index: interface %v queue-id %u "
227  "thread-index set to %u",
228  hi->name, rxq->queue_id, thread_index);
229 }
230 
231 void
234 {
235  vnet_hw_if_rx_node_runtime_t *rt = (void *) node->runtime_data;
236  vnet_main_t *vnm = vnet_get_main ();
237  int int_num = -1;
238 
239  ASSERT (node->state == VLIB_NODE_STATE_INTERRUPT);
240 
241  vec_reset_length (rt->rxq_poll_vector);
242 
243  while ((int_num = clib_interrupt_get_next (rt->rxq_interrupts, int_num)) !=
244  -1)
245  {
246  vnet_hw_if_rx_queue_t *rxq = vnet_hw_if_get_rx_queue (vnm, int_num);
248 
249  clib_interrupt_clear (rt->rxq_interrupts, int_num);
250 
251  vec_add2 (rt->rxq_poll_vector, pv, 1);
252  pv->dev_instance = rxq->dev_instance;
253  pv->queue_id = rxq->queue_id;
254  }
255 }
256 
257 /*
258  * fd.io coding-style-patch-verification: ON
259  *
260  * Local Variables:
261  * eval: (c-set-style "gnu")
262  * End:
263  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
vnet_device_main_t::last_worker_thread_index
uword last_worker_thread_index
Definition: devices.h:64
vnet_device_main
vnet_device_main_t vnet_device_main
Definition: devices.c:22
im
vnet_interface_main_t * im
Definition: interface_output.c:395
file_main
clib_file_main_t file_main
Definition: main.c:63
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
vnet_hw_if_rx_queue_t::file_index
u32 file_index
Definition: interface.h:591
clib_file_set_polling_thread
static void clib_file_set_polling_thread(clib_file_main_t *um, uword index, u32 thread_index)
Definition: file.h:127
vnet_device_class_t
struct _vnet_device_class vnet_device_class_t
vnet_hw_if_rxq_poll_vector_t::queue_id
u32 queue_id
Definition: interface.h:749
vnet_hw_if_rx_queue_t::mode
vnet_hw_if_rx_mode mode
Definition: interface.h:597
vnet_hw_if_set_rx_queue_mode
int vnet_hw_if_set_rx_queue_mode(vnet_main_t *vnm, u32 queue_index, vnet_hw_if_rx_mode mode)
Definition: rx_queue.c:167
vnet_hw_if_get_rx_queue
static_always_inline vnet_hw_if_rx_queue_t * vnet_hw_if_get_rx_queue(vnet_main_t *vnm, u32 queue_index)
Definition: rx_queue_funcs.h:42
vnet_hw_if_rx_queue_t::thread_index
u32 thread_index
Definition: interface.h:588
clib_interrupt_get_next
static_always_inline int clib_interrupt_get_next(void *in, int last)
Definition: interrupt.h:104
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
vnet_interface_main_t
Definition: interface.h:989
vnet_hw_if_rx_queue_t::dev_instance
u32 dev_instance
Definition: interface.h:585
mode
vl_api_tunnel_mode_t mode
Definition: gre.api:48
VNET_HW_IF_RX_MODE_POLLING
@ VNET_HW_IF_RX_MODE_POLLING
Definition: interface.h:56
vnet_hw_if_unregister_all_rx_queues
void vnet_hw_if_unregister_all_rx_queues(vnet_main_t *vnm, u32 hw_if_index)
Definition: rx_queue.c:122
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
node_index
node node_index
Definition: interface_output.c:420
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
vnet_get_device_class
static vnet_device_class_t * vnet_get_device_class(vnet_main_t *vnm, u32 dev_class_index)
Definition: interface_funcs.h:124
vnet_interface_main_t::hw_if_rx_queues
vnet_hw_if_rx_queue_t * hw_if_rx_queues
Definition: interface.h:995
pool_put_index
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:337
key
typedef key
Definition: ipsec_types.api:88
format_clib_error
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
VLIB_REGISTER_LOG_CLASS
VLIB_REGISTER_LOG_CLASS(if_rxq_log, static)
vnet_hw_if_rx_queue_t::queue_id
u32 queue_id
Definition: interface.h:594
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
format_vnet_hw_if_rx_mode
format_function_t format_vnet_hw_if_rx_mode
Definition: interface_funcs.h:450
vec_add2
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:644
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vnet_hw_if_get_rx_queue_index_by_id
u32 vnet_hw_if_get_rx_queue_index_by_id(vnet_main_t *vnm, u32 hw_if_index, u32 queue_id)
Definition: rx_queue.c:54
clib_interrupt_clear
static_always_inline void clib_interrupt_clear(void *in, int int_num)
Definition: interrupt.h:90
vnet_get_hw_interface
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:44
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
rx_queue_funcs.h
vnet_hw_if_rx_mode
vnet_hw_if_rx_mode
Definition: interface.h:53
rx_queue_key
static u64 rx_queue_key(u32 hw_if_index, u32 queue_id)
Definition: rx_queue.c:48
uword
u64 uword
Definition: types.h:112
hash_unset_mem_free
static void hash_unset_mem_free(uword **h, const void *key)
Definition: hash.h:295
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
next_thread_index
static u32 next_thread_index(vnet_main_t *vnm, u32 thread_index)
Definition: rx_queue.c:30
hash_set_mem_alloc
static void hash_set_mem_alloc(uword **h, const void *key, uword v)
Definition: hash.h:279
VNET_HW_IF_RX_MODE_DEFAULT
@ VNET_HW_IF_RX_MODE_DEFAULT
Definition: interface.h:59
vnet_hw_if_set_rx_queue_file_index
void vnet_hw_if_set_rx_queue_file_index(vnet_main_t *vnm, u32 queue_index, u32 file_index)
Definition: rx_queue.c:144
vnet_device_main_t::first_worker_thread_index
uword first_worker_thread_index
Definition: devices.h:63
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
log_err
#define log_err(fmt,...)
Definition: rx_queue.c:27
vnet_hw_if_rx_node_runtime_t
Definition: interface.h:752
format_vlib_node_name
format_function_t format_vlib_node_name
Definition: node_funcs.h:1235
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
u64
unsigned long u64
Definition: types.h:89
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
vnet_hw_if_get_rx_queue_mode
vnet_hw_if_rx_mode vnet_hw_if_get_rx_queue_mode(vnet_main_t *vnm, u32 queue_index)
Definition: rx_queue.c:208
log_debug
#define log_debug(fmt,...)
Definition: rx_queue.c:26
vnet_hw_if_unregister_rx_queue
void vnet_hw_if_unregister_rx_queue(vnet_main_t *vnm, u32 queue_index)
Definition: rx_queue.c:99
vlib_main_t
Definition: main.h:102
vnet_hw_if_register_rx_queue
u32 vnet_hw_if_register_rx_queue(vnet_main_t *vnm, u32 hw_if_index, u32 queue_id, u32 thread_index)
Definition: rx_queue.c:64
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
pool_get_zero
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
Definition: pool.h:258
clib_error_t
Definition: clib_error.h:21
unix.h
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:399
vnet_hw_if_rx_queue_t::hw_if_index
u32 hw_if_index
Definition: interface.h:582
vnet_hw_if_rx_queue_t
Definition: interface.h:579
devices.h
clib_error_free
#define clib_error_free(e)
Definition: error.h:86
vnet_hw_if_set_rx_queue_thread_index
void vnet_hw_if_set_rx_queue_thread_index(vnet_main_t *vnm, u32 queue_index, u32 thread_index)
Definition: rx_queue.c:215
vnet_interface_main_t::rxq_index_by_hw_if_index_and_queue_id
uword * rxq_index_by_hw_if_index_and_queue_id
Definition: interface.h:996
vnet.h
vlib_node_runtime_t
Definition: node.h:454
clib_panic
#define clib_panic(format, args...)
Definition: error.h:72
vnet_device_main_t::next_worker_thread_index
uword next_worker_thread_index
Definition: devices.h:65
vnet_device_main_t
Definition: devices.h:60
vnet_hw_if_rxq_poll_vector_t::dev_instance
u32 dev_instance
Definition: interface.h:748
vec_del1
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:896
vnet_main_t::interface_main
vnet_interface_main_t interface_main
Definition: vnet.h:81
vnet_hw_if_set_input_node
void vnet_hw_if_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: rx_queue.c:157
VNET_HW_IF_RX_MODE_UNKNOWN
@ VNET_HW_IF_RX_MODE_UNKNOWN
Definition: interface.h:55
vnet_hw_if_rxq_poll_vector_t
Definition: interface.h:746
vnet_hw_if_generate_rxq_int_poll_vector
void vnet_hw_if_generate_rxq_int_poll_vector(vlib_main_t *vm, vlib_node_runtime_t *node)
Definition: rx_queue.c:232