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