interface: rx queue infra rework, part one
[vpp.git] / src / vnet / devices / devices.h
1 /*
2  * Copyright (c) 2016 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 #ifndef included_vnet_vnet_device_h
17 #define included_vnet_vnet_device_h
18
19 #include <vppinfra/pcap.h>
20 #include <vnet/l3_types.h>
21
22 typedef enum
23 {
24   VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT,
25   VNET_DEVICE_INPUT_NEXT_IP4_INPUT,
26   VNET_DEVICE_INPUT_NEXT_IP6_INPUT,
27   VNET_DEVICE_INPUT_NEXT_MPLS_INPUT,
28   VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT,
29   VNET_DEVICE_INPUT_NEXT_DROP,
30   VNET_DEVICE_INPUT_N_NEXT_NODES,
31 } vnet_device_input_next_t;
32
33 #define VNET_DEVICE_INPUT_NEXT_NODES {                                  \
34     [VNET_DEVICE_INPUT_NEXT_DROP] = "error-drop",                       \
35     [VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT] = "ethernet-input",         \
36     [VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT] = "ip4-input-no-checksum",   \
37     [VNET_DEVICE_INPUT_NEXT_IP4_INPUT] = "ip4-input",                   \
38     [VNET_DEVICE_INPUT_NEXT_IP6_INPUT] = "ip6-input",                   \
39     [VNET_DEVICE_INPUT_NEXT_MPLS_INPUT] = "mpls-input",                 \
40 }
41
42 typedef struct
43 {
44   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
45
46   /* total input packet counter */
47   u64 aggregate_rx_packets;
48 } vnet_device_per_worker_data_t;
49
50 typedef struct
51 {
52   vnet_device_per_worker_data_t *workers;
53   uword first_worker_thread_index;
54   uword last_worker_thread_index;
55   uword next_worker_thread_index;
56 } vnet_device_main_t;
57
58 typedef struct
59 {
60   u32 hw_if_index;
61   u32 dev_instance;
62   u16 queue_id;
63   vnet_hw_if_rx_mode mode;
64   u32 interrupt_pending;
65 } vnet_device_and_queue_t;
66
67 typedef struct
68 {
69   vnet_device_and_queue_t *devices_and_queues;
70   vlib_node_state_t enabled_node_state;
71   u32 pad;
72 } vnet_device_input_runtime_t;
73
74 extern vnet_device_main_t vnet_device_main;
75 extern vlib_node_registration_t device_input_node;
76 extern const u32 device_input_next_node_advance[];
77 extern const u32 device_input_next_node_flags[];
78
79 static inline void
80 vnet_hw_interface_set_input_node (vnet_main_t * vnm, u32 hw_if_index,
81                                   u32 node_index)
82 {
83   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
84   hw->input_node_index = node_index;
85 }
86
87 void vnet_hw_interface_assign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
88                                          u16 queue_id, uword thread_index);
89 int vnet_hw_interface_unassign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
90                                           u16 queue_id);
91 int vnet_hw_interface_set_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
92                                    u16 queue_id, vnet_hw_if_rx_mode mode);
93 int vnet_hw_interface_get_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
94                                    u16 queue_id, vnet_hw_if_rx_mode * mode);
95
96 static inline u64
97 vnet_get_aggregate_rx_packets (void)
98 {
99   vnet_device_main_t *vdm = &vnet_device_main;
100   u64 sum = 0;
101   vnet_device_per_worker_data_t *pwd;
102
103   vec_foreach (pwd, vdm->workers) sum += pwd->aggregate_rx_packets;
104
105   return sum;
106 }
107
108 static inline void
109 vnet_device_increment_rx_packets (u32 thread_index, u64 count)
110 {
111   vnet_device_main_t *vdm = &vnet_device_main;
112   vnet_device_per_worker_data_t *pwd;
113
114   pwd = vec_elt_at_index (vdm->workers, thread_index);
115   pwd->aggregate_rx_packets += count;
116 }
117
118 static_always_inline uword
119 vnet_get_device_input_thread_index (vnet_main_t * vnm, u32 hw_if_index,
120                                     u16 queue_id)
121 {
122   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
123   ASSERT (queue_id < vec_len (hw->input_node_thread_index_by_queue));
124   return hw->input_node_thread_index_by_queue[queue_id];
125 }
126
127 static_always_inline void
128 vnet_device_input_set_interrupt_pending (vnet_main_t * vnm, u32 hw_if_index,
129                                          u16 queue_id)
130 {
131   vlib_main_t *vm;
132   vnet_hw_interface_t *hw;
133   vnet_device_input_runtime_t *rt;
134   vnet_device_and_queue_t *dq;
135   uword idx;
136
137   hw = vnet_get_hw_interface (vnm, hw_if_index);
138   idx = vnet_get_device_input_thread_index (vnm, hw_if_index, queue_id);
139   vm = vlib_mains[idx];
140   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
141   idx = hw->dq_runtime_index_by_queue[queue_id];
142   dq = vec_elt_at_index (rt->devices_and_queues, idx);
143
144   clib_atomic_store_rel_n (&(dq->interrupt_pending), 1);
145
146   vlib_node_set_interrupt_pending (vm, hw->input_node_index);
147 }
148
149 /*
150  * Acquire RMW Access
151  * Paired with Release Store in vnet_device_input_set_interrupt_pending
152  */
153 #define foreach_device_and_queue(var,vec)                       \
154   for (var = (vec); var < vec_end (vec); var++)                 \
155     if ((var->mode == VNET_HW_IF_RX_MODE_POLLING)               \
156         || clib_atomic_swap_acq_n (&((var)->interrupt_pending), 0))
157
158 #endif /* included_vnet_vnet_device_h */
159
160 /*
161  * fd.io coding-style-patch-verification: ON
162  *
163  * Local Variables:
164  * eval: (c-set-style "gnu")
165  * End:
166  */