LCOV - code coverage report
Current view: top level - vnet/interface - runtime.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 143 174 82.2 %
Date: 2023-10-26 01:39:38 Functions: 3 3 100.0 %

          Line data    Source code
       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>
      18             : #include <vnet/feature/feature.h>
      19             : #include <vnet/ip/ip.h>
      20             : #include <vnet/ethernet/ethernet.h>
      21             : #include <vnet/interface/rx_queue_funcs.h>
      22             : #include <vnet/interface/tx_queue_funcs.h>
      23             : #include <vlib/unix/unix.h>
      24             : 
      25         575 : VLIB_REGISTER_LOG_CLASS (if_rxq_log, static) = {
      26             :   .class_name = "interface",
      27             :   .subclass_name = "runtime",
      28             : };
      29             : 
      30             : #define log_debug(fmt, ...) vlib_log_debug (if_rxq_log.class, fmt, __VA_ARGS__)
      31             : #define log_err(fmt, ...)   vlib_log_err (if_rxq_log.class, fmt, __VA_ARGS__)
      32             : 
      33             : static char *node_state_str[] = {
      34             :   [VLIB_NODE_STATE_DISABLED] = "disabled",
      35             :   [VLIB_NODE_STATE_POLLING] = "polling",
      36             :   [VLIB_NODE_STATE_INTERRUPT] = "interrupt",
      37             : };
      38             : 
      39             : static int
      40         684 : poll_data_sort (void *a1, void *a2)
      41             : {
      42         684 :   vnet_hw_if_rxq_poll_vector_t *pv1 = a1;
      43         684 :   vnet_hw_if_rxq_poll_vector_t *pv2 = a2;
      44             : 
      45         684 :   if (pv1->dev_instance > pv2->dev_instance)
      46         576 :     return 1;
      47         108 :   else if (pv1->dev_instance < pv2->dev_instance)
      48         108 :     return -1;
      49           0 :   else if (pv1->queue_id > pv2->queue_id)
      50           0 :     return 1;
      51           0 :   else if (pv1->queue_id < pv2->queue_id)
      52           0 :     return -1;
      53             :   else
      54           0 :     return 0;
      55             : }
      56             : 
      57             : void
      58       18806 : vnet_hw_if_update_runtime_data (vnet_main_t *vnm, u32 hw_if_index)
      59             : {
      60       18806 :   vlib_main_t *vm = vlib_get_main ();
      61       18806 :   vnet_interface_main_t *im = &vnm->interface_main;
      62       18806 :   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
      63       18806 :   u32 node_index = hi->input_node_index;
      64             :   vnet_hw_if_rx_queue_t *rxq;
      65       18806 :   vnet_hw_if_rxq_poll_vector_t *pv, **d = 0, **a = 0;
      66       18806 :   vnet_hw_if_output_node_runtime_t *new_out_runtimes = 0;
      67       18806 :   vlib_node_state_t *per_thread_node_state = 0;
      68       18806 :   u32 n_threads = vlib_get_n_threads ();
      69       18806 :   u16 *per_thread_node_adaptive = 0;
      70       18806 :   int something_changed_on_rx = 0;
      71       18806 :   int something_changed_on_tx = 0;
      72       18806 :   clib_bitmap_t *pending_int = 0;
      73       18806 :   int last_int = -1;
      74             : 
      75       18806 :   log_debug ("update node '%U' triggered by interface %v",
      76             :              format_vlib_node_name, vm, node_index, hi->name);
      77             : 
      78       18806 :   vec_validate (d, n_threads - 1);
      79       18806 :   vec_validate (a, n_threads - 1);
      80       38225 :   vec_validate_init_empty (per_thread_node_state, n_threads - 1,
      81             :                            VLIB_NODE_STATE_DISABLED);
      82       38225 :   vec_validate_init_empty (per_thread_node_adaptive, n_threads - 1, 0);
      83             : 
      84             :   /* find out desired node state on each thread */
      85       22689 :   pool_foreach (rxq, im->hw_if_rx_queues)
      86             :     {
      87        3883 :       u32 ti = rxq->thread_index;
      88             :       vnet_hw_interface_t *rxq_hi;
      89             : 
      90        3883 :       ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN);
      91        3883 :       ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT);
      92             : 
      93        3883 :       rxq_hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
      94             : 
      95        3883 :       if (rxq_hi->input_node_index != node_index)
      96         212 :         continue;
      97             : 
      98        3671 :       if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING)
      99             :         {
     100        2375 :           per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING;
     101        2375 :           per_thread_node_adaptive[ti] = 0;
     102             :         }
     103             : 
     104        3671 :       if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING)
     105        2375 :         continue;
     106             : 
     107        1296 :       if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
     108           0 :           rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
     109        1296 :         per_thread_node_state[ti] = VLIB_NODE_STATE_INTERRUPT;
     110             : 
     111        1296 :       if (rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
     112           0 :         per_thread_node_adaptive[ti] = 1;
     113             :     }
     114             : 
     115             :   /* construct per-thread polling vectors */
     116       22689 :   pool_foreach (rxq, im->hw_if_rx_queues)
     117             :     {
     118        3883 :       u32 ti = rxq->thread_index;
     119             :       vnet_hw_interface_t *rxq_hi;
     120             : 
     121        3883 :       rxq_hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
     122             : 
     123        3883 :       if (rxq_hi->input_node_index != node_index)
     124         212 :         continue;
     125             : 
     126        3671 :       if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
     127        2375 :           rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
     128        1296 :         last_int = clib_max (last_int, rxq - im->hw_if_rx_queues);
     129             : 
     130        3671 :       if (per_thread_node_adaptive[ti])
     131             :         {
     132           0 :           vec_add2_aligned (a[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
     133           0 :           pv->dev_instance = rxq->dev_instance;
     134           0 :           pv->queue_id = rxq->queue_id;
     135             :         }
     136             : 
     137        3671 :       if (per_thread_node_state[ti] != VLIB_NODE_STATE_POLLING)
     138        1296 :         continue;
     139             : 
     140        2375 :       vec_add2_aligned (d[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
     141        2375 :       pv->dev_instance = rxq->dev_instance;
     142        2375 :       pv->queue_id = rxq->queue_id;
     143             :     }
     144             : 
     145             :   /* sort poll vectors and compare them with active ones to avoid
     146             :    * unnecesary barrier */
     147       38225 :   for (int i = 0; i < n_threads; i++)
     148             :     {
     149       19419 :       vlib_main_t *ovm = vlib_get_main_by_index (i);
     150             :       vlib_node_state_t old_state;
     151       19419 :       vec_sort_with_function (d[i], poll_data_sort);
     152             : 
     153       19419 :       old_state = vlib_node_get_state (ovm, node_index);
     154       19419 :       if (per_thread_node_state[i] != old_state)
     155             :         {
     156        1275 :           something_changed_on_rx = 1;
     157        1275 :           log_debug ("state changed for node %U on thread %u from %s to %s",
     158             :                      format_vlib_node_name, vm, node_index, i,
     159             :                      node_state_str[old_state],
     160             :                      node_state_str[per_thread_node_state[i]]);
     161             :         }
     162             : 
     163             :       /* check if something changed */
     164       19419 :       if (something_changed_on_rx == 0)
     165             :         {
     166             :           vnet_hw_if_rx_node_runtime_t *rt;
     167       18144 :           rt = vlib_node_get_runtime_data (ovm, node_index);
     168       18144 :           if (vec_len (rt->rxq_vector_int) != vec_len (d[i]))
     169         548 :             something_changed_on_rx = 1;
     170       17596 :           else if (memcmp (d[i], rt->rxq_vector_int,
     171       17596 :                            vec_len (d[i]) * sizeof (**d)))
     172           0 :             something_changed_on_rx = 1;
     173       18144 :           if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1)
     174           2 :             something_changed_on_rx = 1;
     175             : 
     176       18144 :           if (something_changed_on_rx == 0 && per_thread_node_adaptive[i])
     177             :             {
     178           0 :               if (vec_len (rt->rxq_vector_poll) != vec_len (a[i]))
     179           0 :                 something_changed_on_rx = 1;
     180           0 :               else if (memcmp (a[i], rt->rxq_vector_poll,
     181           0 :                                vec_len (a[i]) * sizeof (**a)))
     182           0 :                 something_changed_on_rx = 1;
     183             :             }
     184             :         }
     185             :     }
     186             : 
     187       18806 :   if (vec_len (hi->tx_queue_indices) > 0)
     188             :     {
     189        1644 :       new_out_runtimes = vec_dup_aligned (hi->output_node_thread_runtimes,
     190             :                                           CLIB_CACHE_LINE_BYTES);
     191        1644 :       vec_validate_aligned (new_out_runtimes, n_threads - 1,
     192             :                             CLIB_CACHE_LINE_BYTES);
     193             : 
     194        3288 :       for (u32 i = 0; i < vec_len (new_out_runtimes); i++)
     195             :         {
     196             :           vnet_hw_if_output_node_runtime_t *rt;
     197        1644 :           rt = vec_elt_at_index (new_out_runtimes, i);
     198        1644 :           u32 n_queues = 0, total_queues = vec_len (hi->tx_queue_indices);
     199        1644 :           rt->frame = 0;
     200        1644 :           rt->lookup_table = 0;
     201             : 
     202        3288 :           for (u32 j = 0; j < total_queues; j++)
     203             :             {
     204        1644 :               u32 queue_index = hi->tx_queue_indices[j];
     205        1644 :               vnet_hw_if_tx_frame_t frame = { .shared_queue = 0,
     206             :                                               .hints = 7,
     207             :                                               .queue_id = ~0 };
     208             :               vnet_hw_if_tx_queue_t *txq =
     209        1644 :                 vnet_hw_if_get_tx_queue (vnm, queue_index);
     210        1644 :               if (!clib_bitmap_get (txq->threads, i))
     211           0 :                 continue;
     212             : 
     213        1644 :               log_debug ("tx queue data changed for interface %v, thread %u "
     214             :                          "(queue_id %u)",
     215             :                          hi->name, i, txq->queue_id);
     216        1644 :               something_changed_on_tx = 1;
     217             : 
     218        1644 :               frame.queue_id = txq->queue_id;
     219        1644 :               frame.shared_queue = txq->shared_queue;
     220        1644 :               vec_add1 (rt->frame, frame);
     221        1644 :               n_queues++;
     222             :             }
     223             : 
     224             :           // don't initialize rt->n_queues above
     225        1644 :           if (rt->n_queues != n_queues)
     226             :             {
     227         638 :               something_changed_on_tx = 1;
     228         638 :               rt->n_queues = n_queues;
     229             :             }
     230             :           /*
     231             :            * It is only used in case of multiple txq.
     232             :            */
     233        1644 :           if (rt->n_queues > 0)
     234             :             {
     235        1644 :               if (!is_pow2 (n_queues))
     236           0 :                 n_queues = max_pow2 (n_queues);
     237             : 
     238        1644 :               vec_validate_aligned (rt->lookup_table, n_queues - 1,
     239             :                                     CLIB_CACHE_LINE_BYTES);
     240             : 
     241        3288 :               for (u32 k = 0; k < vec_len (rt->lookup_table); k++)
     242             :                 {
     243        1644 :                   rt->lookup_table[k] = rt->frame[k % rt->n_queues].queue_id;
     244        1644 :                   log_debug ("tx queue lookup table changed for interface %v, "
     245             :                              "(lookup table [%u]=%u)",
     246             :                              hi->name, k, rt->lookup_table[k]);
     247             :                 }
     248             :             }
     249             :         }
     250             :     }
     251             :   else
     252             :     /* interface deleted */
     253       17162 :     something_changed_on_tx = 1;
     254             : 
     255       18806 :   if (something_changed_on_rx || something_changed_on_tx)
     256       18806 :     {
     257             :       int with_barrier;
     258             : 
     259       18806 :       if (vlib_worker_thread_barrier_held ())
     260             :         {
     261       18806 :           with_barrier = 0;
     262       18806 :           log_debug ("%s", "already running under the barrier");
     263             :         }
     264             :       else
     265           0 :         with_barrier = 1;
     266             : 
     267       18806 :       if (with_barrier)
     268           0 :         vlib_worker_thread_barrier_sync (vm);
     269             : 
     270       18806 :       if (something_changed_on_rx)
     271             :         {
     272        3614 :           for (int i = 0; i < n_threads; i++)
     273             :             {
     274        1825 :               vlib_main_t *vm = vlib_get_main_by_index (i);
     275             :               vnet_hw_if_rx_node_runtime_t *rt;
     276        1825 :               rt = vlib_node_get_runtime_data (vm, node_index);
     277        1825 :               pv = rt->rxq_vector_int;
     278        1825 :               rt->rxq_vector_int = d[i];
     279        1825 :               d[i] = pv;
     280             : 
     281        1825 :               if (per_thread_node_adaptive[i])
     282             :                 {
     283           0 :                   pv = rt->rxq_vector_poll;
     284           0 :                   rt->rxq_vector_poll = a[i];
     285           0 :                   a[i] = pv;
     286             :                 }
     287             : 
     288        1825 :               if (rt->rxq_interrupts)
     289             :                 {
     290         351 :                   void *in = rt->rxq_interrupts;
     291         351 :                   int int_num = -1;
     292         351 :                   while ((int_num = clib_interrupt_get_next (in, int_num)) !=
     293             :                          -1)
     294             :                     {
     295           0 :                       clib_interrupt_clear (in, int_num);
     296           0 :                       pending_int = clib_bitmap_set (pending_int, int_num, 1);
     297           0 :                       last_int = clib_max (last_int, int_num);
     298             :                     }
     299             :                 }
     300             : 
     301        1825 :               vlib_node_set_state (vm, node_index, per_thread_node_state[i]);
     302        1825 :               vlib_node_set_flag (vm, node_index, VLIB_NODE_FLAG_ADAPTIVE_MODE,
     303        1825 :                                   per_thread_node_adaptive[i]);
     304             : 
     305        1825 :               if (last_int >= 0)
     306         351 :                 clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1);
     307             :               else
     308        1474 :                 clib_interrupt_free (&rt->rxq_interrupts);
     309             :             }
     310             :         }
     311       18806 :       if (something_changed_on_tx)
     312             :         {
     313             :           vnet_hw_if_output_node_runtime_t *t;
     314       18806 :           t = hi->output_node_thread_runtimes;
     315       18806 :           hi->output_node_thread_runtimes = new_out_runtimes;
     316       18806 :           new_out_runtimes = t;
     317             :         }
     318             : 
     319       18806 :       if (with_barrier)
     320           0 :         vlib_worker_thread_barrier_release (vm);
     321             :     }
     322             :   else
     323           0 :     log_debug ("skipping update of node '%U', no changes detected",
     324             :                format_vlib_node_name, vm, node_index);
     325             : 
     326       18806 :   if (pending_int)
     327             :     {
     328             :       int i;
     329           0 :       clib_bitmap_foreach (i, pending_int)
     330             :         {
     331           0 :           vnet_hw_if_rx_queue_set_int_pending (vnm, i);
     332             :         }
     333           0 :       clib_bitmap_free (pending_int);
     334             :     }
     335             : 
     336       38225 :   for (int i = 0; i < n_threads; i++)
     337             :     {
     338       19419 :       vec_free (d[i]);
     339       19419 :       vec_free (a[i]);
     340       19419 :       if (new_out_runtimes)
     341             :         {
     342        1638 :           vec_free (new_out_runtimes[i].frame);
     343        1638 :           vec_free (new_out_runtimes[i].lookup_table);
     344             :         }
     345             :     }
     346             : 
     347       18806 :   vec_free (d);
     348       18806 :   vec_free (a);
     349       18806 :   vec_free (per_thread_node_state);
     350       18806 :   vec_free (per_thread_node_adaptive);
     351       18806 :   vec_free (new_out_runtimes);
     352       18806 : }

Generated by: LCOV version 1.14