Retire support for DPDK 2.1.0 and older
[vpp.git] / vnet / vnet / devices / dpdk / dpdk_priv.h
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 #define DPDK_NB_RX_DESC_DEFAULT   512
17 #define DPDK_NB_TX_DESC_DEFAULT   512
18 #define DPDK_NB_RX_DESC_VIRTIO    256
19 #define DPDK_NB_TX_DESC_VIRTIO    256
20 #define DPDK_NB_RX_DESC_10GE    1024
21 #define DPDK_NB_TX_DESC_10GE    1024
22 #define DPDK_NB_RX_DESC_40GE    1024
23 #define DPDK_NB_TX_DESC_40GE    1024
24 #define DPDK_NB_RX_DESC_ENIC    1024
25
26 /* These args appear by themselves */
27 #define foreach_eal_double_hyphen_predicate_arg \
28 _(no-shconf)                                    \
29 _(no-hpet)                                      \
30 _(no-pci)                                       \
31 _(no-huge)                                      \
32 _(vmware-tsc-map)                               \
33 _(virtio-vhost)
34
35 #define foreach_eal_single_hyphen_mandatory_arg \
36 _(coremask, c)                                  \
37 _(nchannels, n)                                 \
38
39 #define foreach_eal_single_hyphen_arg           \
40 _(blacklist, b)                                 \
41 _(mem-alloc-request, m)                         \
42 _(force-ranks, r)
43
44 /* These args are preceeded by "--" and followed by a single string */
45 #define foreach_eal_double_hyphen_arg           \
46 _(huge-dir)                                     \
47 _(proc-type)                                    \
48 _(file-prefix)                                  \
49 _(vdev)
50
51 static inline u32
52 dpdk_rx_burst ( dpdk_main_t * dm, dpdk_device_t * xd, u16 queue_id)
53 {
54   u32 n_buffers;
55   u32 n_left;
56   u32 n_this_chunk;
57
58   n_left = VLIB_FRAME_SIZE;
59   n_buffers = 0;
60
61   if (PREDICT_TRUE(xd->dev_type == VNET_DPDK_DEV_ETH))
62     {
63       while (n_left)
64         {
65           n_this_chunk = rte_eth_rx_burst (xd->device_index, queue_id,
66                                            xd->rx_vectors[queue_id] + n_buffers, n_left);
67           n_buffers += n_this_chunk;
68           n_left -= n_this_chunk;
69
70           /* Empirically, DPDK r1.8 produces vectors w/ 32 or fewer elts */
71           if (n_this_chunk < 32)
72             break;
73       }
74     }
75   else if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
76     {
77       vlib_main_t * vm = vlib_get_main();
78       vlib_buffer_main_t * bm = vm->buffer_main;
79       unsigned socket_id = rte_socket_id();
80       u32 offset = 0;
81
82       offset = queue_id * VIRTIO_QNUM;
83
84       struct vhost_virtqueue *vq =
85         xd->vu_vhost_dev.virtqueue[offset + VIRTIO_TXQ];
86
87       if (PREDICT_FALSE(!vq->enabled))
88         return 0;
89
90       struct rte_mbuf **pkts = xd->rx_vectors[queue_id];
91       while (n_left) {
92           n_this_chunk = rte_vhost_dequeue_burst(&xd->vu_vhost_dev,
93                                                  offset + VIRTIO_TXQ,
94                                                  bm->pktmbuf_pools[socket_id],
95                                                  pkts + n_buffers,
96                                                  n_left);
97           n_buffers += n_this_chunk;
98           n_left -= n_this_chunk;
99           if (n_this_chunk == 0)
100               break;
101       }
102
103       int i; u32 bytes = 0;
104       for (i = 0; i < n_buffers; i++) {
105           struct rte_mbuf *buff = pkts[i];
106           bytes += rte_pktmbuf_data_len(buff);
107       } 
108
109       f64 now = vlib_time_now (vm);
110
111       dpdk_vu_vring *vring = NULL;
112       /* send pending interrupts if needed */
113       if (dpdk_vhost_user_want_interrupt(xd, offset + VIRTIO_TXQ)) {
114           vring = &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
115           vring->n_since_last_int += n_buffers;
116
117           if ((vring->n_since_last_int && (vring->int_deadline < now))
118               || (vring->n_since_last_int > dm->conf->vhost_coalesce_frames))
119             dpdk_vhost_user_send_interrupt(vm, xd, offset + VIRTIO_TXQ);
120       }
121
122       vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
123       vring->packets += n_buffers;
124       vring->bytes += bytes;
125
126       if (dpdk_vhost_user_want_interrupt(xd, offset + VIRTIO_RXQ)) {
127           if (vring->n_since_last_int && (vring->int_deadline < now))
128             dpdk_vhost_user_send_interrupt(vm, xd, offset + VIRTIO_RXQ);
129       }
130
131     }
132 #ifdef RTE_LIBRTE_KNI
133   else if (xd->dev_type == VNET_DPDK_DEV_KNI)
134     {
135       n_buffers = rte_kni_rx_burst(xd->kni, xd->rx_vectors[queue_id], VLIB_FRAME_SIZE);
136       rte_kni_handle_request(xd->kni);
137     }
138 #endif
139   else
140     {
141       ASSERT(0);
142     }
143
144   return n_buffers;
145 }
146
147
148 static inline void
149 dpdk_get_xstats (dpdk_device_t * xd)
150 {
151   int len;
152   if ((len = rte_eth_xstats_get(xd->device_index, NULL, 0)) > 0)
153     {
154       vec_validate(xd->xstats, len - 1);
155       vec_validate(xd->last_cleared_xstats, len - 1);
156
157       len = rte_eth_xstats_get(xd->device_index, xd->xstats, vec_len(xd->xstats));
158
159       ASSERT(vec_len(xd->xstats) == len);
160       ASSERT(vec_len(xd->last_cleared_xstats) == len);
161
162       _vec_len(xd->xstats) = len;
163       _vec_len(xd->last_cleared_xstats) = len;
164
165     }
166 }
167
168
169 static inline void
170 dpdk_update_counters (dpdk_device_t * xd, f64 now)
171 {
172   vlib_simple_counter_main_t * cm;
173   vnet_main_t * vnm = vnet_get_main();
174   u32 my_cpu = os_get_cpu_number();
175   u64 rxerrors, last_rxerrors;
176
177   /* only update counters for PMD interfaces */
178   if (xd->dev_type != VNET_DPDK_DEV_ETH)
179     return;
180
181   /*
182    * DAW-FIXME: VMXNET3 device stop/start doesn't work,
183    * therefore fake the stop in the dpdk driver by
184    * silently dropping all of the incoming pkts instead of
185    * stopping the driver / hardware.
186    */
187   if (xd->admin_up != 0xff)
188     {
189       xd->time_last_stats_update = now ? now : xd->time_last_stats_update;
190       clib_memcpy (&xd->last_stats, &xd->stats, sizeof (xd->last_stats));
191       rte_eth_stats_get (xd->device_index, &xd->stats);
192
193       /* maybe bump interface rx no buffer counter */
194       if (PREDICT_FALSE (xd->stats.rx_nombuf != xd->last_stats.rx_nombuf))
195         {
196           cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
197                                  VNET_INTERFACE_COUNTER_RX_NO_BUF);
198
199           vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
200                                          xd->stats.rx_nombuf -
201                                          xd->last_stats.rx_nombuf);
202         }
203
204       /* missed pkt counter */
205       if (PREDICT_FALSE (xd->stats.imissed != xd->last_stats.imissed))
206         {
207           cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
208                                  VNET_INTERFACE_COUNTER_RX_MISS);
209
210           vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
211                                          xd->stats.imissed -
212                                          xd->last_stats.imissed);
213         }
214       rxerrors = xd->stats.ierrors;
215       last_rxerrors = xd->last_stats.ierrors;
216
217       if (PREDICT_FALSE (rxerrors != last_rxerrors))
218         {
219           cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
220                                  VNET_INTERFACE_COUNTER_RX_ERROR);
221
222           vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
223                                          rxerrors - last_rxerrors);
224         }
225     }
226
227   dpdk_get_xstats(xd);
228 }