Move dpdk (un)format functions to separate c file
[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    2048
21 #define DPDK_NB_TX_DESC_10GE    2048
22 #define DPDK_NB_RX_DESC_40GE    (4096-128)
23 #define DPDK_NB_TX_DESC_40GE    2048
24
25 /* These args appear by themselves */
26 #define foreach_eal_double_hyphen_predicate_arg \
27 _(no-shconf)                                    \
28 _(no-hpet)                                      \
29 _(no-pci)                                       \
30 _(no-huge)                                      \
31 _(vmware-tsc-map)                               \
32 _(virtio-vhost)
33
34 #define foreach_eal_single_hyphen_mandatory_arg \
35 _(coremask, c)                                  \
36 _(nchannels, n)                                 \
37
38 #define foreach_eal_single_hyphen_arg           \
39 _(blacklist, b)                                 \
40 _(mem-alloc-request, m)                         \
41 _(force-ranks, r)
42
43 /* These args are preceeded by "--" and followed by a single string */
44 #define foreach_eal_double_hyphen_arg           \
45 _(huge-dir)                                     \
46 _(proc-type)                                    \
47 _(file-prefix)                                  \
48 _(socket-mem)                                   \
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 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
83       offset = queue_id * VIRTIO_QNUM;
84
85       struct vhost_virtqueue *vq =
86         xd->vu_vhost_dev.virtqueue[offset + VIRTIO_TXQ];
87
88       if (PREDICT_FALSE(!vq->enabled))
89         return 0;
90 #else
91       if (PREDICT_FALSE(!xd->vu_is_running))
92         return 0;
93 #endif
94
95       n_buffers = rte_vhost_dequeue_burst(&xd->vu_vhost_dev, offset + VIRTIO_TXQ,
96                                           bm->pktmbuf_pools[socket_id],
97                                           xd->rx_vectors[queue_id], VLIB_FRAME_SIZE);
98
99       f64 now = vlib_time_now (vm);
100
101       /* send pending interrupts if needed */
102       if (dpdk_vhost_user_want_interrupt(xd, offset + VIRTIO_TXQ)) {
103           dpdk_vu_vring *vring = &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
104           vring->n_since_last_int += n_buffers;
105
106           if ((vring->n_since_last_int && (vring->int_deadline < now))
107               || (vring->n_since_last_int > dm->vhost_coalesce_frames))
108             dpdk_vhost_user_send_interrupt(vm, xd, offset + VIRTIO_TXQ);
109       }
110
111       if (dpdk_vhost_user_want_interrupt(xd, offset + VIRTIO_RXQ)) {
112           dpdk_vu_vring *vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
113           if (vring->n_since_last_int && (vring->int_deadline < now))
114             dpdk_vhost_user_send_interrupt(vm, xd, offset + VIRTIO_RXQ);
115       }
116
117     }
118 #ifdef RTE_LIBRTE_KNI
119   else if (xd->dev_type == VNET_DPDK_DEV_KNI)
120     {
121       n_buffers = rte_kni_rx_burst(xd->kni, xd->rx_vectors[queue_id], VLIB_FRAME_SIZE);
122       rte_kni_handle_request(xd->kni);
123     }
124 #endif
125   else
126     {
127       ASSERT(0);
128     }
129
130   return n_buffers;
131 }
132
133
134 static inline void
135 dpdk_update_counters (dpdk_device_t * xd, f64 now)
136 {
137   vlib_simple_counter_main_t * cm;
138   vnet_main_t * vnm = vnet_get_main();
139   u32 my_cpu = os_get_cpu_number();
140   u64 rxerrors, last_rxerrors;
141   int len;
142
143   /* only update counters for PMD interfaces */
144   if (xd->dev_type != VNET_DPDK_DEV_ETH)
145     return;
146
147   /*
148    * DAW-FIXME: VMXNET3 device stop/start doesn't work,
149    * therefore fake the stop in the dpdk driver by
150    * silently dropping all of the incoming pkts instead of
151    * stopping the driver / hardware.
152    */
153   if (xd->admin_up != 0xff)
154     {
155       xd->time_last_stats_update = now ? now : xd->time_last_stats_update;
156       memcpy (&xd->last_stats, &xd->stats, sizeof (xd->last_stats));
157       rte_eth_stats_get (xd->device_index, &xd->stats);
158
159       /* maybe bump interface rx no buffer counter */
160       if (PREDICT_FALSE (xd->stats.rx_nombuf != xd->last_stats.rx_nombuf))
161         {
162           cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
163                                  VNET_INTERFACE_COUNTER_RX_NO_BUF);
164
165           vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
166                                          xd->stats.rx_nombuf -
167                                          xd->last_stats.rx_nombuf);
168         }
169
170       /* missed pkt counter */
171       if (PREDICT_FALSE (xd->stats.imissed != xd->last_stats.imissed))
172         {
173           cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
174                                  VNET_INTERFACE_COUNTER_RX_MISS);
175
176           vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
177                                          xd->stats.imissed -
178                                          xd->last_stats.imissed);
179         }
180 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
181       rxerrors = xd->stats.ierrors;
182       last_rxerrors = xd->last_stats.ierrors;
183 #else
184       rxerrors = xd->stats.ibadcrc
185         + xd->stats.ibadlen + xd->stats.ierrors;
186       last_rxerrors = xd->last_stats.ibadcrc
187         + xd->last_stats.ibadlen + xd->last_stats.ierrors;
188 #endif
189
190       if (PREDICT_FALSE (rxerrors != last_rxerrors))
191         {
192           cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
193                                  VNET_INTERFACE_COUNTER_RX_ERROR);
194
195           vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
196                                          rxerrors - last_rxerrors);
197         }
198     }
199
200   if ((len = rte_eth_xstats_get(xd->device_index, NULL, 0)) > 0)
201     {
202       vec_validate(xd->xstats, len - 1);
203       len = rte_eth_xstats_get(xd->device_index, xd->xstats, vec_len(xd->xstats));
204       ASSERT(vec_len(xd->xstats) == len);
205       _vec_len(xd->xstats) = len;
206     }
207 }