1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
6 #include <vnet/devices/devices.h>
7 #include <vnet/interface/tx_queue_funcs.h>
8 #include <vlib/unix/unix.h>
10 VLIB_REGISTER_LOG_CLASS (if_txq_log, static) = {
11 .class_name = "interface",
12 .subclass_name = "tx-queue",
15 #define log_debug(fmt, ...) vlib_log_debug (if_txq_log.class, fmt, __VA_ARGS__)
16 #define log_err(fmt, ...) vlib_log_err (if_txq_log.class, fmt, __VA_ARGS__)
19 tx_queue_key (u32 hw_if_index, u32 queue_id)
21 return ((u64) hw_if_index << 32) | queue_id;
25 vnet_hw_if_get_tx_queue_index_by_id (vnet_main_t *vnm, u32 hw_if_index,
28 vnet_interface_main_t *im = &vnm->interface_main;
29 u64 key = tx_queue_key (hw_if_index, queue_id);
30 uword *p = hash_get_mem (im->txq_index_by_hw_if_index_and_queue_id, &key);
35 vnet_hw_if_register_tx_queue (vnet_main_t *vnm, u32 hw_if_index, u32 queue_id)
37 vnet_interface_main_t *im = &vnm->interface_main;
38 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
39 vnet_hw_if_tx_queue_t *txq;
40 u64 key = tx_queue_key (hw_if_index, queue_id);
43 if (hash_get_mem (im->txq_index_by_hw_if_index_and_queue_id, &key))
44 clib_panic ("Trying to register already registered queue id (%u) in the "
48 pool_get_zero (im->hw_if_tx_queues, txq);
49 queue_index = txq - im->hw_if_tx_queues;
50 vec_add1 (hi->tx_queue_indices, queue_index);
51 hash_set_mem_alloc (&im->txq_index_by_hw_if_index_and_queue_id, &key,
53 txq->hw_if_index = hw_if_index;
54 txq->queue_id = queue_id;
56 log_debug ("register: interface %v queue-id %u", hi->name, queue_id);
62 vnet_hw_if_unregister_tx_queue (vnet_main_t *vnm, u32 queue_index)
64 vnet_interface_main_t *im = &vnm->interface_main;
65 vnet_hw_if_tx_queue_t *txq;
66 txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
67 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, txq->hw_if_index);
70 key = tx_queue_key (txq->hw_if_index, txq->queue_id);
71 hash_unset_mem_free (&im->txq_index_by_hw_if_index_and_queue_id, &key);
73 for (int i = 0; i < vec_len (hi->tx_queue_indices); i++)
74 if (hi->tx_queue_indices[i] == queue_index)
76 vec_del1 (hi->tx_queue_indices, i);
80 log_debug ("unregister: interface %v queue-id %u", hi->name, txq->queue_id);
81 clib_bitmap_free (txq->threads);
82 pool_put_index (im->hw_if_tx_queues, queue_index);
86 vnet_hw_if_unregister_all_tx_queues (vnet_main_t *vnm, u32 hw_if_index)
88 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
89 vnet_interface_main_t *im = &vnm->interface_main;
90 vnet_hw_if_tx_queue_t *txq;
93 log_debug ("unregister_all: interface %v", hi->name);
95 for (int i = 0; i < vec_len (hi->tx_queue_indices); i++)
97 txq = vnet_hw_if_get_tx_queue (vnm, hi->tx_queue_indices[i]);
98 key = tx_queue_key (txq->hw_if_index, txq->queue_id);
99 hash_unset_mem_free (&im->txq_index_by_hw_if_index_and_queue_id, &key);
101 clib_bitmap_free (txq->threads);
102 pool_put_index (im->hw_if_tx_queues, hi->tx_queue_indices[i]);
105 vec_free (hi->tx_queue_indices);
109 vnet_hw_if_tx_queue_assign_thread (vnet_main_t *vnm, u32 queue_index,
112 vnet_hw_if_tx_queue_t *txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
113 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, txq->hw_if_index);
114 txq->threads = clib_bitmap_set (txq->threads, thread_index, 1);
115 if (clib_bitmap_count_set_bits (txq->threads) > 1)
116 txq->shared_queue = 1;
118 "assign_thread: interface %v queue-id %u thread %u queue-shared %s",
119 hi->name, txq->queue_id, thread_index,
120 (txq->shared_queue == 1 ? "yes" : "no"));
124 vnet_hw_if_tx_queue_unassign_thread (vnet_main_t *vnm, u32 queue_index,
127 vnet_hw_if_tx_queue_t *txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
128 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, txq->hw_if_index);
129 txq->threads = clib_bitmap_set (txq->threads, thread_index, 0);
130 if (clib_bitmap_count_set_bits (txq->threads) < 2)
131 txq->shared_queue = 0;
133 "unassign_thread: interface %v queue-id %u thread %u queue-shared %s",
134 hi->name, txq->queue_id, thread_index,
135 (txq->shared_queue == 1 ? "yes" : "no"));