]>
Commit | Line | Data |
---|---|---|
8a9562d2 | 1 | /* |
12d0d124 | 2 | * Copyright (c) 2014, 2015, 2016, 2017 Nicira, Inc. |
8a9562d2 PS |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
01961bbd | 18 | #include "netdev-dpdk.h" |
8a9562d2 | 19 | |
6ebc4b09 | 20 | #include <errno.h> |
8a9562d2 PS |
21 | #include <signal.h> |
22 | #include <stdlib.h> | |
6ebc4b09 | 23 | #include <string.h> |
8a9562d2 | 24 | #include <unistd.h> |
f3e7ec25 MW |
25 | #include <linux/virtio_net.h> |
26 | #include <sys/socket.h> | |
27 | #include <linux/if.h> | |
01961bbd | 28 | |
5e925ccc | 29 | #include <rte_bus_pci.h> |
01961bbd DDP |
30 | #include <rte_config.h> |
31 | #include <rte_cycles.h> | |
32 | #include <rte_errno.h> | |
01961bbd | 33 | #include <rte_ethdev.h> |
6ebc4b09 | 34 | #include <rte_flow.h> |
01961bbd DDP |
35 | #include <rte_malloc.h> |
36 | #include <rte_mbuf.h> | |
37 | #include <rte_meter.h> | |
fc56f5e0 | 38 | #include <rte_pci.h> |
3eb8d4fa | 39 | #include <rte_version.h> |
6ebc4b09 | 40 | #include <rte_vhost.h> |
8a9562d2 | 41 | |
e8a2b5bf | 42 | #include "cmap.h" |
9ff24b9c | 43 | #include "coverage.h" |
7d1ced01 | 44 | #include "dirs.h" |
e14deea0 | 45 | #include "dp-packet.h" |
01961bbd | 46 | #include "dpdk.h" |
8a9562d2 | 47 | #include "dpif-netdev.h" |
e5c0f5a4 | 48 | #include "fatal-signal.h" |
988fd463 | 49 | #include "if-notifier.h" |
8a9562d2 PS |
50 | #include "netdev-provider.h" |
51 | #include "netdev-vport.h" | |
52 | #include "odp-util.h" | |
eac84432 | 53 | #include "openvswitch/dynamic-string.h" |
25d436fb | 54 | #include "openvswitch/list.h" |
6ebc4b09 | 55 | #include "openvswitch/match.h" |
25d436fb | 56 | #include "openvswitch/ofp-print.h" |
6ebc4b09 | 57 | #include "openvswitch/shash.h" |
25d436fb | 58 | #include "openvswitch/vlog.h" |
94143fc4 | 59 | #include "ovs-numa.h" |
8a9562d2 | 60 | #include "ovs-rcu.h" |
6ebc4b09 | 61 | #include "ovs-thread.h" |
8a9562d2 | 62 | #include "packets.h" |
0bf765f7 | 63 | #include "smap.h" |
8a9562d2 | 64 | #include "sset.h" |
8a9562d2 | 65 | #include "timeval.h" |
6ebc4b09 | 66 | #include "unaligned.h" |
8a9562d2 | 67 | #include "unixctl.h" |
29cf9c1b | 68 | #include "userspace-tso.h" |
6ebc4b09 IM |
69 | #include "util.h" |
70 | #include "uuid.h" | |
8a9562d2 | 71 | |
f3e7ec25 MW |
72 | enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM}; |
73 | ||
05b49df6 | 74 | VLOG_DEFINE_THIS_MODULE(netdev_dpdk); |
8a9562d2 PS |
75 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); |
76 | ||
9ff24b9c | 77 | COVERAGE_DEFINE(vhost_tx_contention); |
3d56e4ac | 78 | COVERAGE_DEFINE(vhost_notification); |
9ff24b9c | 79 | |
8a9562d2 PS |
80 | #define DPDK_PORT_WATCHDOG_INTERVAL 5 |
81 | ||
82 | #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE | |
83 | #define OVS_VPORT_DPDK "ovs_dpdk" | |
84 | ||
85 | /* | |
86 | * need to reserve tons of extra space in the mbufs so we can align the | |
87 | * DMA addresses to 4KB. | |
18f777b2 TP |
88 | * The minimum mbuf size is limited to avoid scatter behaviour and drop in |
89 | * performance for standard Ethernet MTU. | |
8a9562d2 | 90 | */ |
127b6a6e | 91 | #define ETHER_HDR_MAX_LEN (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN \ |
58be5c0e | 92 | + (2 * VLAN_HEADER_LEN)) |
127b6a6e IS |
93 | #define MTU_TO_FRAME_LEN(mtu) ((mtu) + RTE_ETHER_HDR_LEN + \ |
94 | RTE_ETHER_CRC_LEN) | |
4be4d22c | 95 | #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN) |
58be5c0e | 96 | #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \ |
127b6a6e | 97 | - RTE_ETHER_HDR_LEN - RTE_ETHER_CRC_LEN) |
4be4d22c | 98 | #define NETDEV_DPDK_MBUF_ALIGN 1024 |
0072e931 | 99 | #define NETDEV_DPDK_MAX_PKT_LEN 9728 |
8a9562d2 | 100 | |
43307ad0 IS |
101 | /* Max and min number of packets in the mempool. OVS tries to allocate a |
102 | * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have | |
103 | * enough hugepages) we keep halving the number until the allocation succeeds | |
104 | * or we reach MIN_NB_MBUF */ | |
105 | ||
106 | #define MAX_NB_MBUF (4096 * 64) | |
da79ce2b DDP |
107 | #define MIN_NB_MBUF (4096 * 4) |
108 | #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE | |
109 | ||
43307ad0 IS |
110 | /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */ |
111 | BUILD_ASSERT_DECL(MAX_NB_MBUF % ROUND_DOWN_POW2(MAX_NB_MBUF / MIN_NB_MBUF) | |
112 | == 0); | |
113 | ||
114 | /* The smallest possible NB_MBUF that we're going to try should be a multiple | |
115 | * of MP_CACHE_SZ. This is advised by DPDK documentation. */ | |
116 | BUILD_ASSERT_DECL((MAX_NB_MBUF / ROUND_DOWN_POW2(MAX_NB_MBUF / MIN_NB_MBUF)) | |
117 | % MP_CACHE_SZ == 0); | |
118 | ||
8a9562d2 PS |
119 | #define SOCKET0 0 |
120 | ||
b685696b CL |
121 | /* Default size of Physical NIC RXQ */ |
122 | #define NIC_PORT_DEFAULT_RXQ_SIZE 2048 | |
123 | /* Default size of Physical NIC TXQ */ | |
124 | #define NIC_PORT_DEFAULT_TXQ_SIZE 2048 | |
125 | /* Maximum size of Physical NIC Queues */ | |
126 | #define NIC_PORT_MAX_Q_SIZE 4096 | |
79f5354c | 127 | |
585a5bea | 128 | #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */ |
f3ea2ad2 IM |
129 | #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */ |
130 | #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not | |
131 | * yet mapped to another queue. */ | |
585a5bea | 132 | |
bb37956a IM |
133 | #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS |
134 | ||
5e925ccc MK |
135 | /* DPDK library uses uint16_t for port_id. */ |
136 | typedef uint16_t dpdk_port_t; | |
fa9f4eeb | 137 | #define DPDK_PORT_ID_FMT "%"PRIu16 |
bb37956a | 138 | |
080f080c KT |
139 | /* Minimum amount of vhost tx retries, effectively a disable. */ |
140 | #define VHOST_ENQ_RETRY_MIN 0 | |
141 | /* Maximum amount of vhost tx retries. */ | |
142 | #define VHOST_ENQ_RETRY_MAX 32 | |
143 | /* Legacy default value for vhost tx retries. */ | |
144 | #define VHOST_ENQ_RETRY_DEF 8 | |
145 | ||
0a0f39df | 146 | #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ) |
95e9881f | 147 | |
35b5586b FL |
148 | /* List of required flags advertised by the hardware that will be used |
149 | * if TSO is enabled. Ideally this should include DEV_TX_OFFLOAD_SCTP_CKSUM. | |
150 | * However, very few drivers supports that the moment and SCTP is not a | |
151 | * widely used protocol as TCP and UDP, so it's optional. */ | |
8c5163fe FL |
152 | #define DPDK_TX_TSO_OFFLOAD_FLAGS (DEV_TX_OFFLOAD_TCP_TSO \ |
153 | | DEV_TX_OFFLOAD_TCP_CKSUM \ | |
154 | | DEV_TX_OFFLOAD_UDP_CKSUM \ | |
155 | | DEV_TX_OFFLOAD_IPV4_CKSUM) | |
156 | ||
157 | ||
8a9562d2 | 158 | static const struct rte_eth_conf port_conf = { |
a28ddd11 | 159 | .rxmode = { |
a28ddd11 | 160 | .split_hdr_size = 0, |
03f3f9c0 | 161 | .offloads = 0, |
a28ddd11 DDP |
162 | }, |
163 | .rx_adv_conf = { | |
164 | .rss_conf = { | |
165 | .rss_key = NULL, | |
543342a4 | 166 | .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP, |
8a9562d2 | 167 | }, |
a28ddd11 DDP |
168 | }, |
169 | .txmode = { | |
170 | .mq_mode = ETH_MQ_TX_NONE, | |
171 | }, | |
8a9562d2 PS |
172 | }; |
173 | ||
f3e7ec25 MW |
174 | /* |
175 | * These callbacks allow virtio-net devices to be added to vhost ports when | |
176 | * configuration has been fully completed. | |
177 | */ | |
178 | static int new_device(int vid); | |
179 | static void destroy_device(int vid); | |
180 | static int vring_state_changed(int vid, uint16_t queue_id, int enable); | |
61473a0e | 181 | static void destroy_connection(int vid); |
3d56e4ac EC |
182 | static void vhost_guest_notified(int vid); |
183 | ||
f3e7ec25 MW |
184 | static const struct vhost_device_ops virtio_net_device_ops = |
185 | { | |
186 | .new_device = new_device, | |
187 | .destroy_device = destroy_device, | |
188 | .vring_state_changed = vring_state_changed, | |
61473a0e DM |
189 | .features_changed = NULL, |
190 | .new_connection = NULL, | |
191 | .destroy_connection = destroy_connection, | |
3d56e4ac | 192 | .guest_notified = vhost_guest_notified, |
f3e7ec25 MW |
193 | }; |
194 | ||
2f862c71 SV |
195 | /* Custom software stats for dpdk ports */ |
196 | struct netdev_dpdk_sw_stats { | |
197 | /* No. of retries when unable to transmit. */ | |
198 | uint64_t tx_retries; | |
199 | /* Packet drops when unable to transmit; Probably Tx queue is full. */ | |
200 | uint64_t tx_failure_drops; | |
201 | /* Packet length greater than device MTU. */ | |
202 | uint64_t tx_mtu_exceeded_drops; | |
203 | /* Packet drops in egress policer processing. */ | |
204 | uint64_t tx_qos_drops; | |
205 | /* Packet drops in ingress policer processing. */ | |
206 | uint64_t rx_qos_drops; | |
29cf9c1b FL |
207 | /* Packet drops in HWOL processing. */ |
208 | uint64_t tx_invalid_hwol_drops; | |
2f862c71 SV |
209 | }; |
210 | ||
58397e6c KT |
211 | enum dpdk_dev_type { |
212 | DPDK_DEV_ETH = 0, | |
7d1ced01 | 213 | DPDK_DEV_VHOST = 1, |
58397e6c KT |
214 | }; |
215 | ||
0bf765f7 IS |
216 | /* Quality of Service */ |
217 | ||
218 | /* An instance of a QoS configuration. Always associated with a particular | |
219 | * network device. | |
220 | * | |
221 | * Each QoS implementation subclasses this with whatever additional data it | |
222 | * needs. | |
223 | */ | |
224 | struct qos_conf { | |
225 | const struct dpdk_qos_ops *ops; | |
78bd47cf | 226 | rte_spinlock_t lock; |
0bf765f7 IS |
227 | }; |
228 | ||
23c01b19 EC |
229 | /* QoS queue information used by the netdev queue dump functions. */ |
230 | struct netdev_dpdk_queue_state { | |
231 | uint32_t *queues; | |
232 | size_t cur_queue; | |
233 | size_t n_queues; | |
234 | }; | |
235 | ||
0bf765f7 IS |
236 | /* A particular implementation of dpdk QoS operations. |
237 | * | |
238 | * The functions below return 0 if successful or a positive errno value on | |
239 | * failure, except where otherwise noted. All of them must be provided, except | |
240 | * where otherwise noted. | |
241 | */ | |
242 | struct dpdk_qos_ops { | |
243 | ||
244 | /* Name of the QoS type */ | |
245 | const char *qos_name; | |
246 | ||
78bd47cf DDP |
247 | /* Called to construct a qos_conf object. The implementation should make |
248 | * the appropriate calls to configure QoS according to 'details'. | |
0bf765f7 IS |
249 | * |
250 | * The contents of 'details' should be documented as valid for 'ovs_name' | |
251 | * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml | |
252 | * (which is built as ovs-vswitchd.conf.db(8)). | |
253 | * | |
78bd47cf DDP |
254 | * This function must return 0 if and only if it sets '*conf' to an |
255 | * initialized 'struct qos_conf'. | |
0bf765f7 IS |
256 | * |
257 | * For all QoS implementations it should always be non-null. | |
258 | */ | |
78bd47cf | 259 | int (*qos_construct)(const struct smap *details, struct qos_conf **conf); |
0bf765f7 IS |
260 | |
261 | /* Destroys the data structures allocated by the implementation as part of | |
78bd47cf | 262 | * 'qos_conf'. |
0bf765f7 IS |
263 | * |
264 | * For all QoS implementations it should always be non-null. | |
265 | */ | |
78bd47cf | 266 | void (*qos_destruct)(struct qos_conf *conf); |
0bf765f7 | 267 | |
78bd47cf | 268 | /* Retrieves details of 'conf' configuration into 'details'. |
0bf765f7 IS |
269 | * |
270 | * The contents of 'details' should be documented as valid for 'ovs_name' | |
271 | * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml | |
272 | * (which is built as ovs-vswitchd.conf.db(8)). | |
273 | */ | |
78bd47cf | 274 | int (*qos_get)(const struct qos_conf *conf, struct smap *details); |
0bf765f7 | 275 | |
78bd47cf | 276 | /* Returns true if 'conf' is already configured according to 'details'. |
0bf765f7 IS |
277 | * |
278 | * The contents of 'details' should be documented as valid for 'ovs_name' | |
279 | * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml | |
280 | * (which is built as ovs-vswitchd.conf.db(8)). | |
281 | * | |
78bd47cf | 282 | * For all QoS implementations it should always be non-null. |
0bf765f7 | 283 | */ |
78bd47cf DDP |
284 | bool (*qos_is_equal)(const struct qos_conf *conf, |
285 | const struct smap *details); | |
0bf765f7 IS |
286 | |
287 | /* Modify an array of rte_mbufs. The modification is specific to | |
288 | * each qos implementation. | |
289 | * | |
290 | * The function should take and array of mbufs and an int representing | |
291 | * the current number of mbufs present in the array. | |
292 | * | |
293 | * After the function has performed a qos modification to the array of | |
294 | * mbufs it returns an int representing the number of mbufs now present in | |
295 | * the array. This value is can then be passed to the port send function | |
296 | * along with the modified array for transmission. | |
297 | * | |
298 | * For all QoS implementations it should always be non-null. | |
299 | */ | |
78bd47cf | 300 | int (*qos_run)(struct qos_conf *qos_conf, struct rte_mbuf **pkts, |
7d7ded7a | 301 | int pkt_cnt, bool should_steal); |
23c01b19 EC |
302 | |
303 | /* Called to construct a QoS Queue. The implementation should make | |
304 | * the appropriate calls to configure QoS Queue according to 'details'. | |
305 | * | |
306 | * The contents of 'details' should be documented as valid for 'ovs_name' | |
307 | * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml | |
308 | * (which is built as ovs-vswitchd.conf.db(8)). | |
309 | * | |
310 | * This function must return 0 if and only if it constructs | |
311 | * QoS queue successfully. | |
312 | */ | |
313 | int (*qos_queue_construct)(const struct smap *details, | |
314 | uint32_t queue_id, struct qos_conf *conf); | |
315 | ||
316 | /* Destroys the QoS Queue. */ | |
317 | void (*qos_queue_destruct)(struct qos_conf *conf, uint32_t queue_id); | |
318 | ||
319 | /* Retrieves details of QoS Queue configuration into 'details'. | |
320 | * | |
321 | * The contents of 'details' should be documented as valid for 'ovs_name' | |
322 | * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml | |
323 | * (which is built as ovs-vswitchd.conf.db(8)). | |
324 | */ | |
325 | int (*qos_queue_get)(struct smap *details, uint32_t queue_id, | |
326 | const struct qos_conf *conf); | |
327 | ||
328 | /* Retrieves statistics of QoS Queue configuration into 'stats'. */ | |
329 | int (*qos_queue_get_stats)(const struct qos_conf *conf, uint32_t queue_id, | |
330 | struct netdev_queue_stats *stats); | |
331 | ||
332 | /* Setup the 'netdev_dpdk_queue_state' structure used by the dpdk queue | |
333 | * dump functions. | |
334 | */ | |
335 | int (*qos_queue_dump_state_init)(const struct qos_conf *conf, | |
336 | struct netdev_dpdk_queue_state *state); | |
0bf765f7 IS |
337 | }; |
338 | ||
e61bdffc | 339 | /* dpdk_qos_ops for each type of user space QoS implementation. */ |
0bf765f7 | 340 | static const struct dpdk_qos_ops egress_policer_ops; |
e61bdffc | 341 | static const struct dpdk_qos_ops trtcm_policer_ops; |
0bf765f7 IS |
342 | |
343 | /* | |
344 | * Array of dpdk_qos_ops, contains pointer to all supported QoS | |
345 | * operations. | |
346 | */ | |
347 | static const struct dpdk_qos_ops *const qos_confs[] = { | |
348 | &egress_policer_ops, | |
e61bdffc | 349 | &trtcm_policer_ops, |
0bf765f7 IS |
350 | NULL |
351 | }; | |
352 | ||
c2adb102 IM |
353 | static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER; |
354 | ||
8a9562d2 | 355 | /* Contains all 'struct dpdk_dev's. */ |
ca6ba700 | 356 | static struct ovs_list dpdk_list OVS_GUARDED_BY(dpdk_mutex) |
55951e15 | 357 | = OVS_LIST_INITIALIZER(&dpdk_list); |
8a9562d2 | 358 | |
c2adb102 IM |
359 | static struct ovs_mutex dpdk_mp_mutex OVS_ACQ_AFTER(dpdk_mutex) |
360 | = OVS_MUTEX_INITIALIZER; | |
361 | ||
91fccdad | 362 | /* Contains all 'struct dpdk_mp's. */ |
43307ad0 IS |
363 | static struct ovs_list dpdk_mp_list OVS_GUARDED_BY(dpdk_mp_mutex) |
364 | = OVS_LIST_INITIALIZER(&dpdk_mp_list); | |
91fccdad | 365 | |
91fccdad KT |
366 | struct dpdk_mp { |
367 | struct rte_mempool *mp; | |
43307ad0 IS |
368 | int mtu; |
369 | int socket_id; | |
370 | int refcount; | |
91fccdad KT |
371 | struct ovs_list list_node OVS_GUARDED_BY(dpdk_mp_mutex); |
372 | }; | |
373 | ||
5a034064 | 374 | /* There should be one 'struct dpdk_tx_queue' created for |
15ba075d | 375 | * each netdev tx queue. */ |
8a9562d2 | 376 | struct dpdk_tx_queue { |
15ba075d IM |
377 | /* Padding to make dpdk_tx_queue exactly one cache line long. */ |
378 | PADDED_MEMBERS(CACHE_LINE_SIZE, | |
379 | /* Protects the members and the NIC queue from concurrent access. | |
380 | * It is used only if the queue is shared among different pmd threads | |
381 | * (see 'concurrent_txq'). */ | |
382 | rte_spinlock_t tx_lock; | |
383 | /* Mapping of configured vhost-user queue to enabled by guest. */ | |
384 | int map; | |
385 | ); | |
8a9562d2 PS |
386 | }; |
387 | ||
9509913a IS |
388 | struct ingress_policer { |
389 | struct rte_meter_srtcm_params app_srtcm_params; | |
390 | struct rte_meter_srtcm in_policer; | |
03f3f9c0 | 391 | struct rte_meter_srtcm_profile in_prof; |
9509913a IS |
392 | rte_spinlock_t policer_lock; |
393 | }; | |
394 | ||
1a2bb118 SC |
395 | enum dpdk_hw_ol_features { |
396 | NETDEV_RX_CHECKSUM_OFFLOAD = 1 << 0, | |
e10ca8b9 | 397 | NETDEV_RX_HW_CRC_STRIP = 1 << 1, |
29cf9c1b FL |
398 | NETDEV_RX_HW_SCATTER = 1 << 2, |
399 | NETDEV_TX_TSO_OFFLOAD = 1 << 3, | |
35b5586b | 400 | NETDEV_TX_SCTP_CHECKSUM_OFFLOAD = 1 << 4, |
1a2bb118 SC |
401 | }; |
402 | ||
b2e72a9c IM |
403 | /* |
404 | * In order to avoid confusion in variables names, following naming convention | |
405 | * should be used, if possible: | |
406 | * | |
407 | * 'struct netdev' : 'netdev' | |
408 | * 'struct netdev_dpdk' : 'dev' | |
409 | * 'struct netdev_rxq' : 'rxq' | |
410 | * 'struct netdev_rxq_dpdk' : 'rx' | |
411 | * | |
412 | * Example: | |
413 | * struct netdev *netdev = netdev_from_name(name); | |
414 | * struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
415 | * | |
416 | * Also, 'netdev' should be used instead of 'dev->up', where 'netdev' was | |
417 | * already defined. | |
418 | */ | |
419 | ||
8a9562d2 | 420 | struct netdev_dpdk { |
23d4d53f BB |
421 | PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline0, |
422 | dpdk_port_t port_id; | |
423 | ||
424 | /* If true, device was attached by rte_eth_dev_attach(). */ | |
425 | bool attached; | |
606f6650 EC |
426 | /* If true, rte_eth_dev_start() was successfully called */ |
427 | bool started; | |
988fd463 EC |
428 | bool reset_needed; |
429 | /* 1 pad byte here. */ | |
23d4d53f BB |
430 | struct eth_addr hwaddr; |
431 | int mtu; | |
432 | int socket_id; | |
433 | int buf_size; | |
434 | int max_packet_len; | |
435 | enum dpdk_dev_type type; | |
436 | enum netdev_flags flags; | |
eaa43581 | 437 | int link_reset_cnt; |
bb9d2623 IM |
438 | union { |
439 | /* Device arguments for dpdk ports. */ | |
440 | char *devargs; | |
441 | /* Identifier used to distinguish vhost devices from each other. */ | |
442 | char *vhost_id; | |
443 | }; | |
23d4d53f BB |
444 | struct dpdk_tx_queue *tx_q; |
445 | struct rte_eth_link link; | |
23d4d53f BB |
446 | ); |
447 | ||
448 | PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline1, | |
449 | struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex); | |
43307ad0 | 450 | struct dpdk_mp *dpdk_mp; |
23d4d53f BB |
451 | |
452 | /* virtio identifier for vhost devices */ | |
453 | ovsrcu_index vid; | |
454 | ||
455 | /* True if vHost device is 'up' and has been reconfigured at least once */ | |
456 | bool vhost_reconfigured; | |
080f080c KT |
457 | |
458 | atomic_uint8_t vhost_tx_retries_max; | |
459 | /* 2 pad bytes here. */ | |
23d4d53f BB |
460 | ); |
461 | ||
23d4d53f BB |
462 | PADDED_MEMBERS(CACHE_LINE_SIZE, |
463 | struct netdev up; | |
464 | /* In dpdk_list. */ | |
465 | struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex); | |
466 | ||
467 | /* QoS configuration and lock for the device */ | |
468 | OVSRCU_TYPE(struct qos_conf *) qos_conf; | |
469 | ||
470 | /* Ingress Policer */ | |
471 | OVSRCU_TYPE(struct ingress_policer *) ingress_policer; | |
472 | uint32_t policer_rate; | |
473 | uint32_t policer_burst; | |
35c91567 DM |
474 | |
475 | /* Array of vhost rxq states, see vring_state_changed. */ | |
476 | bool *vhost_rxq_enabled; | |
23d4d53f BB |
477 | ); |
478 | ||
479 | PADDED_MEMBERS(CACHE_LINE_SIZE, | |
480 | struct netdev_stats stats; | |
2f862c71 | 481 | struct netdev_dpdk_sw_stats *sw_stats; |
23d4d53f BB |
482 | /* Protects stats */ |
483 | rte_spinlock_t stats_lock; | |
2f862c71 | 484 | /* 36 pad bytes here. */ |
23d4d53f BB |
485 | ); |
486 | ||
487 | PADDED_MEMBERS(CACHE_LINE_SIZE, | |
488 | /* The following properties cannot be changed when a device is running, | |
489 | * so we remember the request and update them next time | |
490 | * netdev_dpdk*_reconfigure() is called */ | |
491 | int requested_mtu; | |
492 | int requested_n_txq; | |
493 | int requested_n_rxq; | |
494 | int requested_rxq_size; | |
495 | int requested_txq_size; | |
496 | ||
497 | /* Number of rx/tx descriptors for physical devices */ | |
498 | int rxq_size; | |
499 | int txq_size; | |
500 | ||
501 | /* Socket ID detected when vHost device is brought up */ | |
502 | int requested_socket_id; | |
503 | ||
504 | /* Denotes whether vHost port is client/server mode */ | |
505 | uint64_t vhost_driver_flags; | |
506 | ||
507 | /* DPDK-ETH Flow control */ | |
508 | struct rte_eth_fc_conf fc_conf; | |
509 | ||
510 | /* DPDK-ETH hardware offload features, | |
511 | * from the enum set 'dpdk_hw_ol_features' */ | |
512 | uint32_t hw_ol_features; | |
f8b64a61 RM |
513 | |
514 | /* Properties for link state change detection mode. | |
515 | * If lsc_interrupt_mode is set to false, poll mode is used, | |
516 | * otherwise interrupt mode is used. */ | |
517 | bool requested_lsc_interrupt_mode; | |
518 | bool lsc_interrupt_mode; | |
f4336f50 GR |
519 | |
520 | /* VF configuration. */ | |
521 | struct eth_addr requested_hwaddr; | |
23d4d53f | 522 | ); |
971f4b39 MW |
523 | |
524 | PADDED_MEMBERS(CACHE_LINE_SIZE, | |
525 | /* Names of all XSTATS counters */ | |
526 | struct rte_eth_xstat_name *rte_xstats_names; | |
527 | int rte_xstats_names_size; | |
528 | int rte_xstats_ids_size; | |
529 | uint64_t *rte_xstats_ids; | |
530 | ); | |
8a9562d2 PS |
531 | }; |
532 | ||
533 | struct netdev_rxq_dpdk { | |
534 | struct netdev_rxq up; | |
bb37956a | 535 | dpdk_port_t port_id; |
8a9562d2 PS |
536 | }; |
537 | ||
f3e7ec25 MW |
538 | static void netdev_dpdk_destruct(struct netdev *netdev); |
539 | static void netdev_dpdk_vhost_destruct(struct netdev *netdev); | |
8a9562d2 | 540 | |
b99ab8aa IM |
541 | static int netdev_dpdk_get_sw_custom_stats(const struct netdev *, |
542 | struct netdev_custom_stats *); | |
ac1a9bb9 IM |
543 | static void netdev_dpdk_clear_xstats(struct netdev_dpdk *dev); |
544 | ||
0a0f39df | 545 | int netdev_dpdk_get_vid(const struct netdev_dpdk *dev); |
58397e6c | 546 | |
9509913a IS |
547 | struct ingress_policer * |
548 | netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev); | |
549 | ||
8a9562d2 PS |
550 | static bool |
551 | is_dpdk_class(const struct netdev_class *class) | |
552 | { | |
f3e7ec25 MW |
553 | return class->destruct == netdev_dpdk_destruct |
554 | || class->destruct == netdev_dpdk_vhost_destruct; | |
8a9562d2 PS |
555 | } |
556 | ||
4be4d22c MK |
557 | /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically |
558 | * aligned at 1k or less. If a declared mbuf size is not a multiple of this | |
559 | * value, insufficient buffers are allocated to accomodate the packet in its | |
560 | * entirety. Furthermore, certain drivers need to ensure that there is also | |
561 | * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ | |
562 | * frames). If the RX buffer is too small, then the driver enables scatter RX | |
58be5c0e MK |
563 | * behaviour, which reduces performance. To prevent this, use a buffer size |
564 | * that is closest to 'mtu', but which satisfies the aforementioned criteria. | |
4be4d22c MK |
565 | */ |
566 | static uint32_t | |
567 | dpdk_buf_size(int mtu) | |
568 | { | |
a32bab26 TL |
569 | return ROUND_UP(MTU_TO_MAX_FRAME_LEN(mtu), NETDEV_DPDK_MBUF_ALIGN) |
570 | + RTE_PKTMBUF_HEADROOM; | |
4be4d22c MK |
571 | } |
572 | ||
eff23640 DDP |
573 | /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed. |
574 | * | |
575 | * Unlike xmalloc(), this function can return NULL on failure. */ | |
8a9562d2 PS |
576 | static void * |
577 | dpdk_rte_mzalloc(size_t sz) | |
578 | { | |
eff23640 | 579 | return rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE); |
8a9562d2 PS |
580 | } |
581 | ||
582 | void | |
e14deea0 | 583 | free_dpdk_buf(struct dp_packet *p) |
8a9562d2 | 584 | { |
db73f716 | 585 | struct rte_mbuf *pkt = (struct rte_mbuf *) p; |
8a9562d2 | 586 | |
b00b4a81 | 587 | rte_pktmbuf_free(pkt); |
8a9562d2 PS |
588 | } |
589 | ||
b3cd9f9d | 590 | static void |
401b70d6 | 591 | ovs_rte_pktmbuf_init(struct rte_mempool *mp OVS_UNUSED, |
b3cd9f9d | 592 | void *opaque_arg OVS_UNUSED, |
2391135c | 593 | void *_p, |
b3cd9f9d PS |
594 | unsigned i OVS_UNUSED) |
595 | { | |
2391135c | 596 | struct rte_mbuf *pkt = _p; |
b3cd9f9d | 597 | |
3aaa6201 | 598 | dp_packet_init_dpdk((struct dp_packet *) pkt); |
b3cd9f9d PS |
599 | } |
600 | ||
91fccdad KT |
601 | static int |
602 | dpdk_mp_full(const struct rte_mempool *mp) OVS_REQUIRES(dpdk_mp_mutex) | |
603 | { | |
1f84a2d5 KT |
604 | /* At this point we want to know if all the mbufs are back |
605 | * in the mempool. rte_mempool_full() is not atomic but it's | |
606 | * the best available and as we are no longer requesting mbufs | |
607 | * from the mempool, it means mbufs will not move from | |
608 | * 'mempool ring' --> 'mempool cache'. In rte_mempool_full() | |
609 | * the ring is counted before caches, so we won't get false | |
610 | * positives in this use case and we handle false negatives. | |
611 | * | |
612 | * If future implementations of rte_mempool_full() were to change | |
613 | * it could be possible for a false positive. Even that would | |
614 | * likely be ok, as there are additional checks during mempool | |
615 | * freeing but it would make things racey. | |
91fccdad | 616 | */ |
1f84a2d5 | 617 | return rte_mempool_full(mp); |
91fccdad KT |
618 | } |
619 | ||
620 | /* Free unused mempools. */ | |
621 | static void | |
43307ad0 | 622 | dpdk_mp_sweep(void) OVS_REQUIRES(dpdk_mp_mutex) |
91fccdad KT |
623 | { |
624 | struct dpdk_mp *dmp, *next; | |
625 | ||
43307ad0 IS |
626 | LIST_FOR_EACH_SAFE (dmp, next, list_node, &dpdk_mp_list) { |
627 | if (!dmp->refcount && dpdk_mp_full(dmp->mp)) { | |
91fccdad KT |
628 | VLOG_DBG("Freeing mempool \"%s\"", dmp->mp->name); |
629 | ovs_list_remove(&dmp->list_node); | |
630 | rte_mempool_free(dmp->mp); | |
631 | rte_free(dmp); | |
632 | } | |
633 | } | |
91fccdad KT |
634 | } |
635 | ||
43307ad0 IS |
636 | /* Calculating the required number of mbufs differs depending on the |
637 | * mempool model being used. Check if per port memory is in use before | |
638 | * calculating. | |
639 | */ | |
640 | static uint32_t | |
641 | dpdk_calculate_mbufs(struct netdev_dpdk *dev, int mtu, bool per_port_mp) | |
91fccdad | 642 | { |
43307ad0 | 643 | uint32_t n_mbufs; |
91fccdad | 644 | |
43307ad0 IS |
645 | if (!per_port_mp) { |
646 | /* Shared memory are being used. | |
647 | * XXX: this is a really rough method of provisioning memory. | |
648 | * It's impossible to determine what the exact memory requirements are | |
649 | * when the number of ports and rxqs that utilize a particular mempool | |
650 | * can change dynamically at runtime. For now, use this rough | |
651 | * heurisitic. | |
652 | */ | |
127b6a6e | 653 | if (mtu >= RTE_ETHER_MTU) { |
43307ad0 IS |
654 | n_mbufs = MAX_NB_MBUF; |
655 | } else { | |
656 | n_mbufs = MIN_NB_MBUF; | |
91fccdad | 657 | } |
43307ad0 IS |
658 | } else { |
659 | /* Per port memory is being used. | |
660 | * XXX: rough estimation of number of mbufs required for this port: | |
661 | * <packets required to fill the device rxqs> | |
662 | * + <packets that could be stuck on other ports txqs> | |
663 | * + <packets in the pmd threads> | |
664 | * + <additional memory for corner cases> | |
665 | */ | |
666 | n_mbufs = dev->requested_n_rxq * dev->requested_rxq_size | |
667 | + dev->requested_n_txq * dev->requested_txq_size | |
668 | + MIN(RTE_MAX_LCORE, dev->requested_n_rxq) * NETDEV_MAX_BURST | |
669 | + MIN_NB_MBUF; | |
91fccdad | 670 | } |
43307ad0 IS |
671 | |
672 | return n_mbufs; | |
91fccdad KT |
673 | } |
674 | ||
43307ad0 IS |
675 | static struct dpdk_mp * |
676 | dpdk_mp_create(struct netdev_dpdk *dev, int mtu, bool per_port_mp) | |
8a9562d2 | 677 | { |
24e78f93 IM |
678 | char mp_name[RTE_MEMPOOL_NAMESIZE]; |
679 | const char *netdev_name = netdev_get_name(&dev->up); | |
680 | int socket_id = dev->requested_socket_id; | |
dfaf00e8 MK |
681 | uint32_t n_mbufs = 0; |
682 | uint32_t mbuf_size = 0; | |
683 | uint32_t aligned_mbuf_size = 0; | |
684 | uint32_t mbuf_priv_data_len = 0; | |
685 | uint32_t pkt_size = 0; | |
24e78f93 | 686 | uint32_t hash = hash_string(netdev_name, 0); |
43307ad0 IS |
687 | struct dpdk_mp *dmp = NULL; |
688 | int ret; | |
689 | ||
690 | dmp = dpdk_rte_mzalloc(sizeof *dmp); | |
691 | if (!dmp) { | |
692 | return NULL; | |
693 | } | |
694 | dmp->socket_id = socket_id; | |
695 | dmp->mtu = mtu; | |
696 | dmp->refcount = 1; | |
697 | ||
dfaf00e8 MK |
698 | /* Get the size of each mbuf, based on the MTU */ |
699 | mbuf_size = MTU_TO_FRAME_LEN(mtu); | |
700 | ||
43307ad0 | 701 | n_mbufs = dpdk_calculate_mbufs(dev, mtu, per_port_mp); |
d555d9bd | 702 | |
da79ce2b | 703 | do { |
24e78f93 | 704 | /* Full DPDK memory pool name must be unique and cannot be |
43307ad0 IS |
705 | * longer than RTE_MEMPOOL_NAMESIZE. Note that for the shared |
706 | * mempool case this can result in one device using a mempool | |
707 | * which references a different device in it's name. However as | |
708 | * mempool names are hashed, the device name will not be readable | |
709 | * so this is not an issue for tasks such as debugging. | |
710 | */ | |
711 | ret = snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, | |
dfaf00e8 MK |
712 | "ovs%08x%02d%05d%07u", |
713 | hash, socket_id, mtu, n_mbufs); | |
24e78f93 IM |
714 | if (ret < 0 || ret >= RTE_MEMPOOL_NAMESIZE) { |
715 | VLOG_DBG("snprintf returned %d. " | |
716 | "Failed to generate a mempool name for \"%s\". " | |
717 | "Hash:0x%x, socket_id: %d, mtu:%d, mbufs:%u.", | |
718 | ret, netdev_name, hash, socket_id, mtu, n_mbufs); | |
719 | break; | |
65056fd7 | 720 | } |
95fb793a | 721 | |
dfaf00e8 MK |
722 | VLOG_DBG("Port %s: Requesting a mempool of %u mbufs of size %u " |
723 | "on socket %d for %d Rx and %d Tx queues, " | |
724 | "cache line size of %u", | |
725 | netdev_name, n_mbufs, mbuf_size, socket_id, | |
726 | dev->requested_n_rxq, dev->requested_n_txq, | |
727 | RTE_CACHE_LINE_SIZE); | |
728 | ||
a32bab26 TL |
729 | /* The size of the mbuf's private area (i.e. area that holds OvS' |
730 | * dp_packet data)*/ | |
dfaf00e8 MK |
731 | mbuf_priv_data_len = sizeof(struct dp_packet) - |
732 | sizeof(struct rte_mbuf); | |
733 | /* The size of the entire dp_packet. */ | |
734 | pkt_size = sizeof(struct dp_packet) + mbuf_size; | |
735 | /* mbuf size, rounded up to cacheline size. */ | |
736 | aligned_mbuf_size = ROUND_UP(pkt_size, RTE_CACHE_LINE_SIZE); | |
737 | /* If there is a size discrepancy, add padding to mbuf_priv_data_len. | |
738 | * This maintains mbuf size cache alignment, while also honoring RX | |
739 | * buffer alignment in the data portion of the mbuf. If this adjustment | |
740 | * is not made, there is a possiblity later on that for an element of | |
741 | * the mempool, buf, buf->data_len < (buf->buf_len - buf->data_off). | |
742 | * This is problematic in the case of multi-segment mbufs, particularly | |
743 | * when an mbuf segment needs to be resized (when [push|popp]ing a VLAN | |
744 | * header, for example. | |
745 | */ | |
746 | mbuf_priv_data_len += (aligned_mbuf_size - pkt_size); | |
747 | ||
748 | dmp->mp = rte_pktmbuf_pool_create(mp_name, n_mbufs, MP_CACHE_SZ, | |
749 | mbuf_priv_data_len, | |
750 | mbuf_size, | |
43307ad0 | 751 | socket_id); |
24e78f93 | 752 | |
43307ad0 | 753 | if (dmp->mp) { |
24e78f93 IM |
754 | VLOG_DBG("Allocated \"%s\" mempool with %u mbufs", |
755 | mp_name, n_mbufs); | |
837c1761 | 756 | /* rte_pktmbuf_pool_create has done some initialization of the |
43307ad0 IS |
757 | * rte_mbuf part of each dp_packet, while ovs_rte_pktmbuf_init |
758 | * initializes some OVS specific fields of dp_packet. | |
759 | */ | |
760 | rte_mempool_obj_iter(dmp->mp, ovs_rte_pktmbuf_init, NULL); | |
761 | return dmp; | |
d555d9bd RW |
762 | } else if (rte_errno == EEXIST) { |
763 | /* A mempool with the same name already exists. We just | |
764 | * retrieve its pointer to be returned to the caller. */ | |
43307ad0 | 765 | dmp->mp = rte_mempool_lookup(mp_name); |
d555d9bd RW |
766 | /* As the mempool create returned EEXIST we can expect the |
767 | * lookup has returned a valid pointer. If for some reason | |
768 | * that's not the case we keep track of it. */ | |
24e78f93 | 769 | VLOG_DBG("A mempool with name \"%s\" already exists at %p.", |
43307ad0 IS |
770 | mp_name, dmp->mp); |
771 | return dmp; | |
d555d9bd | 772 | } else { |
43307ad0 IS |
773 | VLOG_DBG("Failed to create mempool \"%s\" with a request of " |
774 | "%u mbufs, retrying with %u mbufs", | |
775 | mp_name, n_mbufs, n_mbufs / 2); | |
0c6f39e5 | 776 | } |
43307ad0 | 777 | } while (!dmp->mp && rte_errno == ENOMEM && (n_mbufs /= 2) >= MIN_NB_MBUF); |
2ae3d542 | 778 | |
43307ad0 IS |
779 | VLOG_ERR("Failed to create mempool \"%s\" with a request of %u mbufs", |
780 | mp_name, n_mbufs); | |
781 | ||
782 | rte_free(dmp); | |
783 | return NULL; | |
8a9562d2 PS |
784 | } |
785 | ||
43307ad0 IS |
786 | static struct dpdk_mp * |
787 | dpdk_mp_get(struct netdev_dpdk *dev, int mtu, bool per_port_mp) | |
8a9562d2 | 788 | { |
43307ad0 IS |
789 | struct dpdk_mp *dmp, *next; |
790 | bool reuse = false; | |
8a9562d2 | 791 | |
c2adb102 | 792 | ovs_mutex_lock(&dpdk_mp_mutex); |
43307ad0 IS |
793 | /* Check if shared memory is being used, if so check existing mempools |
794 | * to see if reuse is possible. */ | |
795 | if (!per_port_mp) { | |
796 | LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) { | |
797 | if (dmp->socket_id == dev->requested_socket_id | |
798 | && dmp->mtu == mtu) { | |
799 | VLOG_DBG("Reusing mempool \"%s\"", dmp->mp->name); | |
800 | dmp->refcount++; | |
801 | reuse = true; | |
802 | break; | |
803 | } | |
804 | } | |
805 | } | |
806 | /* Sweep mempools after reuse or before create. */ | |
807 | dpdk_mp_sweep(); | |
91fccdad | 808 | |
43307ad0 IS |
809 | if (!reuse) { |
810 | dmp = dpdk_mp_create(dev, mtu, per_port_mp); | |
91fccdad | 811 | if (dmp) { |
43307ad0 IS |
812 | /* Shared memory will hit the reuse case above so will not |
813 | * request a mempool that already exists but we need to check | |
814 | * for the EEXIST case for per port memory case. Compare the | |
815 | * mempool returned by dmp to each entry in dpdk_mp_list. If a | |
816 | * match is found, free dmp as a new entry is not required, set | |
817 | * dmp to point to the existing entry and increment the refcount | |
818 | * to avoid being freed at a later stage. | |
819 | */ | |
820 | if (per_port_mp && rte_errno == EEXIST) { | |
821 | LIST_FOR_EACH (next, list_node, &dpdk_mp_list) { | |
822 | if (dmp->mp == next->mp) { | |
823 | rte_free(dmp); | |
824 | dmp = next; | |
825 | dmp->refcount++; | |
826 | } | |
827 | } | |
828 | } else { | |
829 | ovs_list_push_back(&dpdk_mp_list, &dmp->list_node); | |
830 | } | |
91fccdad KT |
831 | } |
832 | } | |
43307ad0 | 833 | |
43307ad0 IS |
834 | ovs_mutex_unlock(&dpdk_mp_mutex); |
835 | ||
836 | return dmp; | |
837 | } | |
838 | ||
839 | /* Decrement reference to a mempool. */ | |
840 | static void | |
841 | dpdk_mp_put(struct dpdk_mp *dmp) | |
842 | { | |
843 | if (!dmp) { | |
844 | return; | |
845 | } | |
846 | ||
847 | ovs_mutex_lock(&dpdk_mp_mutex); | |
848 | ovs_assert(dmp->refcount); | |
849 | dmp->refcount--; | |
c2adb102 | 850 | ovs_mutex_unlock(&dpdk_mp_mutex); |
8a9562d2 PS |
851 | } |
852 | ||
43307ad0 IS |
853 | /* Depending on the memory model being used this function tries to |
854 | * identify and reuse an existing mempool or tries to allocate a new | |
855 | * mempool on requested_socket_id with mbuf size corresponding to the | |
856 | * requested_mtu. On success, a new configuration will be applied. | |
0072e931 MK |
857 | * On error, device will be left unchanged. */ |
858 | static int | |
859 | netdev_dpdk_mempool_configure(struct netdev_dpdk *dev) | |
0072e931 MK |
860 | OVS_REQUIRES(dev->mutex) |
861 | { | |
862 | uint32_t buf_size = dpdk_buf_size(dev->requested_mtu); | |
43307ad0 | 863 | struct dpdk_mp *dmp; |
24e78f93 | 864 | int ret = 0; |
43307ad0 | 865 | bool per_port_mp = dpdk_per_port_memory(); |
0072e931 | 866 | |
43307ad0 IS |
867 | /* With shared memory we do not need to configure a mempool if the MTU |
868 | * and socket ID have not changed, the previous configuration is still | |
869 | * valid so return 0 */ | |
870 | if (!per_port_mp && dev->mtu == dev->requested_mtu | |
871 | && dev->socket_id == dev->requested_socket_id) { | |
872 | return ret; | |
873 | } | |
91fccdad | 874 | |
43307ad0 IS |
875 | dmp = dpdk_mp_get(dev, FRAME_LEN_TO_MTU(buf_size), per_port_mp); |
876 | if (!dmp) { | |
c67e46c0 MK |
877 | VLOG_ERR("Failed to create memory pool for netdev " |
878 | "%s, with MTU %d on socket %d: %s\n", | |
879 | dev->up.name, dev->requested_mtu, dev->requested_socket_id, | |
880 | rte_strerror(rte_errno)); | |
24e78f93 | 881 | ret = rte_errno; |
0072e931 | 882 | } else { |
43307ad0 IS |
883 | /* Check for any pre-existing dpdk_mp for the device before accessing |
884 | * the associated mempool. | |
885 | */ | |
886 | if (dev->dpdk_mp != NULL) { | |
887 | /* A new MTU was requested, decrement the reference count for the | |
888 | * devices current dpdk_mp. This is required even if a pointer to | |
889 | * same dpdk_mp is returned by dpdk_mp_get. The refcount for dmp | |
890 | * has already been incremented by dpdk_mp_get at this stage so it | |
891 | * must be decremented to keep an accurate refcount for the | |
892 | * dpdk_mp. | |
893 | */ | |
894 | dpdk_mp_put(dev->dpdk_mp); | |
895 | } | |
896 | dev->dpdk_mp = dmp; | |
0072e931 MK |
897 | dev->mtu = dev->requested_mtu; |
898 | dev->socket_id = dev->requested_socket_id; | |
899 | dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu); | |
900 | } | |
901 | ||
24e78f93 | 902 | return ret; |
0072e931 MK |
903 | } |
904 | ||
8a9562d2 PS |
905 | static void |
906 | check_link_status(struct netdev_dpdk *dev) | |
907 | { | |
908 | struct rte_eth_link link; | |
909 | ||
910 | rte_eth_link_get_nowait(dev->port_id, &link); | |
911 | ||
912 | if (dev->link.link_status != link.link_status) { | |
3e912ffc | 913 | netdev_change_seq_changed(&dev->up); |
8a9562d2 PS |
914 | |
915 | dev->link_reset_cnt++; | |
916 | dev->link = link; | |
917 | if (dev->link.link_status) { | |
fa9f4eeb IM |
918 | VLOG_DBG_RL(&rl, |
919 | "Port "DPDK_PORT_ID_FMT" Link Up - speed %u Mbps - %s", | |
58be5c0e | 920 | dev->port_id, (unsigned) dev->link.link_speed, |
fa9f4eeb IM |
921 | (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) |
922 | ? "full-duplex" : "half-duplex"); | |
8a9562d2 | 923 | } else { |
fa9f4eeb IM |
924 | VLOG_DBG_RL(&rl, "Port "DPDK_PORT_ID_FMT" Link Down", |
925 | dev->port_id); | |
8a9562d2 PS |
926 | } |
927 | } | |
928 | } | |
929 | ||
930 | static void * | |
931 | dpdk_watchdog(void *dummy OVS_UNUSED) | |
932 | { | |
933 | struct netdev_dpdk *dev; | |
934 | ||
935 | pthread_detach(pthread_self()); | |
936 | ||
937 | for (;;) { | |
938 | ovs_mutex_lock(&dpdk_mutex); | |
939 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { | |
940 | ovs_mutex_lock(&dev->mutex); | |
1f5b157e IM |
941 | if (dev->type == DPDK_DEV_ETH) { |
942 | check_link_status(dev); | |
943 | } | |
8a9562d2 PS |
944 | ovs_mutex_unlock(&dev->mutex); |
945 | } | |
946 | ovs_mutex_unlock(&dpdk_mutex); | |
947 | xsleep(DPDK_PORT_WATCHDOG_INTERVAL); | |
948 | } | |
949 | ||
950 | return NULL; | |
951 | } | |
952 | ||
b98d7669 | 953 | static int |
f8b64a61 | 954 | dpdk_eth_dev_port_config(struct netdev_dpdk *dev, int n_rxq, int n_txq) |
b98d7669 DDP |
955 | { |
956 | int diag = 0; | |
957 | int i; | |
0072e931 | 958 | struct rte_eth_conf conf = port_conf; |
65a87968 | 959 | struct rte_eth_dev_info info; |
4dd16ca0 | 960 | uint16_t conf_mtu; |
65a87968 | 961 | |
03f3f9c0 OM |
962 | rte_eth_dev_info_get(dev->port_id, &info); |
963 | ||
db704171 JCR |
964 | /* As of DPDK 19.11, it is not allowed to set a mq_mode for |
965 | * virtio PMD driver. */ | |
966 | if (!strcmp(info.driver_name, "net_virtio")) { | |
967 | conf.rxmode.mq_mode = ETH_MQ_RX_NONE; | |
968 | } else { | |
969 | conf.rxmode.mq_mode = ETH_MQ_RX_RSS; | |
970 | } | |
971 | ||
65a87968 | 972 | /* As of DPDK 17.11.1 a few PMDs require to explicitly enable |
03f3f9c0 OM |
973 | * scatter to support jumbo RX. |
974 | * Setting scatter for the device is done after checking for | |
975 | * scatter support in the device capabilites. */ | |
127b6a6e | 976 | if (dev->mtu > RTE_ETHER_MTU) { |
03f3f9c0 OM |
977 | if (dev->hw_ol_features & NETDEV_RX_HW_SCATTER) { |
978 | conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER; | |
65a87968 | 979 | } |
0072e931 | 980 | } |
67fe6d63 | 981 | |
f8b64a61 | 982 | conf.intr_conf.lsc = dev->lsc_interrupt_mode; |
e10ca8b9 | 983 | |
03f3f9c0 OM |
984 | if (dev->hw_ol_features & NETDEV_RX_CHECKSUM_OFFLOAD) { |
985 | conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM; | |
986 | } | |
987 | ||
988 | if (!(dev->hw_ol_features & NETDEV_RX_HW_CRC_STRIP) | |
989 | && info.rx_offload_capa & DEV_RX_OFFLOAD_KEEP_CRC) { | |
990 | conf.rxmode.offloads |= DEV_RX_OFFLOAD_KEEP_CRC; | |
e10ca8b9 MW |
991 | } |
992 | ||
29cf9c1b | 993 | if (dev->hw_ol_features & NETDEV_TX_TSO_OFFLOAD) { |
8c5163fe | 994 | conf.txmode.offloads |= DPDK_TX_TSO_OFFLOAD_FLAGS; |
35b5586b FL |
995 | if (dev->hw_ol_features & NETDEV_TX_SCTP_CHECKSUM_OFFLOAD) { |
996 | conf.txmode.offloads |= DEV_TX_OFFLOAD_SCTP_CKSUM; | |
997 | } | |
29cf9c1b FL |
998 | } |
999 | ||
03f3f9c0 OM |
1000 | /* Limit configured rss hash functions to only those supported |
1001 | * by the eth device. */ | |
1002 | conf.rx_adv_conf.rss_conf.rss_hf &= info.flow_type_rss_offloads; | |
1003 | ||
b98d7669 DDP |
1004 | /* A device may report more queues than it makes available (this has |
1005 | * been observed for Intel xl710, which reserves some of them for | |
1006 | * SRIOV): rte_eth_*_queue_setup will fail if a queue is not | |
1007 | * available. When this happens we can retry the configuration | |
1008 | * and request less queues */ | |
1009 | while (n_rxq && n_txq) { | |
1010 | if (diag) { | |
1011 | VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq, n_txq); | |
1012 | } | |
1013 | ||
0072e931 | 1014 | diag = rte_eth_dev_configure(dev->port_id, n_rxq, n_txq, &conf); |
b98d7669 | 1015 | if (diag) { |
0072e931 MK |
1016 | VLOG_WARN("Interface %s eth_dev setup error %s\n", |
1017 | dev->up.name, rte_strerror(-diag)); | |
b98d7669 DDP |
1018 | break; |
1019 | } | |
1020 | ||
67fe6d63 MK |
1021 | diag = rte_eth_dev_set_mtu(dev->port_id, dev->mtu); |
1022 | if (diag) { | |
4dd16ca0 IS |
1023 | /* A device may not support rte_eth_dev_set_mtu, in this case |
1024 | * flag a warning to the user and include the devices configured | |
1025 | * MTU value that will be used instead. */ | |
1026 | if (-ENOTSUP == diag) { | |
1027 | rte_eth_dev_get_mtu(dev->port_id, &conf_mtu); | |
1028 | VLOG_WARN("Interface %s does not support MTU configuration, " | |
1029 | "max packet size supported is %"PRIu16".", | |
1030 | dev->up.name, conf_mtu); | |
1031 | } else { | |
1032 | VLOG_ERR("Interface %s MTU (%d) setup error: %s", | |
1033 | dev->up.name, dev->mtu, rte_strerror(-diag)); | |
1034 | break; | |
1035 | } | |
67fe6d63 MK |
1036 | } |
1037 | ||
b98d7669 | 1038 | for (i = 0; i < n_txq; i++) { |
b685696b | 1039 | diag = rte_eth_tx_queue_setup(dev->port_id, i, dev->txq_size, |
b98d7669 DDP |
1040 | dev->socket_id, NULL); |
1041 | if (diag) { | |
1dfebee9 | 1042 | VLOG_INFO("Interface %s unable to setup txq(%d): %s", |
b98d7669 DDP |
1043 | dev->up.name, i, rte_strerror(-diag)); |
1044 | break; | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | if (i != n_txq) { | |
1049 | /* Retry with less tx queues */ | |
1050 | n_txq = i; | |
1051 | continue; | |
1052 | } | |
1053 | ||
1054 | for (i = 0; i < n_rxq; i++) { | |
b685696b | 1055 | diag = rte_eth_rx_queue_setup(dev->port_id, i, dev->rxq_size, |
43307ad0 IS |
1056 | dev->socket_id, NULL, |
1057 | dev->dpdk_mp->mp); | |
b98d7669 | 1058 | if (diag) { |
1dfebee9 | 1059 | VLOG_INFO("Interface %s unable to setup rxq(%d): %s", |
b98d7669 DDP |
1060 | dev->up.name, i, rte_strerror(-diag)); |
1061 | break; | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | if (i != n_rxq) { | |
1066 | /* Retry with less rx queues */ | |
1067 | n_rxq = i; | |
1068 | continue; | |
1069 | } | |
1070 | ||
1071 | dev->up.n_rxq = n_rxq; | |
81acebda | 1072 | dev->up.n_txq = n_txq; |
b98d7669 DDP |
1073 | |
1074 | return 0; | |
1075 | } | |
1076 | ||
1077 | return diag; | |
1078 | } | |
1079 | ||
9fd39370 SC |
1080 | static void |
1081 | dpdk_eth_flow_ctrl_setup(struct netdev_dpdk *dev) OVS_REQUIRES(dev->mutex) | |
1082 | { | |
1083 | if (rte_eth_dev_flow_ctrl_set(dev->port_id, &dev->fc_conf)) { | |
fa9f4eeb | 1084 | VLOG_WARN("Failed to enable flow control on device "DPDK_PORT_ID_FMT, |
bb37956a | 1085 | dev->port_id); |
9fd39370 SC |
1086 | } |
1087 | } | |
b98d7669 | 1088 | |
8a9562d2 | 1089 | static int |
c2adb102 IM |
1090 | dpdk_eth_dev_init(struct netdev_dpdk *dev) |
1091 | OVS_REQUIRES(dev->mutex) | |
8a9562d2 PS |
1092 | { |
1093 | struct rte_pktmbuf_pool_private *mbp_priv; | |
a0cb2d66 | 1094 | struct rte_eth_dev_info info; |
127b6a6e | 1095 | struct rte_ether_addr eth_addr; |
8a9562d2 | 1096 | int diag; |
b98d7669 | 1097 | int n_rxq, n_txq; |
8c5163fe | 1098 | uint32_t tx_tso_offload_capa = DPDK_TX_TSO_OFFLOAD_FLAGS; |
d4f5282c KT |
1099 | uint32_t rx_chksm_offload_capa = DEV_RX_OFFLOAD_UDP_CKSUM | |
1100 | DEV_RX_OFFLOAD_TCP_CKSUM | | |
1101 | DEV_RX_OFFLOAD_IPV4_CKSUM; | |
8a9562d2 | 1102 | |
a0cb2d66 | 1103 | rte_eth_dev_info_get(dev->port_id, &info); |
a0cb2d66 | 1104 | |
e10ca8b9 MW |
1105 | if (strstr(info.driver_name, "vf") != NULL) { |
1106 | VLOG_INFO("Virtual function detected, HW_CRC_STRIP will be enabled"); | |
1107 | dev->hw_ol_features |= NETDEV_RX_HW_CRC_STRIP; | |
1108 | } else { | |
1109 | dev->hw_ol_features &= ~NETDEV_RX_HW_CRC_STRIP; | |
1110 | } | |
1111 | ||
d4f5282c KT |
1112 | if ((info.rx_offload_capa & rx_chksm_offload_capa) != |
1113 | rx_chksm_offload_capa) { | |
fa9f4eeb IM |
1114 | VLOG_WARN("Rx checksum offload is not supported on port " |
1115 | DPDK_PORT_ID_FMT, dev->port_id); | |
d4f5282c KT |
1116 | dev->hw_ol_features &= ~NETDEV_RX_CHECKSUM_OFFLOAD; |
1117 | } else { | |
1118 | dev->hw_ol_features |= NETDEV_RX_CHECKSUM_OFFLOAD; | |
1119 | } | |
1120 | ||
03f3f9c0 OM |
1121 | if (info.rx_offload_capa & DEV_RX_OFFLOAD_SCATTER) { |
1122 | dev->hw_ol_features |= NETDEV_RX_HW_SCATTER; | |
1123 | } else { | |
1124 | /* Do not warn on lack of scatter support */ | |
1125 | dev->hw_ol_features &= ~NETDEV_RX_HW_SCATTER; | |
1126 | } | |
1127 | ||
1223cf12 IM |
1128 | dev->hw_ol_features &= ~NETDEV_TX_TSO_OFFLOAD; |
1129 | if (userspace_tso_enabled()) { | |
1130 | if ((info.tx_offload_capa & tx_tso_offload_capa) | |
1131 | == tx_tso_offload_capa) { | |
1132 | dev->hw_ol_features |= NETDEV_TX_TSO_OFFLOAD; | |
35b5586b FL |
1133 | if (info.tx_offload_capa & DEV_TX_OFFLOAD_SCTP_CKSUM) { |
1134 | dev->hw_ol_features |= NETDEV_TX_SCTP_CHECKSUM_OFFLOAD; | |
1135 | } else { | |
1136 | VLOG_WARN("%s: Tx SCTP checksum offload is not supported, " | |
1137 | "SCTP packets sent to this device will be dropped", | |
1138 | netdev_get_name(&dev->up)); | |
1139 | } | |
1223cf12 IM |
1140 | } else { |
1141 | VLOG_WARN("%s: Tx TSO offload is not supported.", | |
1142 | netdev_get_name(&dev->up)); | |
1143 | } | |
29cf9c1b FL |
1144 | } |
1145 | ||
b98d7669 DDP |
1146 | n_rxq = MIN(info.max_rx_queues, dev->up.n_rxq); |
1147 | n_txq = MIN(info.max_tx_queues, dev->up.n_txq); | |
1148 | ||
f8b64a61 | 1149 | diag = dpdk_eth_dev_port_config(dev, n_rxq, n_txq); |
8a9562d2 | 1150 | if (diag) { |
f8b64a61 RM |
1151 | VLOG_ERR("Interface %s(rxq:%d txq:%d lsc interrupt mode:%s) " |
1152 | "configure error: %s", | |
1153 | dev->up.name, n_rxq, n_txq, | |
1154 | dev->lsc_interrupt_mode ? "true" : "false", | |
1155 | rte_strerror(-diag)); | |
95fb793a | 1156 | return -diag; |
8a9562d2 PS |
1157 | } |
1158 | ||
8a9562d2 PS |
1159 | diag = rte_eth_dev_start(dev->port_id); |
1160 | if (diag) { | |
b98d7669 DDP |
1161 | VLOG_ERR("Interface %s start error: %s", dev->up.name, |
1162 | rte_strerror(-diag)); | |
95fb793a | 1163 | return -diag; |
8a9562d2 | 1164 | } |
606f6650 | 1165 | dev->started = true; |
8a9562d2 PS |
1166 | |
1167 | rte_eth_promiscuous_enable(dev->port_id); | |
1168 | rte_eth_allmulticast_enable(dev->port_id); | |
1169 | ||
1170 | memset(ð_addr, 0x0, sizeof(eth_addr)); | |
1171 | rte_eth_macaddr_get(dev->port_id, ð_addr); | |
fa9f4eeb IM |
1172 | VLOG_INFO_RL(&rl, "Port "DPDK_PORT_ID_FMT": "ETH_ADDR_FMT, |
1173 | dev->port_id, ETH_ADDR_BYTES_ARGS(eth_addr.addr_bytes)); | |
8a9562d2 | 1174 | |
ca92d173 | 1175 | memcpy(dev->hwaddr.ea, eth_addr.addr_bytes, ETH_ADDR_LEN); |
8a9562d2 PS |
1176 | rte_eth_link_get_nowait(dev->port_id, &dev->link); |
1177 | ||
43307ad0 | 1178 | mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp); |
8a9562d2 | 1179 | dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM; |
8a9562d2 PS |
1180 | return 0; |
1181 | } | |
1182 | ||
1183 | static struct netdev_dpdk * | |
1184 | netdev_dpdk_cast(const struct netdev *netdev) | |
1185 | { | |
1186 | return CONTAINER_OF(netdev, struct netdev_dpdk, up); | |
1187 | } | |
1188 | ||
1189 | static struct netdev * | |
1190 | netdev_dpdk_alloc(void) | |
1191 | { | |
bab69409 AC |
1192 | struct netdev_dpdk *dev; |
1193 | ||
65e19e70 DDP |
1194 | dev = dpdk_rte_mzalloc(sizeof *dev); |
1195 | if (dev) { | |
1196 | return &dev->up; | |
bab69409 | 1197 | } |
65e19e70 | 1198 | |
bab69409 | 1199 | return NULL; |
8a9562d2 PS |
1200 | } |
1201 | ||
eff23640 DDP |
1202 | static struct dpdk_tx_queue * |
1203 | netdev_dpdk_alloc_txq(unsigned int n_txqs) | |
5a034064 | 1204 | { |
eff23640 | 1205 | struct dpdk_tx_queue *txqs; |
bd5131ba | 1206 | unsigned i; |
5a034064 | 1207 | |
eff23640 DDP |
1208 | txqs = dpdk_rte_mzalloc(n_txqs * sizeof *txqs); |
1209 | if (txqs) { | |
1210 | for (i = 0; i < n_txqs; i++) { | |
1211 | /* Initialize map for vhost devices. */ | |
1212 | txqs[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN; | |
1213 | rte_spinlock_init(&txqs[i].tx_lock); | |
1214 | } | |
5a034064 | 1215 | } |
eff23640 DDP |
1216 | |
1217 | return txqs; | |
5a034064 AW |
1218 | } |
1219 | ||
8a9562d2 | 1220 | static int |
bb37956a | 1221 | common_construct(struct netdev *netdev, dpdk_port_t port_no, |
1ce30dfd | 1222 | enum dpdk_dev_type type, int socket_id) |
5a034064 | 1223 | OVS_REQUIRES(dpdk_mutex) |
8a9562d2 | 1224 | { |
d46285a2 | 1225 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 | 1226 | |
d46285a2 | 1227 | ovs_mutex_init(&dev->mutex); |
8a9562d2 | 1228 | |
d46285a2 | 1229 | rte_spinlock_init(&dev->stats_lock); |
45d947c4 | 1230 | |
1b7a04e0 AW |
1231 | /* If the 'sid' is negative, it means that the kernel fails |
1232 | * to obtain the pci numa info. In that situation, always | |
1233 | * use 'SOCKET0'. */ | |
1ce30dfd | 1234 | dev->socket_id = socket_id < 0 ? SOCKET0 : socket_id; |
db8f13b0 | 1235 | dev->requested_socket_id = dev->socket_id; |
d46285a2 DDP |
1236 | dev->port_id = port_no; |
1237 | dev->type = type; | |
1238 | dev->flags = 0; | |
127b6a6e | 1239 | dev->requested_mtu = RTE_ETHER_MTU; |
d46285a2 | 1240 | dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu); |
f8b64a61 | 1241 | dev->requested_lsc_interrupt_mode = 0; |
0a0f39df CL |
1242 | ovsrcu_index_init(&dev->vid, -1); |
1243 | dev->vhost_reconfigured = false; | |
5dcde09c | 1244 | dev->attached = false; |
988fd463 EC |
1245 | dev->started = false; |
1246 | dev->reset_needed = false; | |
8a9562d2 | 1247 | |
78bd47cf | 1248 | ovsrcu_init(&dev->qos_conf, NULL); |
0bf765f7 | 1249 | |
9509913a IS |
1250 | ovsrcu_init(&dev->ingress_policer, NULL); |
1251 | dev->policer_rate = 0; | |
1252 | dev->policer_burst = 0; | |
1253 | ||
7f381c2e DDP |
1254 | netdev->n_rxq = 0; |
1255 | netdev->n_txq = 0; | |
1256 | dev->requested_n_rxq = NR_QUEUE; | |
1257 | dev->requested_n_txq = NR_QUEUE; | |
1258 | dev->requested_rxq_size = NIC_PORT_DEFAULT_RXQ_SIZE; | |
1259 | dev->requested_txq_size = NIC_PORT_DEFAULT_TXQ_SIZE; | |
58397e6c | 1260 | |
9fd39370 SC |
1261 | /* Initialize the flow control to NULL */ |
1262 | memset(&dev->fc_conf, 0, sizeof dev->fc_conf); | |
1a2bb118 SC |
1263 | |
1264 | /* Initilize the hardware offload flags to 0 */ | |
1265 | dev->hw_ol_features = 0; | |
3b1fb077 DDP |
1266 | |
1267 | dev->flags = NETDEV_UP | NETDEV_PROMISC; | |
1268 | ||
d46285a2 | 1269 | ovs_list_push_back(&dpdk_list, &dev->list_node); |
8a9562d2 | 1270 | |
7f381c2e DDP |
1271 | netdev_request_reconfigure(netdev); |
1272 | ||
971f4b39 MW |
1273 | dev->rte_xstats_names = NULL; |
1274 | dev->rte_xstats_names_size = 0; | |
1275 | ||
1276 | dev->rte_xstats_ids = NULL; | |
1277 | dev->rte_xstats_ids_size = 0; | |
1278 | ||
2f862c71 SV |
1279 | dev->sw_stats = xzalloc(sizeof *dev->sw_stats); |
1280 | dev->sw_stats->tx_retries = (dev->type == DPDK_DEV_VHOST) ? 0 : UINT64_MAX; | |
c161357d | 1281 | |
1ce30dfd | 1282 | return 0; |
95fb793a | 1283 | } |
1284 | ||
40e940e4 OM |
1285 | /* Get the number of OVS interfaces which have the same DPDK |
1286 | * rte device (e.g. same pci bus address). | |
1287 | * FIXME: avoid direct access to DPDK internal array rte_eth_devices. | |
1288 | */ | |
1289 | static int | |
1290 | netdev_dpdk_get_num_ports(struct rte_device *device) | |
1291 | OVS_REQUIRES(dpdk_mutex) | |
1292 | { | |
1293 | struct netdev_dpdk *dev; | |
1294 | int count = 0; | |
1295 | ||
1296 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { | |
1297 | if (rte_eth_devices[dev->port_id].device == device | |
1298 | && rte_eth_devices[dev->port_id].state != RTE_ETH_DEV_UNUSED) { | |
1299 | count++; | |
1300 | } | |
1301 | } | |
1302 | return count; | |
1303 | } | |
1304 | ||
1ce30dfd DDP |
1305 | static int |
1306 | vhost_common_construct(struct netdev *netdev) | |
1307 | OVS_REQUIRES(dpdk_mutex) | |
1308 | { | |
252e1e57 | 1309 | int socket_id = rte_lcore_to_socket_id(rte_get_main_lcore()); |
1ce30dfd DDP |
1310 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
1311 | ||
35c91567 DM |
1312 | dev->vhost_rxq_enabled = dpdk_rte_mzalloc(OVS_VHOST_MAX_QUEUE_NUM * |
1313 | sizeof *dev->vhost_rxq_enabled); | |
1314 | if (!dev->vhost_rxq_enabled) { | |
1315 | return ENOMEM; | |
1316 | } | |
1ce30dfd DDP |
1317 | dev->tx_q = netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM); |
1318 | if (!dev->tx_q) { | |
35c91567 | 1319 | rte_free(dev->vhost_rxq_enabled); |
1ce30dfd DDP |
1320 | return ENOMEM; |
1321 | } | |
1322 | ||
080f080c KT |
1323 | atomic_init(&dev->vhost_tx_retries_max, VHOST_ENQ_RETRY_DEF); |
1324 | ||
bb37956a IM |
1325 | return common_construct(netdev, DPDK_ETH_PORT_ID_INVALID, |
1326 | DPDK_DEV_VHOST, socket_id); | |
1ce30dfd DDP |
1327 | } |
1328 | ||
7d1ced01 | 1329 | static int |
53f50d24 | 1330 | netdev_dpdk_vhost_construct(struct netdev *netdev) |
7d1ced01 | 1331 | { |
d46285a2 DDP |
1332 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
1333 | const char *name = netdev->name; | |
7d1ced01 | 1334 | int err; |
a0cb2d66 | 1335 | |
1af27e8a DDP |
1336 | /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in |
1337 | * the file system. '/' or '\' would traverse directories, so they're not | |
1338 | * acceptable in 'name'. */ | |
1339 | if (strchr(name, '/') || strchr(name, '\\')) { | |
1340 | VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. " | |
1341 | "A valid name must not include '/' or '\\'", | |
1342 | name); | |
1343 | return EINVAL; | |
1344 | } | |
1345 | ||
7d1ced01 CL |
1346 | ovs_mutex_lock(&dpdk_mutex); |
1347 | /* Take the name of the vhost-user port and append it to the location where | |
2d24d165 | 1348 | * the socket is to be created, then register the socket. |
7d1ced01 | 1349 | */ |
bb9d2623 | 1350 | dev->vhost_id = xasprintf("%s/%s", dpdk_get_vhost_sock_dir(), name); |
1af27e8a | 1351 | |
2d24d165 | 1352 | dev->vhost_driver_flags &= ~RTE_VHOST_USER_CLIENT; |
e666e8e0 FL |
1353 | |
1354 | /* There is no support for multi-segments buffers. */ | |
1355 | dev->vhost_driver_flags |= RTE_VHOST_USER_LINEARBUF_SUPPORT; | |
2d24d165 | 1356 | err = rte_vhost_driver_register(dev->vhost_id, dev->vhost_driver_flags); |
7d1ced01 CL |
1357 | if (err) { |
1358 | VLOG_ERR("vhost-user socket device setup failure for socket %s\n", | |
2d24d165 | 1359 | dev->vhost_id); |
f3e7ec25 | 1360 | goto out; |
e5c0f5a4 | 1361 | } else { |
2d24d165 CL |
1362 | fatal_signal_add_file_to_unlink(dev->vhost_id); |
1363 | VLOG_INFO("Socket %s created for vhost-user port %s\n", | |
1364 | dev->vhost_id, name); | |
1365 | } | |
f3e7ec25 MW |
1366 | |
1367 | err = rte_vhost_driver_callback_register(dev->vhost_id, | |
1368 | &virtio_net_device_ops); | |
1369 | if (err) { | |
1370 | VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user " | |
1371 | "port: %s\n", name); | |
1372 | goto out; | |
1373 | } | |
1374 | ||
29cf9c1b FL |
1375 | if (!userspace_tso_enabled()) { |
1376 | err = rte_vhost_driver_disable_features(dev->vhost_id, | |
1377 | 1ULL << VIRTIO_NET_F_HOST_TSO4 | |
1378 | | 1ULL << VIRTIO_NET_F_HOST_TSO6 | |
1379 | | 1ULL << VIRTIO_NET_F_CSUM); | |
1380 | if (err) { | |
1381 | VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user " | |
1382 | "port: %s\n", name); | |
1383 | goto out; | |
1384 | } | |
f3e7ec25 MW |
1385 | } |
1386 | ||
1387 | err = rte_vhost_driver_start(dev->vhost_id); | |
1388 | if (err) { | |
1389 | VLOG_ERR("rte_vhost_driver_start failed for vhost user " | |
1390 | "port: %s\n", name); | |
1391 | goto out; | |
1392 | } | |
1393 | ||
1ce30dfd | 1394 | err = vhost_common_construct(netdev); |
f3e7ec25 MW |
1395 | if (err) { |
1396 | VLOG_ERR("vhost_common_construct failed for vhost user " | |
1397 | "port: %s\n", name); | |
1398 | } | |
2d24d165 | 1399 | |
f3e7ec25 | 1400 | out: |
bb9d2623 IM |
1401 | if (err) { |
1402 | free(dev->vhost_id); | |
1403 | dev->vhost_id = NULL; | |
1404 | } | |
1405 | ||
2d24d165 | 1406 | ovs_mutex_unlock(&dpdk_mutex); |
28ca969e AC |
1407 | VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; " |
1408 | "please migrate to dpdkvhostuserclient ports."); | |
2d24d165 CL |
1409 | return err; |
1410 | } | |
1411 | ||
1412 | static int | |
1413 | netdev_dpdk_vhost_client_construct(struct netdev *netdev) | |
1414 | { | |
1415 | int err; | |
1416 | ||
2d24d165 | 1417 | ovs_mutex_lock(&dpdk_mutex); |
1ce30dfd | 1418 | err = vhost_common_construct(netdev); |
f3e7ec25 MW |
1419 | if (err) { |
1420 | VLOG_ERR("vhost_common_construct failed for vhost user client" | |
1421 | "port: %s\n", netdev->name); | |
1422 | } | |
7d1ced01 | 1423 | ovs_mutex_unlock(&dpdk_mutex); |
58397e6c KT |
1424 | return err; |
1425 | } | |
1426 | ||
95fb793a | 1427 | static int |
1428 | netdev_dpdk_construct(struct netdev *netdev) | |
1429 | { | |
95fb793a | 1430 | int err; |
1431 | ||
95fb793a | 1432 | ovs_mutex_lock(&dpdk_mutex); |
bb37956a IM |
1433 | err = common_construct(netdev, DPDK_ETH_PORT_ID_INVALID, |
1434 | DPDK_DEV_ETH, SOCKET0); | |
8a9562d2 PS |
1435 | ovs_mutex_unlock(&dpdk_mutex); |
1436 | return err; | |
1437 | } | |
1438 | ||
1ce30dfd DDP |
1439 | static void |
1440 | common_destruct(struct netdev_dpdk *dev) | |
1441 | OVS_REQUIRES(dpdk_mutex) | |
1442 | OVS_EXCLUDED(dev->mutex) | |
1443 | { | |
1444 | rte_free(dev->tx_q); | |
43307ad0 | 1445 | dpdk_mp_put(dev->dpdk_mp); |
1ce30dfd DDP |
1446 | |
1447 | ovs_list_remove(&dev->list_node); | |
1448 | free(ovsrcu_get_protected(struct ingress_policer *, | |
1449 | &dev->ingress_policer)); | |
2f862c71 | 1450 | free(dev->sw_stats); |
1ce30dfd DDP |
1451 | ovs_mutex_destroy(&dev->mutex); |
1452 | } | |
1453 | ||
8a9562d2 | 1454 | static void |
d46285a2 | 1455 | netdev_dpdk_destruct(struct netdev *netdev) |
8a9562d2 | 1456 | { |
d46285a2 | 1457 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
40e940e4 OM |
1458 | struct rte_device *rte_dev; |
1459 | struct rte_eth_dev *eth_dev; | |
8a9562d2 | 1460 | |
8d38823b | 1461 | ovs_mutex_lock(&dpdk_mutex); |
8d38823b | 1462 | |
8a9562d2 | 1463 | rte_eth_dev_stop(dev->port_id); |
606f6650 | 1464 | dev->started = false; |
5dcde09c IM |
1465 | |
1466 | if (dev->attached) { | |
40e940e4 OM |
1467 | /* Retrieve eth device data before closing it. |
1468 | * FIXME: avoid direct access to DPDK internal array rte_eth_devices. | |
1469 | */ | |
1470 | eth_dev = &rte_eth_devices[dev->port_id]; | |
40e940e4 OM |
1471 | rte_dev = eth_dev->device; |
1472 | ||
1473 | /* Remove the eth device. */ | |
5dcde09c | 1474 | rte_eth_dev_close(dev->port_id); |
40e940e4 | 1475 | |
252e1e57 IS |
1476 | /* Remove this rte device and all its eth devices if all the eth |
1477 | * devices belonging to the rte device are closed. | |
40e940e4 | 1478 | */ |
252e1e57 | 1479 | if (!netdev_dpdk_get_num_ports(rte_dev)) { |
595ce47c IM |
1480 | int ret = rte_dev_remove(rte_dev); |
1481 | ||
1482 | if (ret < 0) { | |
1483 | VLOG_ERR("Device '%s' can not be detached: %s.", | |
1484 | dev->devargs, rte_strerror(-ret)); | |
40e940e4 OM |
1485 | } else { |
1486 | /* Device was closed and detached. */ | |
1487 | VLOG_INFO("Device '%s' has been removed and detached", | |
1488 | dev->devargs); | |
1489 | } | |
5dcde09c | 1490 | } else { |
40e940e4 OM |
1491 | /* Device was only closed. rte_dev_remove() was not called. */ |
1492 | VLOG_INFO("Device '%s' has been removed", dev->devargs); | |
5dcde09c IM |
1493 | } |
1494 | } | |
1495 | ||
ac1a9bb9 | 1496 | netdev_dpdk_clear_xstats(dev); |
55e075e6 | 1497 | free(dev->devargs); |
1ce30dfd | 1498 | common_destruct(dev); |
8d38823b | 1499 | |
8a9562d2 | 1500 | ovs_mutex_unlock(&dpdk_mutex); |
58397e6c | 1501 | } |
8a9562d2 | 1502 | |
3f891bbe DDP |
1503 | /* rte_vhost_driver_unregister() can call back destroy_device(), which will |
1504 | * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a | |
1505 | * deadlock, none of the mutexes must be held while calling this function. */ | |
1506 | static int | |
c1ff66ac CL |
1507 | dpdk_vhost_driver_unregister(struct netdev_dpdk *dev OVS_UNUSED, |
1508 | char *vhost_id) | |
3f891bbe DDP |
1509 | OVS_EXCLUDED(dpdk_mutex) |
1510 | OVS_EXCLUDED(dev->mutex) | |
1511 | { | |
c1ff66ac | 1512 | return rte_vhost_driver_unregister(vhost_id); |
3f891bbe DDP |
1513 | } |
1514 | ||
58397e6c | 1515 | static void |
d46285a2 | 1516 | netdev_dpdk_vhost_destruct(struct netdev *netdev) |
58397e6c | 1517 | { |
d46285a2 | 1518 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
c1ff66ac | 1519 | char *vhost_id; |
58397e6c | 1520 | |
8d38823b | 1521 | ovs_mutex_lock(&dpdk_mutex); |
8d38823b | 1522 | |
c62da695 | 1523 | /* Guest becomes an orphan if still attached. */ |
c1ff66ac CL |
1524 | if (netdev_dpdk_get_vid(dev) >= 0 |
1525 | && !(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) { | |
c62da695 | 1526 | VLOG_ERR("Removing port '%s' while vhost device still attached.", |
d46285a2 | 1527 | netdev->name); |
58be5c0e MK |
1528 | VLOG_ERR("To restore connectivity after re-adding of port, VM on " |
1529 | "socket '%s' must be restarted.", dev->vhost_id); | |
58397e6c KT |
1530 | } |
1531 | ||
bb9d2623 IM |
1532 | vhost_id = dev->vhost_id; |
1533 | dev->vhost_id = NULL; | |
35c91567 | 1534 | rte_free(dev->vhost_rxq_enabled); |
c1ff66ac | 1535 | |
1ce30dfd DDP |
1536 | common_destruct(dev); |
1537 | ||
58397e6c | 1538 | ovs_mutex_unlock(&dpdk_mutex); |
3f891bbe | 1539 | |
bb9d2623 | 1540 | if (!vhost_id) { |
821b8664 IM |
1541 | goto out; |
1542 | } | |
1543 | ||
c1ff66ac | 1544 | if (dpdk_vhost_driver_unregister(dev, vhost_id)) { |
41964543 IM |
1545 | VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n", |
1546 | netdev->name, vhost_id); | |
c1ff66ac CL |
1547 | } else if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) { |
1548 | /* OVS server mode - remove this socket from list for deletion */ | |
1549 | fatal_signal_remove_file_to_unlink(vhost_id); | |
3f891bbe | 1550 | } |
821b8664 | 1551 | out: |
c1ff66ac | 1552 | free(vhost_id); |
8a9562d2 PS |
1553 | } |
1554 | ||
1555 | static void | |
d46285a2 | 1556 | netdev_dpdk_dealloc(struct netdev *netdev) |
8a9562d2 | 1557 | { |
d46285a2 | 1558 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 | 1559 | |
d46285a2 | 1560 | rte_free(dev); |
8a9562d2 PS |
1561 | } |
1562 | ||
971f4b39 | 1563 | static void |
ac1a9bb9 | 1564 | netdev_dpdk_clear_xstats(struct netdev_dpdk *dev) |
971f4b39 MW |
1565 | { |
1566 | /* If statistics are already allocated, we have to | |
1567 | * reconfigure, as port_id could have been changed. */ | |
1568 | if (dev->rte_xstats_names) { | |
1569 | free(dev->rte_xstats_names); | |
1570 | dev->rte_xstats_names = NULL; | |
1571 | dev->rte_xstats_names_size = 0; | |
1572 | } | |
1573 | if (dev->rte_xstats_ids) { | |
1574 | free(dev->rte_xstats_ids); | |
1575 | dev->rte_xstats_ids = NULL; | |
1576 | dev->rte_xstats_ids_size = 0; | |
1577 | } | |
1578 | } | |
1579 | ||
1580 | static const char* | |
1581 | netdev_dpdk_get_xstat_name(struct netdev_dpdk *dev, uint64_t id) | |
1582 | { | |
1583 | if (id >= dev->rte_xstats_names_size) { | |
1584 | return "UNKNOWN"; | |
1585 | } | |
1586 | return dev->rte_xstats_names[id].name; | |
1587 | } | |
1588 | ||
1589 | static bool | |
1590 | netdev_dpdk_configure_xstats(struct netdev_dpdk *dev) | |
1591 | OVS_REQUIRES(dev->mutex) | |
1592 | { | |
1593 | int rte_xstats_len; | |
1594 | bool ret; | |
1595 | struct rte_eth_xstat *rte_xstats; | |
1596 | uint64_t id; | |
1597 | int xstats_no; | |
1598 | const char *name; | |
1599 | ||
1600 | /* Retrieving all XSTATS names. If something will go wrong | |
1601 | * or amount of counters will be equal 0, rte_xstats_names | |
1602 | * buffer will be marked as NULL, and any further xstats | |
1603 | * query won't be performed (e.g. during netdev_dpdk_get_stats | |
1604 | * execution). */ | |
1605 | ||
1606 | ret = false; | |
1607 | rte_xstats = NULL; | |
1608 | ||
1609 | if (dev->rte_xstats_names == NULL || dev->rte_xstats_ids == NULL) { | |
1610 | dev->rte_xstats_names_size = | |
1611 | rte_eth_xstats_get_names(dev->port_id, NULL, 0); | |
1612 | ||
1613 | if (dev->rte_xstats_names_size < 0) { | |
fa9f4eeb IM |
1614 | VLOG_WARN("Cannot get XSTATS for port: "DPDK_PORT_ID_FMT, |
1615 | dev->port_id); | |
971f4b39 MW |
1616 | dev->rte_xstats_names_size = 0; |
1617 | } else { | |
1618 | /* Reserve memory for xstats names and values */ | |
1619 | dev->rte_xstats_names = xcalloc(dev->rte_xstats_names_size, | |
1620 | sizeof *dev->rte_xstats_names); | |
1621 | ||
1622 | if (dev->rte_xstats_names) { | |
1623 | /* Retreive xstats names */ | |
1624 | rte_xstats_len = | |
1625 | rte_eth_xstats_get_names(dev->port_id, | |
1626 | dev->rte_xstats_names, | |
1627 | dev->rte_xstats_names_size); | |
1628 | ||
1629 | if (rte_xstats_len < 0) { | |
fa9f4eeb IM |
1630 | VLOG_WARN("Cannot get XSTATS names for port: " |
1631 | DPDK_PORT_ID_FMT, dev->port_id); | |
971f4b39 MW |
1632 | goto out; |
1633 | } else if (rte_xstats_len != dev->rte_xstats_names_size) { | |
fa9f4eeb IM |
1634 | VLOG_WARN("XSTATS size doesn't match for port: " |
1635 | DPDK_PORT_ID_FMT, dev->port_id); | |
971f4b39 MW |
1636 | goto out; |
1637 | } | |
1638 | ||
1639 | dev->rte_xstats_ids = xcalloc(dev->rte_xstats_names_size, | |
1640 | sizeof(uint64_t)); | |
1641 | ||
1642 | /* We have to calculate number of counters */ | |
1643 | rte_xstats = xmalloc(rte_xstats_len * sizeof *rte_xstats); | |
1644 | memset(rte_xstats, 0xff, sizeof *rte_xstats * rte_xstats_len); | |
1645 | ||
1646 | /* Retreive xstats values */ | |
1647 | if (rte_eth_xstats_get(dev->port_id, rte_xstats, | |
1648 | rte_xstats_len) > 0) { | |
1649 | dev->rte_xstats_ids_size = 0; | |
1650 | xstats_no = 0; | |
1651 | for (uint32_t i = 0; i < rte_xstats_len; i++) { | |
1652 | id = rte_xstats[i].id; | |
1653 | name = netdev_dpdk_get_xstat_name(dev, id); | |
1654 | /* We need to filter out everything except | |
1655 | * dropped, error and management counters */ | |
1656 | if (string_ends_with(name, "_errors") || | |
1657 | strstr(name, "_management_") || | |
1658 | string_ends_with(name, "_dropped")) { | |
1659 | ||
1660 | dev->rte_xstats_ids[xstats_no] = id; | |
1661 | xstats_no++; | |
1662 | } | |
1663 | } | |
1664 | dev->rte_xstats_ids_size = xstats_no; | |
1665 | ret = true; | |
1666 | } else { | |
fa9f4eeb IM |
1667 | VLOG_WARN("Can't get XSTATS IDs for port: " |
1668 | DPDK_PORT_ID_FMT, dev->port_id); | |
971f4b39 | 1669 | } |
34eb0863 IM |
1670 | |
1671 | free(rte_xstats); | |
971f4b39 MW |
1672 | } |
1673 | } | |
1674 | } else { | |
1675 | /* Already configured */ | |
1676 | ret = true; | |
1677 | } | |
1678 | ||
1679 | out: | |
1680 | if (!ret) { | |
1681 | netdev_dpdk_clear_xstats(dev); | |
1682 | } | |
1683 | return ret; | |
1684 | } | |
1685 | ||
f4336f50 GR |
1686 | static bool |
1687 | dpdk_port_is_representor(struct netdev_dpdk *dev) | |
1688 | OVS_REQUIRES(dev->mutex) | |
1689 | { | |
1690 | struct rte_eth_dev_info dev_info; | |
1691 | ||
1692 | rte_eth_dev_info_get(dev->port_id, &dev_info); | |
1693 | return (*dev_info.dev_flags) & RTE_ETH_DEV_REPRESENTOR; | |
1694 | } | |
1695 | ||
8a9562d2 | 1696 | static int |
a14b8947 | 1697 | netdev_dpdk_get_config(const struct netdev *netdev, struct smap *args) |
8a9562d2 | 1698 | { |
a14b8947 | 1699 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
1700 | |
1701 | ovs_mutex_lock(&dev->mutex); | |
1702 | ||
050c60bf | 1703 | smap_add_format(args, "requested_rx_queues", "%d", dev->requested_n_rxq); |
a14b8947 | 1704 | smap_add_format(args, "configured_rx_queues", "%d", netdev->n_rxq); |
81acebda IM |
1705 | smap_add_format(args, "requested_tx_queues", "%d", dev->requested_n_txq); |
1706 | smap_add_format(args, "configured_tx_queues", "%d", netdev->n_txq); | |
0072e931 | 1707 | smap_add_format(args, "mtu", "%d", dev->mtu); |
451f26fd IM |
1708 | |
1709 | if (dev->type == DPDK_DEV_ETH) { | |
1710 | smap_add_format(args, "requested_rxq_descriptors", "%d", | |
1711 | dev->requested_rxq_size); | |
1712 | smap_add_format(args, "configured_rxq_descriptors", "%d", | |
1713 | dev->rxq_size); | |
1714 | smap_add_format(args, "requested_txq_descriptors", "%d", | |
1715 | dev->requested_txq_size); | |
1716 | smap_add_format(args, "configured_txq_descriptors", "%d", | |
1717 | dev->txq_size); | |
1a2bb118 SC |
1718 | if (dev->hw_ol_features & NETDEV_RX_CHECKSUM_OFFLOAD) { |
1719 | smap_add(args, "rx_csum_offload", "true"); | |
8155ab7e KT |
1720 | } else { |
1721 | smap_add(args, "rx_csum_offload", "false"); | |
1a2bb118 | 1722 | } |
29cf9c1b FL |
1723 | if (dev->hw_ol_features & NETDEV_TX_TSO_OFFLOAD) { |
1724 | smap_add(args, "tx_tso_offload", "true"); | |
1725 | } else { | |
1726 | smap_add(args, "tx_tso_offload", "false"); | |
1727 | } | |
f8b64a61 RM |
1728 | smap_add(args, "lsc_interrupt_mode", |
1729 | dev->lsc_interrupt_mode ? "true" : "false"); | |
f4336f50 GR |
1730 | |
1731 | if (dpdk_port_is_representor(dev)) { | |
1732 | smap_add_format(args, "dpdk-vf-mac", ETH_ADDR_FMT, | |
1733 | ETH_ADDR_ARGS(dev->requested_hwaddr)); | |
1734 | } | |
451f26fd | 1735 | } |
8a9562d2 PS |
1736 | ovs_mutex_unlock(&dev->mutex); |
1737 | ||
1738 | return 0; | |
1739 | } | |
1740 | ||
55e075e6 | 1741 | static struct netdev_dpdk * |
bb37956a | 1742 | netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id) |
55e075e6 CL |
1743 | OVS_REQUIRES(dpdk_mutex) |
1744 | { | |
1745 | struct netdev_dpdk *dev; | |
1746 | ||
1747 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { | |
1748 | if (dev->port_id == port_id) { | |
1749 | return dev; | |
1750 | } | |
1751 | } | |
1752 | ||
1753 | return NULL; | |
1754 | } | |
1755 | ||
5e758818 YL |
1756 | static dpdk_port_t |
1757 | netdev_dpdk_get_port_by_mac(const char *mac_str) | |
1758 | { | |
1759 | dpdk_port_t port_id; | |
1760 | struct eth_addr mac, port_mac; | |
1761 | ||
1762 | if (!eth_addr_from_string(mac_str, &mac)) { | |
1763 | VLOG_ERR("invalid mac: %s", mac_str); | |
1764 | return DPDK_ETH_PORT_ID_INVALID; | |
1765 | } | |
1766 | ||
1767 | RTE_ETH_FOREACH_DEV (port_id) { | |
127b6a6e | 1768 | struct rte_ether_addr ea; |
5e758818 YL |
1769 | |
1770 | rte_eth_macaddr_get(port_id, &ea); | |
1771 | memcpy(port_mac.ea, ea.addr_bytes, ETH_ADDR_LEN); | |
1772 | if (eth_addr_equals(mac, port_mac)) { | |
1773 | return port_id; | |
1774 | } | |
1775 | } | |
1776 | ||
1777 | return DPDK_ETH_PORT_ID_INVALID; | |
1778 | } | |
1779 | ||
40e940e4 OM |
1780 | /* Return the first DPDK port id matching the devargs pattern. */ |
1781 | static dpdk_port_t netdev_dpdk_get_port_by_devargs(const char *devargs) | |
1782 | OVS_REQUIRES(dpdk_mutex) | |
1783 | { | |
1784 | dpdk_port_t port_id; | |
1785 | struct rte_dev_iterator iterator; | |
1786 | ||
1787 | RTE_ETH_FOREACH_MATCHING_DEV (port_id, devargs, &iterator) { | |
1788 | /* If a break is done - must call rte_eth_iterator_cleanup. */ | |
1789 | rte_eth_iterator_cleanup(&iterator); | |
1790 | break; | |
1791 | } | |
1792 | ||
1793 | return port_id; | |
1794 | } | |
1795 | ||
5e758818 | 1796 | /* |
40e940e4 OM |
1797 | * Normally, a PCI id (optionally followed by a representor number) |
1798 | * is enough for identifying a specific DPDK port. | |
5e758818 YL |
1799 | * However, for some NICs having multiple ports sharing the same PCI |
1800 | * id, using PCI id won't work then. | |
1801 | * | |
1802 | * To fix that, here one more method is introduced: "class=eth,mac=$MAC". | |
1803 | * | |
1804 | * Note that the compatibility is fully kept: user can still use the | |
1805 | * PCI id for adding ports (when it's enough for them). | |
1806 | */ | |
bb37956a | 1807 | static dpdk_port_t |
5dcde09c IM |
1808 | netdev_dpdk_process_devargs(struct netdev_dpdk *dev, |
1809 | const char *devargs, char **errp) | |
40e940e4 | 1810 | OVS_REQUIRES(dpdk_mutex) |
55e075e6 | 1811 | { |
40e940e4 | 1812 | dpdk_port_t new_port_id; |
55e075e6 | 1813 | |
5e758818 YL |
1814 | if (strncmp(devargs, "class=eth,mac=", 14) == 0) { |
1815 | new_port_id = netdev_dpdk_get_port_by_mac(&devargs[14]); | |
1816 | } else { | |
40e940e4 OM |
1817 | new_port_id = netdev_dpdk_get_port_by_devargs(devargs); |
1818 | if (!rte_eth_dev_is_valid_port(new_port_id)) { | |
5e758818 | 1819 | /* Device not found in DPDK, attempt to attach it */ |
40e940e4 | 1820 | if (rte_dev_probe(devargs)) { |
5e758818 | 1821 | new_port_id = DPDK_ETH_PORT_ID_INVALID; |
40e940e4 OM |
1822 | } else { |
1823 | new_port_id = netdev_dpdk_get_port_by_devargs(devargs); | |
1824 | if (rte_eth_dev_is_valid_port(new_port_id)) { | |
1825 | /* Attach successful */ | |
1826 | dev->attached = true; | |
1827 | VLOG_INFO("Device '%s' attached to DPDK", devargs); | |
1828 | } else { | |
1829 | /* Attach unsuccessful */ | |
1830 | new_port_id = DPDK_ETH_PORT_ID_INVALID; | |
1831 | } | |
5e758818 | 1832 | } |
55e075e6 | 1833 | } |
5e758818 YL |
1834 | } |
1835 | ||
1836 | if (new_port_id == DPDK_ETH_PORT_ID_INVALID) { | |
1837 | VLOG_WARN_BUF(errp, "Error attaching device '%s' to DPDK", devargs); | |
55e075e6 CL |
1838 | } |
1839 | ||
1840 | return new_port_id; | |
1841 | } | |
1842 | ||
988fd463 EC |
1843 | static int |
1844 | dpdk_eth_event_callback(dpdk_port_t port_id, enum rte_eth_event_type type, | |
1845 | void *param OVS_UNUSED, void *ret_param OVS_UNUSED) | |
1846 | { | |
1847 | struct netdev_dpdk *dev; | |
1848 | ||
1849 | switch ((int) type) { | |
1850 | case RTE_ETH_EVENT_INTR_RESET: | |
1851 | ovs_mutex_lock(&dpdk_mutex); | |
1852 | dev = netdev_dpdk_lookup_by_port_id(port_id); | |
1853 | if (dev) { | |
1854 | ovs_mutex_lock(&dev->mutex); | |
1855 | dev->reset_needed = true; | |
1856 | netdev_request_reconfigure(&dev->up); | |
1857 | VLOG_DBG_RL(&rl, "%s: Device reset requested.", | |
1858 | netdev_get_name(&dev->up)); | |
1859 | ovs_mutex_unlock(&dev->mutex); | |
1860 | } | |
1861 | ovs_mutex_unlock(&dpdk_mutex); | |
1862 | break; | |
1863 | ||
1864 | default: | |
1865 | /* Ignore all other types. */ | |
1866 | break; | |
1867 | } | |
1868 | return 0; | |
1869 | } | |
1870 | ||
c3d062a7 CL |
1871 | static void |
1872 | dpdk_set_rxq_config(struct netdev_dpdk *dev, const struct smap *args) | |
b614c894 | 1873 | OVS_REQUIRES(dev->mutex) |
a14b8947 | 1874 | { |
050c60bf | 1875 | int new_n_rxq; |
a14b8947 | 1876 | |
2a21e757 | 1877 | new_n_rxq = MAX(smap_get_int(args, "n_rxq", NR_QUEUE), 1); |
050c60bf DDP |
1878 | if (new_n_rxq != dev->requested_n_rxq) { |
1879 | dev->requested_n_rxq = new_n_rxq; | |
c3d062a7 | 1880 | netdev_request_reconfigure(&dev->up); |
050c60bf | 1881 | } |
c3d062a7 CL |
1882 | } |
1883 | ||
b685696b CL |
1884 | static void |
1885 | dpdk_process_queue_size(struct netdev *netdev, const struct smap *args, | |
1886 | const char *flag, int default_size, int *new_size) | |
1887 | { | |
1888 | int queue_size = smap_get_int(args, flag, default_size); | |
1889 | ||
1890 | if (queue_size <= 0 || queue_size > NIC_PORT_MAX_Q_SIZE | |
1891 | || !is_pow2(queue_size)) { | |
1892 | queue_size = default_size; | |
1893 | } | |
1894 | ||
1895 | if (queue_size != *new_size) { | |
1896 | *new_size = queue_size; | |
1897 | netdev_request_reconfigure(netdev); | |
1898 | } | |
1899 | } | |
1900 | ||
c3d062a7 | 1901 | static int |
9fff138e DDP |
1902 | netdev_dpdk_set_config(struct netdev *netdev, const struct smap *args, |
1903 | char **errp) | |
c3d062a7 CL |
1904 | { |
1905 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
f8b64a61 | 1906 | bool rx_fc_en, tx_fc_en, autoneg, lsc_interrupt_mode; |
c2c84474 | 1907 | bool flow_control_requested = true; |
b614c894 IM |
1908 | enum rte_eth_fc_mode fc_mode; |
1909 | static const enum rte_eth_fc_mode fc_mode_set[2][2] = { | |
1910 | {RTE_FC_NONE, RTE_FC_TX_PAUSE}, | |
1911 | {RTE_FC_RX_PAUSE, RTE_FC_FULL } | |
1912 | }; | |
55e075e6 | 1913 | const char *new_devargs; |
f4336f50 | 1914 | const char *vf_mac; |
55e075e6 | 1915 | int err = 0; |
c3d062a7 | 1916 | |
55e075e6 | 1917 | ovs_mutex_lock(&dpdk_mutex); |
c3d062a7 CL |
1918 | ovs_mutex_lock(&dev->mutex); |
1919 | ||
1920 | dpdk_set_rxq_config(dev, args); | |
1921 | ||
b685696b CL |
1922 | dpdk_process_queue_size(netdev, args, "n_rxq_desc", |
1923 | NIC_PORT_DEFAULT_RXQ_SIZE, | |
1924 | &dev->requested_rxq_size); | |
1925 | dpdk_process_queue_size(netdev, args, "n_txq_desc", | |
1926 | NIC_PORT_DEFAULT_TXQ_SIZE, | |
1927 | &dev->requested_txq_size); | |
1928 | ||
55e075e6 CL |
1929 | new_devargs = smap_get(args, "dpdk-devargs"); |
1930 | ||
cefdd80a | 1931 | if (dev->devargs && new_devargs && strcmp(new_devargs, dev->devargs)) { |
55e075e6 CL |
1932 | /* The user requested a new device. If we return error, the caller |
1933 | * will delete this netdev and try to recreate it. */ | |
1934 | err = EAGAIN; | |
1935 | goto out; | |
1936 | } | |
1937 | ||
1938 | /* dpdk-devargs is required for device configuration */ | |
1939 | if (new_devargs && new_devargs[0]) { | |
1940 | /* Don't process dpdk-devargs if value is unchanged and port id | |
1941 | * is valid */ | |
1942 | if (!(dev->devargs && !strcmp(dev->devargs, new_devargs) | |
1943 | && rte_eth_dev_is_valid_port(dev->port_id))) { | |
bb37956a IM |
1944 | dpdk_port_t new_port_id = netdev_dpdk_process_devargs(dev, |
1945 | new_devargs, | |
1946 | errp); | |
55e075e6 CL |
1947 | if (!rte_eth_dev_is_valid_port(new_port_id)) { |
1948 | err = EINVAL; | |
1949 | } else if (new_port_id == dev->port_id) { | |
1950 | /* Already configured, do not reconfigure again */ | |
1951 | err = 0; | |
1952 | } else { | |
1953 | struct netdev_dpdk *dup_dev; | |
bb37956a | 1954 | |
55e075e6 CL |
1955 | dup_dev = netdev_dpdk_lookup_by_port_id(new_port_id); |
1956 | if (dup_dev) { | |
9fff138e | 1957 | VLOG_WARN_BUF(errp, "'%s' is trying to use device '%s' " |
40e940e4 | 1958 | "which is already in use by '%s'", |
9fff138e DDP |
1959 | netdev_get_name(netdev), new_devargs, |
1960 | netdev_get_name(&dup_dev->up)); | |
55e075e6 CL |
1961 | err = EADDRINUSE; |
1962 | } else { | |
bd4e172b | 1963 | int sid = rte_eth_dev_socket_id(new_port_id); |
bb37956a | 1964 | |
bd4e172b | 1965 | dev->requested_socket_id = sid < 0 ? SOCKET0 : sid; |
55e075e6 CL |
1966 | dev->devargs = xstrdup(new_devargs); |
1967 | dev->port_id = new_port_id; | |
1968 | netdev_request_reconfigure(&dev->up); | |
971f4b39 | 1969 | netdev_dpdk_clear_xstats(dev); |
55e075e6 CL |
1970 | err = 0; |
1971 | } | |
1972 | } | |
1973 | } | |
1974 | } else { | |
9fff138e DDP |
1975 | VLOG_WARN_BUF(errp, "'%s' is missing 'options:dpdk-devargs'. " |
1976 | "The old 'dpdk<port_id>' names are not supported", | |
1977 | netdev_get_name(netdev)); | |
55e075e6 CL |
1978 | err = EINVAL; |
1979 | } | |
1980 | ||
1981 | if (err) { | |
1982 | goto out; | |
1983 | } | |
1984 | ||
f4336f50 GR |
1985 | vf_mac = smap_get(args, "dpdk-vf-mac"); |
1986 | if (vf_mac) { | |
1987 | struct eth_addr mac; | |
1988 | ||
1989 | if (!dpdk_port_is_representor(dev)) { | |
1990 | VLOG_WARN_BUF(errp, "'%s' is trying to set the VF MAC '%s' " | |
1991 | "but 'options:dpdk-vf-mac' is only supported for " | |
1992 | "VF representors.", | |
1993 | netdev_get_name(netdev), vf_mac); | |
1994 | } else if (!eth_addr_from_string(vf_mac, &mac)) { | |
1995 | VLOG_WARN_BUF(errp, "interface '%s': cannot parse VF MAC '%s'.", | |
1996 | netdev_get_name(netdev), vf_mac); | |
1997 | } else if (eth_addr_is_multicast(mac)) { | |
1998 | VLOG_WARN_BUF(errp, | |
1999 | "interface '%s': cannot set VF MAC to multicast " | |
2000 | "address '%s'.", netdev_get_name(netdev), vf_mac); | |
2001 | } else if (!eth_addr_equals(dev->requested_hwaddr, mac)) { | |
2002 | dev->requested_hwaddr = mac; | |
2003 | netdev_request_reconfigure(netdev); | |
2004 | } | |
2005 | } | |
2006 | ||
f8b64a61 RM |
2007 | lsc_interrupt_mode = smap_get_bool(args, "dpdk-lsc-interrupt", false); |
2008 | if (dev->requested_lsc_interrupt_mode != lsc_interrupt_mode) { | |
2009 | dev->requested_lsc_interrupt_mode = lsc_interrupt_mode; | |
2010 | netdev_request_reconfigure(netdev); | |
2011 | } | |
2012 | ||
c3d062a7 CL |
2013 | rx_fc_en = smap_get_bool(args, "rx-flow-ctrl", false); |
2014 | tx_fc_en = smap_get_bool(args, "tx-flow-ctrl", false); | |
b614c894 | 2015 | autoneg = smap_get_bool(args, "flow-ctrl-autoneg", false); |
c3d062a7 | 2016 | |
b614c894 | 2017 | fc_mode = fc_mode_set[tx_fc_en][rx_fc_en]; |
c2c84474 TK |
2018 | |
2019 | if (!smap_get(args, "rx-flow-ctrl") && !smap_get(args, "tx-flow-ctrl") | |
2020 | && !smap_get(args, "flow-ctrl-autoneg")) { | |
2021 | /* FIXME: User didn't ask for flow control configuration. | |
2022 | * For now we'll not print a warning if flow control is not | |
2023 | * supported by the DPDK port. */ | |
2024 | flow_control_requested = false; | |
2025 | } | |
2026 | ||
2027 | /* Get the Flow control configuration. */ | |
2028 | err = -rte_eth_dev_flow_ctrl_get(dev->port_id, &dev->fc_conf); | |
2029 | if (err) { | |
2030 | if (err == ENOTSUP) { | |
2031 | if (flow_control_requested) { | |
2032 | VLOG_WARN("%s: Flow control is not supported.", | |
2033 | netdev_get_name(netdev)); | |
2034 | } | |
2035 | err = 0; /* Not fatal. */ | |
2036 | } else { | |
2037 | VLOG_WARN("%s: Cannot get flow control parameters: %s", | |
2038 | netdev_get_name(netdev), rte_strerror(err)); | |
2039 | } | |
2040 | goto out; | |
2041 | } | |
2042 | ||
b614c894 IM |
2043 | if (dev->fc_conf.mode != fc_mode || autoneg != dev->fc_conf.autoneg) { |
2044 | dev->fc_conf.mode = fc_mode; | |
2045 | dev->fc_conf.autoneg = autoneg; | |
2046 | dpdk_eth_flow_ctrl_setup(dev); | |
2047 | } | |
9fd39370 | 2048 | |
55e075e6 | 2049 | out: |
c3d062a7 | 2050 | ovs_mutex_unlock(&dev->mutex); |
55e075e6 | 2051 | ovs_mutex_unlock(&dpdk_mutex); |
c3d062a7 | 2052 | |
55e075e6 | 2053 | return err; |
c3d062a7 CL |
2054 | } |
2055 | ||
c1ff66ac | 2056 | static int |
2d24d165 | 2057 | netdev_dpdk_vhost_client_set_config(struct netdev *netdev, |
9fff138e DDP |
2058 | const struct smap *args, |
2059 | char **errp OVS_UNUSED) | |
c1ff66ac CL |
2060 | { |
2061 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
2062 | const char *path; | |
080f080c | 2063 | int max_tx_retries, cur_max_tx_retries; |
c1ff66ac | 2064 | |
6881885a | 2065 | ovs_mutex_lock(&dev->mutex); |
c1ff66ac CL |
2066 | if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) { |
2067 | path = smap_get(args, "vhost-server-path"); | |
bb9d2623 IM |
2068 | if (!nullable_string_is_equal(path, dev->vhost_id)) { |
2069 | free(dev->vhost_id); | |
2070 | dev->vhost_id = nullable_xstrdup(path); | |
c1ff66ac CL |
2071 | netdev_request_reconfigure(netdev); |
2072 | } | |
2073 | } | |
080f080c KT |
2074 | |
2075 | max_tx_retries = smap_get_int(args, "tx-retries-max", | |
2076 | VHOST_ENQ_RETRY_DEF); | |
2077 | if (max_tx_retries < VHOST_ENQ_RETRY_MIN | |
2078 | || max_tx_retries > VHOST_ENQ_RETRY_MAX) { | |
2079 | max_tx_retries = VHOST_ENQ_RETRY_DEF; | |
2080 | } | |
2081 | atomic_read_relaxed(&dev->vhost_tx_retries_max, &cur_max_tx_retries); | |
2082 | if (max_tx_retries != cur_max_tx_retries) { | |
2083 | atomic_store_relaxed(&dev->vhost_tx_retries_max, max_tx_retries); | |
2084 | VLOG_INFO("Max Tx retries for vhost device '%s' set to %d", | |
2085 | netdev_get_name(netdev), max_tx_retries); | |
2086 | } | |
6881885a | 2087 | ovs_mutex_unlock(&dev->mutex); |
c1ff66ac CL |
2088 | |
2089 | return 0; | |
2090 | } | |
2091 | ||
7dec44fe | 2092 | static int |
d46285a2 | 2093 | netdev_dpdk_get_numa_id(const struct netdev *netdev) |
7dec44fe | 2094 | { |
d46285a2 | 2095 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
7dec44fe | 2096 | |
d46285a2 | 2097 | return dev->socket_id; |
7dec44fe AW |
2098 | } |
2099 | ||
050c60bf | 2100 | /* Sets the number of tx queues for the dpdk interface. */ |
5496878c | 2101 | static int |
050c60bf | 2102 | netdev_dpdk_set_tx_multiq(struct netdev *netdev, unsigned int n_txq) |
5496878c | 2103 | { |
d46285a2 | 2104 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
5496878c | 2105 | |
d46285a2 | 2106 | ovs_mutex_lock(&dev->mutex); |
91968eb0 | 2107 | |
050c60bf DDP |
2108 | if (dev->requested_n_txq == n_txq) { |
2109 | goto out; | |
4573fbd3 FL |
2110 | } |
2111 | ||
050c60bf DDP |
2112 | dev->requested_n_txq = n_txq; |
2113 | netdev_request_reconfigure(netdev); | |
58397e6c | 2114 | |
050c60bf | 2115 | out: |
d46285a2 | 2116 | ovs_mutex_unlock(&dev->mutex); |
050c60bf | 2117 | return 0; |
58397e6c KT |
2118 | } |
2119 | ||
8a9562d2 PS |
2120 | static struct netdev_rxq * |
2121 | netdev_dpdk_rxq_alloc(void) | |
2122 | { | |
2123 | struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx); | |
2124 | ||
eff23640 DDP |
2125 | if (rx) { |
2126 | return &rx->up; | |
2127 | } | |
2128 | ||
2129 | return NULL; | |
8a9562d2 PS |
2130 | } |
2131 | ||
2132 | static struct netdev_rxq_dpdk * | |
d46285a2 | 2133 | netdev_rxq_dpdk_cast(const struct netdev_rxq *rxq) |
8a9562d2 | 2134 | { |
d46285a2 | 2135 | return CONTAINER_OF(rxq, struct netdev_rxq_dpdk, up); |
8a9562d2 PS |
2136 | } |
2137 | ||
2138 | static int | |
d46285a2 | 2139 | netdev_dpdk_rxq_construct(struct netdev_rxq *rxq) |
8a9562d2 | 2140 | { |
d46285a2 DDP |
2141 | struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq); |
2142 | struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev); | |
8a9562d2 | 2143 | |
d46285a2 DDP |
2144 | ovs_mutex_lock(&dev->mutex); |
2145 | rx->port_id = dev->port_id; | |
2146 | ovs_mutex_unlock(&dev->mutex); | |
8a9562d2 PS |
2147 | |
2148 | return 0; | |
2149 | } | |
2150 | ||
2151 | static void | |
d46285a2 | 2152 | netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq OVS_UNUSED) |
8a9562d2 PS |
2153 | { |
2154 | } | |
2155 | ||
2156 | static void | |
d46285a2 | 2157 | netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq) |
8a9562d2 | 2158 | { |
d46285a2 | 2159 | struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq); |
8a9562d2 PS |
2160 | |
2161 | rte_free(rx); | |
2162 | } | |
2163 | ||
29cf9c1b FL |
2164 | /* Prepare the packet for HWOL. |
2165 | * Return True if the packet is OK to continue. */ | |
2166 | static bool | |
2167 | netdev_dpdk_prep_hwol_packet(struct netdev_dpdk *dev, struct rte_mbuf *mbuf) | |
2168 | { | |
2169 | struct dp_packet *pkt = CONTAINER_OF(mbuf, struct dp_packet, mbuf); | |
2170 | ||
2171 | if (mbuf->ol_flags & PKT_TX_L4_MASK) { | |
2172 | mbuf->l2_len = (char *)dp_packet_l3(pkt) - (char *)dp_packet_eth(pkt); | |
2173 | mbuf->l3_len = (char *)dp_packet_l4(pkt) - (char *)dp_packet_l3(pkt); | |
2174 | mbuf->outer_l2_len = 0; | |
2175 | mbuf->outer_l3_len = 0; | |
2176 | } | |
2177 | ||
2178 | if (mbuf->ol_flags & PKT_TX_TCP_SEG) { | |
2179 | struct tcp_header *th = dp_packet_l4(pkt); | |
2180 | ||
2181 | if (!th) { | |
2182 | VLOG_WARN_RL(&rl, "%s: TCP Segmentation without L4 header" | |
2183 | " pkt len: %"PRIu32"", dev->up.name, mbuf->pkt_len); | |
2184 | return false; | |
2185 | } | |
2186 | ||
2187 | mbuf->l4_len = TCP_OFFSET(th->tcp_ctl) * 4; | |
2188 | mbuf->ol_flags |= PKT_TX_TCP_CKSUM; | |
2189 | mbuf->tso_segsz = dev->mtu - mbuf->l3_len - mbuf->l4_len; | |
2190 | ||
2191 | if (mbuf->ol_flags & PKT_TX_IPV4) { | |
2192 | mbuf->ol_flags |= PKT_TX_IP_CKSUM; | |
2193 | } | |
2194 | } | |
2195 | return true; | |
2196 | } | |
2197 | ||
2198 | /* Prepare a batch for HWOL. | |
2199 | * Return the number of good packets in the batch. */ | |
2200 | static int | |
2201 | netdev_dpdk_prep_hwol_batch(struct netdev_dpdk *dev, struct rte_mbuf **pkts, | |
2202 | int pkt_cnt) | |
2203 | { | |
2204 | int i = 0; | |
2205 | int cnt = 0; | |
2206 | struct rte_mbuf *pkt; | |
2207 | ||
2208 | /* Prepare and filter bad HWOL packets. */ | |
2209 | for (i = 0; i < pkt_cnt; i++) { | |
2210 | pkt = pkts[i]; | |
2211 | if (!netdev_dpdk_prep_hwol_packet(dev, pkt)) { | |
2212 | rte_pktmbuf_free(pkt); | |
2213 | continue; | |
2214 | } | |
2215 | ||
2216 | if (OVS_UNLIKELY(i != cnt)) { | |
2217 | pkts[cnt] = pkt; | |
2218 | } | |
2219 | cnt++; | |
2220 | } | |
2221 | ||
2222 | return cnt; | |
2223 | } | |
2224 | ||
819f13bd DDP |
2225 | /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of |
2226 | * 'pkts', even in case of failure. | |
2227 | * | |
2228 | * Returns the number of packets that weren't transmitted. */ | |
2229 | static inline int | |
b59cc14e | 2230 | netdev_dpdk_eth_tx_burst(struct netdev_dpdk *dev, int qid, |
819f13bd | 2231 | struct rte_mbuf **pkts, int cnt) |
8a9562d2 | 2232 | { |
1304f1f8 | 2233 | uint32_t nb_tx = 0; |
29cf9c1b FL |
2234 | uint16_t nb_tx_prep = cnt; |
2235 | ||
2236 | if (userspace_tso_enabled()) { | |
2237 | nb_tx_prep = rte_eth_tx_prepare(dev->port_id, qid, pkts, cnt); | |
2238 | if (nb_tx_prep != cnt) { | |
2239 | VLOG_WARN_RL(&rl, "%s: Output batch contains invalid packets. " | |
2240 | "Only %u/%u are valid: %s", dev->up.name, nb_tx_prep, | |
2241 | cnt, rte_strerror(rte_errno)); | |
2242 | } | |
2243 | } | |
1304f1f8 | 2244 | |
29cf9c1b | 2245 | while (nb_tx != nb_tx_prep) { |
1304f1f8 DDP |
2246 | uint32_t ret; |
2247 | ||
29cf9c1b FL |
2248 | ret = rte_eth_tx_burst(dev->port_id, qid, pkts + nb_tx, |
2249 | nb_tx_prep - nb_tx); | |
1304f1f8 DDP |
2250 | if (!ret) { |
2251 | break; | |
2252 | } | |
2253 | ||
2254 | nb_tx += ret; | |
2255 | } | |
8a9562d2 | 2256 | |
b59cc14e | 2257 | if (OVS_UNLIKELY(nb_tx != cnt)) { |
819f13bd | 2258 | /* Free buffers, which we couldn't transmit, one at a time (each |
db73f716 DDP |
2259 | * packet could come from a different mempool) */ |
2260 | int i; | |
2261 | ||
b59cc14e IM |
2262 | for (i = nb_tx; i < cnt; i++) { |
2263 | rte_pktmbuf_free(pkts[i]); | |
db73f716 | 2264 | } |
8a9562d2 | 2265 | } |
819f13bd DDP |
2266 | |
2267 | return cnt - nb_tx; | |
8a9562d2 PS |
2268 | } |
2269 | ||
f3926f29 | 2270 | static inline bool |
e61bdffc EC |
2271 | netdev_dpdk_srtcm_policer_pkt_handle(struct rte_meter_srtcm *meter, |
2272 | struct rte_meter_srtcm_profile *profile, | |
2273 | struct rte_mbuf *pkt, uint64_t time) | |
f3926f29 | 2274 | { |
127b6a6e | 2275 | uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct rte_ether_hdr); |
f3926f29 | 2276 | |
03f3f9c0 | 2277 | return rte_meter_srtcm_color_blind_check(meter, profile, time, pkt_len) == |
127b6a6e | 2278 | RTE_COLOR_GREEN; |
f3926f29 IS |
2279 | } |
2280 | ||
2281 | static int | |
e61bdffc EC |
2282 | srtcm_policer_run_single_packet(struct rte_meter_srtcm *meter, |
2283 | struct rte_meter_srtcm_profile *profile, | |
2284 | struct rte_mbuf **pkts, int pkt_cnt, | |
2285 | bool should_steal) | |
f3926f29 IS |
2286 | { |
2287 | int i = 0; | |
2288 | int cnt = 0; | |
2289 | struct rte_mbuf *pkt = NULL; | |
2290 | uint64_t current_time = rte_rdtsc(); | |
2291 | ||
2292 | for (i = 0; i < pkt_cnt; i++) { | |
2293 | pkt = pkts[i]; | |
2294 | /* Handle current packet */ | |
e61bdffc EC |
2295 | if (netdev_dpdk_srtcm_policer_pkt_handle(meter, profile, |
2296 | pkt, current_time)) { | |
f3926f29 IS |
2297 | if (cnt != i) { |
2298 | pkts[cnt] = pkt; | |
2299 | } | |
2300 | cnt++; | |
2301 | } else { | |
7d7ded7a | 2302 | if (should_steal) { |
3e90f7d7 GZ |
2303 | rte_pktmbuf_free(pkt); |
2304 | } | |
f3926f29 IS |
2305 | } |
2306 | } | |
2307 | ||
2308 | return cnt; | |
2309 | } | |
2310 | ||
9509913a IS |
2311 | static int |
2312 | ingress_policer_run(struct ingress_policer *policer, struct rte_mbuf **pkts, | |
7d7ded7a | 2313 | int pkt_cnt, bool should_steal) |
9509913a IS |
2314 | { |
2315 | int cnt = 0; | |
2316 | ||
2317 | rte_spinlock_lock(&policer->policer_lock); | |
e61bdffc EC |
2318 | cnt = srtcm_policer_run_single_packet(&policer->in_policer, |
2319 | &policer->in_prof, | |
2320 | pkts, pkt_cnt, should_steal); | |
9509913a IS |
2321 | rte_spinlock_unlock(&policer->policer_lock); |
2322 | ||
2323 | return cnt; | |
2324 | } | |
2325 | ||
58397e6c | 2326 | static bool |
0a0f39df | 2327 | is_vhost_running(struct netdev_dpdk *dev) |
58397e6c | 2328 | { |
0a0f39df | 2329 | return (netdev_dpdk_get_vid(dev) >= 0 && dev->vhost_reconfigured); |
58397e6c KT |
2330 | } |
2331 | ||
d6e3feb5 | 2332 | static inline void |
2333 | netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats *stats, | |
2334 | unsigned int packet_size) | |
2335 | { | |
2336 | /* Hard-coded search for the size bucket. */ | |
2337 | if (packet_size < 256) { | |
2338 | if (packet_size >= 128) { | |
2339 | stats->rx_128_to_255_packets++; | |
2340 | } else if (packet_size <= 64) { | |
2341 | stats->rx_1_to_64_packets++; | |
2342 | } else { | |
2343 | stats->rx_65_to_127_packets++; | |
2344 | } | |
2345 | } else { | |
2346 | if (packet_size >= 1523) { | |
2347 | stats->rx_1523_to_max_packets++; | |
2348 | } else if (packet_size >= 1024) { | |
2349 | stats->rx_1024_to_1522_packets++; | |
2350 | } else if (packet_size < 512) { | |
2351 | stats->rx_256_to_511_packets++; | |
2352 | } else { | |
2353 | stats->rx_512_to_1023_packets++; | |
2354 | } | |
2355 | } | |
2356 | } | |
2357 | ||
9e3ddd45 | 2358 | static inline void |
2f862c71 | 2359 | netdev_dpdk_vhost_update_rx_counters(struct netdev_dpdk *dev, |
9509913a | 2360 | struct dp_packet **packets, int count, |
2f862c71 | 2361 | int qos_drops) |
9e3ddd45 | 2362 | { |
2f862c71 | 2363 | struct netdev_stats *stats = &dev->stats; |
9e3ddd45 | 2364 | struct dp_packet *packet; |
2f862c71 SV |
2365 | unsigned int packet_size; |
2366 | int i; | |
9e3ddd45 TP |
2367 | |
2368 | stats->rx_packets += count; | |
2f862c71 | 2369 | stats->rx_dropped += qos_drops; |
9e3ddd45 TP |
2370 | for (i = 0; i < count; i++) { |
2371 | packet = packets[i]; | |
d6e3feb5 | 2372 | packet_size = dp_packet_size(packet); |
9e3ddd45 | 2373 | |
d6e3feb5 | 2374 | if (OVS_UNLIKELY(packet_size < ETH_HEADER_LEN)) { |
9e3ddd45 TP |
2375 | /* This only protects the following multicast counting from |
2376 | * too short packets, but it does not stop the packet from | |
2377 | * further processing. */ | |
2378 | stats->rx_errors++; | |
2379 | stats->rx_length_errors++; | |
2380 | continue; | |
2381 | } | |
2382 | ||
d6e3feb5 | 2383 | netdev_dpdk_vhost_update_rx_size_counters(stats, packet_size); |
2384 | ||
9e3ddd45 TP |
2385 | struct eth_header *eh = (struct eth_header *) dp_packet_data(packet); |
2386 | if (OVS_UNLIKELY(eth_addr_is_multicast(eh->eth_dst))) { | |
2387 | stats->multicast++; | |
2388 | } | |
2389 | ||
d6e3feb5 | 2390 | stats->rx_bytes += packet_size; |
9e3ddd45 | 2391 | } |
2f862c71 | 2392 | |
6d77abf4 KT |
2393 | if (OVS_UNLIKELY(qos_drops)) { |
2394 | dev->sw_stats->rx_qos_drops += qos_drops; | |
2395 | } | |
9e3ddd45 TP |
2396 | } |
2397 | ||
58397e6c KT |
2398 | /* |
2399 | * The receive path for the vhost port is the TX path out from guest. | |
2400 | */ | |
2401 | static int | |
d46285a2 | 2402 | netdev_dpdk_vhost_rxq_recv(struct netdev_rxq *rxq, |
8492adc2 | 2403 | struct dp_packet_batch *batch, int *qfill) |
58397e6c | 2404 | { |
d46285a2 | 2405 | struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev); |
9509913a | 2406 | struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev); |
58397e6c | 2407 | uint16_t nb_rx = 0; |
2f862c71 | 2408 | uint16_t qos_drops = 0; |
8492adc2 | 2409 | int qid = rxq->queue_id * VIRTIO_QNUM + VIRTIO_TXQ; |
daf22bf7 | 2410 | int vid = netdev_dpdk_get_vid(dev); |
58397e6c | 2411 | |
daf22bf7 | 2412 | if (OVS_UNLIKELY(vid < 0 || !dev->vhost_reconfigured |
e543851d | 2413 | || !(dev->flags & NETDEV_UP))) { |
58397e6c KT |
2414 | return EAGAIN; |
2415 | } | |
2416 | ||
43307ad0 | 2417 | nb_rx = rte_vhost_dequeue_burst(vid, qid, dev->dpdk_mp->mp, |
64839cf4 | 2418 | (struct rte_mbuf **) batch->packets, |
cd159f1a | 2419 | NETDEV_MAX_BURST); |
58397e6c KT |
2420 | if (!nb_rx) { |
2421 | return EAGAIN; | |
2422 | } | |
2423 | ||
8492adc2 JS |
2424 | if (qfill) { |
2425 | if (nb_rx == NETDEV_MAX_BURST) { | |
2426 | /* The DPDK API returns a uint32_t which often has invalid bits in | |
2427 | * the upper 16-bits. Need to restrict the value to uint16_t. */ | |
2428 | *qfill = rte_vhost_rx_queue_count(vid, qid) & UINT16_MAX; | |
2429 | } else { | |
2430 | *qfill = 0; | |
2431 | } | |
2432 | } | |
2433 | ||
9509913a | 2434 | if (policer) { |
2f862c71 | 2435 | qos_drops = nb_rx; |
64839cf4 WT |
2436 | nb_rx = ingress_policer_run(policer, |
2437 | (struct rte_mbuf **) batch->packets, | |
3e90f7d7 | 2438 | nb_rx, true); |
2f862c71 | 2439 | qos_drops -= nb_rx; |
9509913a IS |
2440 | } |
2441 | ||
d46285a2 | 2442 | rte_spinlock_lock(&dev->stats_lock); |
2f862c71 SV |
2443 | netdev_dpdk_vhost_update_rx_counters(dev, batch->packets, |
2444 | nb_rx, qos_drops); | |
d46285a2 | 2445 | rte_spinlock_unlock(&dev->stats_lock); |
45d947c4 | 2446 | |
75fb9148 ZB |
2447 | batch->count = nb_rx; |
2448 | dp_packet_batch_init_packet_fields(batch); | |
2449 | ||
58397e6c KT |
2450 | return 0; |
2451 | } | |
2452 | ||
35c91567 DM |
2453 | static bool |
2454 | netdev_dpdk_vhost_rxq_enabled(struct netdev_rxq *rxq) | |
2455 | { | |
2456 | struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev); | |
2457 | ||
2458 | return dev->vhost_rxq_enabled[rxq->queue_id]; | |
2459 | } | |
2460 | ||
8a9562d2 | 2461 | static int |
8492adc2 JS |
2462 | netdev_dpdk_rxq_recv(struct netdev_rxq *rxq, struct dp_packet_batch *batch, |
2463 | int *qfill) | |
8a9562d2 | 2464 | { |
d46285a2 DDP |
2465 | struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq); |
2466 | struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev); | |
9509913a | 2467 | struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev); |
8a9562d2 | 2468 | int nb_rx; |
9509913a | 2469 | int dropped = 0; |
8a9562d2 | 2470 | |
3b1fb077 DDP |
2471 | if (OVS_UNLIKELY(!(dev->flags & NETDEV_UP))) { |
2472 | return EAGAIN; | |
2473 | } | |
2474 | ||
d46285a2 | 2475 | nb_rx = rte_eth_rx_burst(rx->port_id, rxq->queue_id, |
64839cf4 | 2476 | (struct rte_mbuf **) batch->packets, |
cd159f1a | 2477 | NETDEV_MAX_BURST); |
8a9562d2 PS |
2478 | if (!nb_rx) { |
2479 | return EAGAIN; | |
2480 | } | |
2481 | ||
9509913a IS |
2482 | if (policer) { |
2483 | dropped = nb_rx; | |
64839cf4 | 2484 | nb_rx = ingress_policer_run(policer, |
58be5c0e | 2485 | (struct rte_mbuf **) batch->packets, |
3e90f7d7 | 2486 | nb_rx, true); |
9509913a IS |
2487 | dropped -= nb_rx; |
2488 | } | |
2489 | ||
2490 | /* Update stats to reflect dropped packets */ | |
2491 | if (OVS_UNLIKELY(dropped)) { | |
2492 | rte_spinlock_lock(&dev->stats_lock); | |
2493 | dev->stats.rx_dropped += dropped; | |
2f862c71 | 2494 | dev->sw_stats->rx_qos_drops += dropped; |
9509913a IS |
2495 | rte_spinlock_unlock(&dev->stats_lock); |
2496 | } | |
2497 | ||
64839cf4 | 2498 | batch->count = nb_rx; |
75fb9148 | 2499 | dp_packet_batch_init_packet_fields(batch); |
8a9562d2 | 2500 | |
8492adc2 JS |
2501 | if (qfill) { |
2502 | if (nb_rx == NETDEV_MAX_BURST) { | |
2503 | *qfill = rte_eth_rx_queue_count(rx->port_id, rxq->queue_id); | |
2504 | } else { | |
2505 | *qfill = 0; | |
2506 | } | |
2507 | } | |
2508 | ||
8a9562d2 PS |
2509 | return 0; |
2510 | } | |
2511 | ||
0bf765f7 | 2512 | static inline int |
78bd47cf | 2513 | netdev_dpdk_qos_run(struct netdev_dpdk *dev, struct rte_mbuf **pkts, |
7d7ded7a | 2514 | int cnt, bool should_steal) |
0bf765f7 | 2515 | { |
78bd47cf | 2516 | struct qos_conf *qos_conf = ovsrcu_get(struct qos_conf *, &dev->qos_conf); |
0bf765f7 | 2517 | |
78bd47cf DDP |
2518 | if (qos_conf) { |
2519 | rte_spinlock_lock(&qos_conf->lock); | |
7d7ded7a | 2520 | cnt = qos_conf->ops->qos_run(qos_conf, pkts, cnt, should_steal); |
78bd47cf | 2521 | rte_spinlock_unlock(&qos_conf->lock); |
0bf765f7 IS |
2522 | } |
2523 | ||
2524 | return cnt; | |
2525 | } | |
2526 | ||
c6ec9d17 IM |
2527 | static int |
2528 | netdev_dpdk_filter_packet_len(struct netdev_dpdk *dev, struct rte_mbuf **pkts, | |
2529 | int pkt_cnt) | |
2530 | { | |
2531 | int i = 0; | |
2532 | int cnt = 0; | |
2533 | struct rte_mbuf *pkt; | |
2534 | ||
29cf9c1b | 2535 | /* Filter oversized packets, unless are marked for TSO. */ |
c6ec9d17 IM |
2536 | for (i = 0; i < pkt_cnt; i++) { |
2537 | pkt = pkts[i]; | |
29cf9c1b FL |
2538 | if (OVS_UNLIKELY((pkt->pkt_len > dev->max_packet_len) |
2539 | && !(pkt->ol_flags & PKT_TX_TCP_SEG))) { | |
2540 | VLOG_WARN_RL(&rl, "%s: Too big size %" PRIu32 " " | |
2541 | "max_packet_len %d", dev->up.name, pkt->pkt_len, | |
2542 | dev->max_packet_len); | |
c6ec9d17 IM |
2543 | rte_pktmbuf_free(pkt); |
2544 | continue; | |
2545 | } | |
2546 | ||
2547 | if (OVS_UNLIKELY(i != cnt)) { | |
2548 | pkts[cnt] = pkt; | |
2549 | } | |
2550 | cnt++; | |
2551 | } | |
2552 | ||
2553 | return cnt; | |
2554 | } | |
2555 | ||
9e3ddd45 | 2556 | static inline void |
2f862c71 | 2557 | netdev_dpdk_vhost_update_tx_counters(struct netdev_dpdk *dev, |
9e3ddd45 TP |
2558 | struct dp_packet **packets, |
2559 | int attempted, | |
2f862c71 | 2560 | struct netdev_dpdk_sw_stats *sw_stats_add) |
9e3ddd45 | 2561 | { |
2f862c71 SV |
2562 | int dropped = sw_stats_add->tx_mtu_exceeded_drops + |
2563 | sw_stats_add->tx_qos_drops + | |
29cf9c1b FL |
2564 | sw_stats_add->tx_failure_drops + |
2565 | sw_stats_add->tx_invalid_hwol_drops; | |
2f862c71 | 2566 | struct netdev_stats *stats = &dev->stats; |
9e3ddd45 | 2567 | int sent = attempted - dropped; |
2f862c71 | 2568 | int i; |
9e3ddd45 TP |
2569 | |
2570 | stats->tx_packets += sent; | |
2571 | stats->tx_dropped += dropped; | |
2572 | ||
2573 | for (i = 0; i < sent; i++) { | |
2574 | stats->tx_bytes += dp_packet_size(packets[i]); | |
2575 | } | |
2f862c71 | 2576 | |
6d77abf4 KT |
2577 | if (OVS_UNLIKELY(dropped || sw_stats_add->tx_retries)) { |
2578 | struct netdev_dpdk_sw_stats *sw_stats = dev->sw_stats; | |
2579 | ||
2580 | sw_stats->tx_retries += sw_stats_add->tx_retries; | |
2581 | sw_stats->tx_failure_drops += sw_stats_add->tx_failure_drops; | |
2582 | sw_stats->tx_mtu_exceeded_drops += sw_stats_add->tx_mtu_exceeded_drops; | |
2583 | sw_stats->tx_qos_drops += sw_stats_add->tx_qos_drops; | |
29cf9c1b | 2584 | sw_stats->tx_invalid_hwol_drops += sw_stats_add->tx_invalid_hwol_drops; |
6d77abf4 | 2585 | } |
9e3ddd45 TP |
2586 | } |
2587 | ||
58397e6c | 2588 | static void |
4573fbd3 | 2589 | __netdev_dpdk_vhost_send(struct netdev *netdev, int qid, |
dd52de45 | 2590 | struct dp_packet **pkts, int cnt) |
58397e6c | 2591 | { |
d46285a2 | 2592 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
95e9881f | 2593 | struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts; |
2f862c71 SV |
2594 | struct netdev_dpdk_sw_stats sw_stats_add; |
2595 | unsigned int n_packets_to_free = cnt; | |
2596 | unsigned int total_packets = cnt; | |
dd52de45 | 2597 | int i, retries = 0; |
080f080c | 2598 | int max_retries = VHOST_ENQ_RETRY_MIN; |
daf22bf7 | 2599 | int vid = netdev_dpdk_get_vid(dev); |
58397e6c | 2600 | |
81acebda | 2601 | qid = dev->tx_q[qid % netdev->n_txq].map; |
585a5bea | 2602 | |
daf22bf7 | 2603 | if (OVS_UNLIKELY(vid < 0 || !dev->vhost_reconfigured || qid < 0 |
e543851d | 2604 | || !(dev->flags & NETDEV_UP))) { |
d46285a2 DDP |
2605 | rte_spinlock_lock(&dev->stats_lock); |
2606 | dev->stats.tx_dropped+= cnt; | |
2607 | rte_spinlock_unlock(&dev->stats_lock); | |
1b99bb05 | 2608 | goto out; |
58397e6c KT |
2609 | } |
2610 | ||
9ff24b9c DM |
2611 | if (OVS_UNLIKELY(!rte_spinlock_trylock(&dev->tx_q[qid].tx_lock))) { |
2612 | COVERAGE_INC(vhost_tx_contention); | |
2613 | rte_spinlock_lock(&dev->tx_q[qid].tx_lock); | |
2614 | } | |
58397e6c | 2615 | |
29cf9c1b FL |
2616 | sw_stats_add.tx_invalid_hwol_drops = cnt; |
2617 | if (userspace_tso_enabled()) { | |
2618 | cnt = netdev_dpdk_prep_hwol_batch(dev, cur_pkts, cnt); | |
2619 | } | |
2620 | ||
2621 | sw_stats_add.tx_invalid_hwol_drops -= cnt; | |
2622 | sw_stats_add.tx_mtu_exceeded_drops = cnt; | |
c6ec9d17 | 2623 | cnt = netdev_dpdk_filter_packet_len(dev, cur_pkts, cnt); |
29cf9c1b | 2624 | sw_stats_add.tx_mtu_exceeded_drops -= cnt; |
2f862c71 | 2625 | |
0bf765f7 | 2626 | /* Check has QoS has been configured for the netdev */ |
2f862c71 | 2627 | sw_stats_add.tx_qos_drops = cnt; |
3e90f7d7 | 2628 | cnt = netdev_dpdk_qos_run(dev, cur_pkts, cnt, true); |
2f862c71 SV |
2629 | sw_stats_add.tx_qos_drops -= cnt; |
2630 | ||
2631 | n_packets_to_free = cnt; | |
0bf765f7 | 2632 | |
95e9881f | 2633 | do { |
4573fbd3 | 2634 | int vhost_qid = qid * VIRTIO_QNUM + VIRTIO_RXQ; |
95e9881f KT |
2635 | unsigned int tx_pkts; |
2636 | ||
daf22bf7 | 2637 | tx_pkts = rte_vhost_enqueue_burst(vid, vhost_qid, cur_pkts, cnt); |
95e9881f KT |
2638 | if (OVS_LIKELY(tx_pkts)) { |
2639 | /* Packets have been sent.*/ | |
2640 | cnt -= tx_pkts; | |
31871ee3 | 2641 | /* Prepare for possible retry.*/ |
95e9881f | 2642 | cur_pkts = &cur_pkts[tx_pkts]; |
080f080c KT |
2643 | if (OVS_UNLIKELY(cnt && !retries)) { |
2644 | /* | |
2645 | * Read max retries as there are packets not sent | |
2646 | * and no retries have already occurred. | |
2647 | */ | |
2648 | atomic_read_relaxed(&dev->vhost_tx_retries_max, &max_retries); | |
2649 | } | |
95e9881f | 2650 | } else { |
31871ee3 KT |
2651 | /* No packets sent - do not retry.*/ |
2652 | break; | |
95e9881f | 2653 | } |
080f080c | 2654 | } while (cnt && (retries++ < max_retries)); |
4573fbd3 | 2655 | |
d46285a2 | 2656 | rte_spinlock_unlock(&dev->tx_q[qid].tx_lock); |
95e9881f | 2657 | |
2f862c71 SV |
2658 | sw_stats_add.tx_failure_drops = cnt; |
2659 | sw_stats_add.tx_retries = MIN(retries, max_retries); | |
2660 | ||
d46285a2 | 2661 | rte_spinlock_lock(&dev->stats_lock); |
2f862c71 SV |
2662 | netdev_dpdk_vhost_update_tx_counters(dev, pkts, total_packets, |
2663 | &sw_stats_add); | |
d46285a2 | 2664 | rte_spinlock_unlock(&dev->stats_lock); |
58397e6c KT |
2665 | |
2666 | out: | |
2f862c71 | 2667 | for (i = 0; i < n_packets_to_free; i++) { |
dd52de45 | 2668 | dp_packet_delete(pkts[i]); |
58397e6c KT |
2669 | } |
2670 | } | |
2671 | ||
29cf9c1b FL |
2672 | static void |
2673 | netdev_dpdk_extbuf_free(void *addr OVS_UNUSED, void *opaque) | |
2674 | { | |
2675 | rte_free(opaque); | |
2676 | } | |
2677 | ||
2678 | static struct rte_mbuf * | |
2679 | dpdk_pktmbuf_attach_extbuf(struct rte_mbuf *pkt, uint32_t data_len) | |
2680 | { | |
2681 | uint32_t total_len = RTE_PKTMBUF_HEADROOM + data_len; | |
2682 | struct rte_mbuf_ext_shared_info *shinfo = NULL; | |
2683 | uint16_t buf_len; | |
2684 | void *buf; | |
2685 | ||
c17f32a1 YY |
2686 | total_len += sizeof *shinfo + sizeof(uintptr_t); |
2687 | total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t)); | |
29cf9c1b FL |
2688 | |
2689 | if (OVS_UNLIKELY(total_len > UINT16_MAX)) { | |
2690 | VLOG_ERR("Can't copy packet: too big %u", total_len); | |
2691 | return NULL; | |
2692 | } | |
2693 | ||
2694 | buf_len = total_len; | |
2695 | buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE); | |
2696 | if (OVS_UNLIKELY(buf == NULL)) { | |
2697 | VLOG_ERR("Failed to allocate memory using rte_malloc: %u", buf_len); | |
2698 | return NULL; | |
2699 | } | |
2700 | ||
2701 | /* Initialize shinfo. */ | |
c17f32a1 YY |
2702 | shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len, |
2703 | netdev_dpdk_extbuf_free, | |
2704 | buf); | |
2705 | if (OVS_UNLIKELY(shinfo == NULL)) { | |
2706 | rte_free(buf); | |
2707 | VLOG_ERR("Failed to initialize shared info for mbuf while " | |
2708 | "attempting to attach an external buffer."); | |
2709 | return NULL; | |
29cf9c1b FL |
2710 | } |
2711 | ||
2712 | rte_pktmbuf_attach_extbuf(pkt, buf, rte_malloc_virt2iova(buf), buf_len, | |
2713 | shinfo); | |
2714 | rte_pktmbuf_reset_headroom(pkt); | |
2715 | ||
2716 | return pkt; | |
2717 | } | |
2718 | ||
2719 | static struct rte_mbuf * | |
2720 | dpdk_pktmbuf_alloc(struct rte_mempool *mp, uint32_t data_len) | |
2721 | { | |
2722 | struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp); | |
2723 | ||
2724 | if (OVS_UNLIKELY(!pkt)) { | |
2725 | return NULL; | |
2726 | } | |
2727 | ||
2728 | if (rte_pktmbuf_tailroom(pkt) >= data_len) { | |
2729 | return pkt; | |
2730 | } | |
2731 | ||
2732 | if (dpdk_pktmbuf_attach_extbuf(pkt, data_len)) { | |
2733 | return pkt; | |
2734 | } | |
2735 | ||
2736 | rte_pktmbuf_free(pkt); | |
2737 | ||
2738 | return NULL; | |
2739 | } | |
2740 | ||
2741 | static struct dp_packet * | |
2742 | dpdk_copy_dp_packet_to_mbuf(struct rte_mempool *mp, struct dp_packet *pkt_orig) | |
2743 | { | |
2744 | struct rte_mbuf *mbuf_dest; | |
2745 | struct dp_packet *pkt_dest; | |
2746 | uint32_t pkt_len; | |
2747 | ||
2748 | pkt_len = dp_packet_size(pkt_orig); | |
2749 | mbuf_dest = dpdk_pktmbuf_alloc(mp, pkt_len); | |
2750 | if (OVS_UNLIKELY(mbuf_dest == NULL)) { | |
2751 | return NULL; | |
2752 | } | |
2753 | ||
2754 | pkt_dest = CONTAINER_OF(mbuf_dest, struct dp_packet, mbuf); | |
2755 | memcpy(dp_packet_data(pkt_dest), dp_packet_data(pkt_orig), pkt_len); | |
2756 | dp_packet_set_size(pkt_dest, pkt_len); | |
2757 | ||
2758 | mbuf_dest->tx_offload = pkt_orig->mbuf.tx_offload; | |
2759 | mbuf_dest->packet_type = pkt_orig->mbuf.packet_type; | |
2760 | mbuf_dest->ol_flags |= (pkt_orig->mbuf.ol_flags & | |
2761 | ~(EXT_ATTACHED_MBUF | IND_ATTACHED_MBUF)); | |
2762 | ||
2763 | memcpy(&pkt_dest->l2_pad_size, &pkt_orig->l2_pad_size, | |
2764 | sizeof(struct dp_packet) - offsetof(struct dp_packet, l2_pad_size)); | |
2765 | ||
2766 | if (mbuf_dest->ol_flags & PKT_TX_L4_MASK) { | |
2767 | mbuf_dest->l2_len = (char *)dp_packet_l3(pkt_dest) | |
2768 | - (char *)dp_packet_eth(pkt_dest); | |
2769 | mbuf_dest->l3_len = (char *)dp_packet_l4(pkt_dest) | |
2770 | - (char *) dp_packet_l3(pkt_dest); | |
2771 | } | |
2772 | ||
2773 | return pkt_dest; | |
2774 | } | |
2775 | ||
8a9562d2 PS |
2776 | /* Tx function. Transmit packets indefinitely */ |
2777 | static void | |
64839cf4 | 2778 | dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch) |
db73f716 | 2779 | OVS_NO_THREAD_SAFETY_ANALYSIS |
8a9562d2 | 2780 | { |
8a14bd7b | 2781 | const size_t batch_cnt = dp_packet_batch_size(batch); |
bce01e3a | 2782 | #if !defined(__CHECKER__) && !defined(_WIN32) |
8a14bd7b | 2783 | const size_t PKT_ARRAY_SIZE = batch_cnt; |
bce01e3a EJ |
2784 | #else |
2785 | /* Sparse or MSVC doesn't like variable length array. */ | |
cd159f1a | 2786 | enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST }; |
bce01e3a | 2787 | #endif |
8a9562d2 | 2788 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
29cf9c1b | 2789 | struct dp_packet *pkts[PKT_ARRAY_SIZE]; |
2f862c71 | 2790 | struct netdev_dpdk_sw_stats *sw_stats = dev->sw_stats; |
8a14bd7b | 2791 | uint32_t cnt = batch_cnt; |
3e90f7d7 | 2792 | uint32_t dropped = 0; |
2f862c71 SV |
2793 | uint32_t tx_failure = 0; |
2794 | uint32_t mtu_drops = 0; | |
2795 | uint32_t qos_drops = 0; | |
3e90f7d7 GZ |
2796 | |
2797 | if (dev->type != DPDK_DEV_VHOST) { | |
2798 | /* Check if QoS has been configured for this netdev. */ | |
2799 | cnt = netdev_dpdk_qos_run(dev, (struct rte_mbuf **) batch->packets, | |
8a14bd7b | 2800 | batch_cnt, false); |
2f862c71 | 2801 | qos_drops = batch_cnt - cnt; |
3e90f7d7 | 2802 | } |
8a9562d2 | 2803 | |
3e90f7d7 GZ |
2804 | uint32_t txcnt = 0; |
2805 | ||
2806 | for (uint32_t i = 0; i < cnt; i++) { | |
8a14bd7b BB |
2807 | struct dp_packet *packet = batch->packets[i]; |
2808 | uint32_t size = dp_packet_size(packet); | |
95fb793a | 2809 | |
29cf9c1b FL |
2810 | if (size > dev->max_packet_len |
2811 | && !(packet->mbuf.ol_flags & PKT_TX_TCP_SEG)) { | |
2812 | VLOG_WARN_RL(&rl, "Too big size %u max_packet_len %d", size, | |
2813 | dev->max_packet_len); | |
2f862c71 | 2814 | mtu_drops++; |
f4fd623c DDP |
2815 | continue; |
2816 | } | |
8a9562d2 | 2817 | |
29cf9c1b | 2818 | pkts[txcnt] = dpdk_copy_dp_packet_to_mbuf(dev->dpdk_mp->mp, packet); |
8a14bd7b | 2819 | if (OVS_UNLIKELY(!pkts[txcnt])) { |
2f862c71 | 2820 | dropped = cnt - i; |
175cf4de | 2821 | break; |
f4fd623c DDP |
2822 | } |
2823 | ||
3e90f7d7 | 2824 | txcnt++; |
f4fd623c | 2825 | } |
8a9562d2 | 2826 | |
3e90f7d7 GZ |
2827 | if (OVS_LIKELY(txcnt)) { |
2828 | if (dev->type == DPDK_DEV_VHOST) { | |
29cf9c1b | 2829 | __netdev_dpdk_vhost_send(netdev, qid, pkts, txcnt); |
3e90f7d7 | 2830 | } else { |
29cf9c1b FL |
2831 | tx_failure += netdev_dpdk_eth_tx_burst(dev, qid, |
2832 | (struct rte_mbuf **)pkts, | |
2833 | txcnt); | |
3e90f7d7 | 2834 | } |
58397e6c | 2835 | } |
db73f716 | 2836 | |
2f862c71 | 2837 | dropped += qos_drops + mtu_drops + tx_failure; |
0bf765f7 IS |
2838 | if (OVS_UNLIKELY(dropped)) { |
2839 | rte_spinlock_lock(&dev->stats_lock); | |
2840 | dev->stats.tx_dropped += dropped; | |
2f862c71 SV |
2841 | sw_stats->tx_failure_drops += tx_failure; |
2842 | sw_stats->tx_mtu_exceeded_drops += mtu_drops; | |
2843 | sw_stats->tx_qos_drops += qos_drops; | |
0bf765f7 IS |
2844 | rte_spinlock_unlock(&dev->stats_lock); |
2845 | } | |
8a9562d2 PS |
2846 | } |
2847 | ||
58397e6c | 2848 | static int |
64839cf4 WT |
2849 | netdev_dpdk_vhost_send(struct netdev *netdev, int qid, |
2850 | struct dp_packet_batch *batch, | |
b30896c9 | 2851 | bool concurrent_txq OVS_UNUSED) |
58397e6c | 2852 | { |
58397e6c | 2853 | |
b30896c9 | 2854 | if (OVS_UNLIKELY(batch->packets[0]->source != DPBUF_DPDK)) { |
64839cf4 | 2855 | dpdk_do_tx_copy(netdev, qid, batch); |
b30896c9 | 2856 | dp_packet_delete_batch(batch, true); |
58397e6c | 2857 | } else { |
940ac2ce PC |
2858 | __netdev_dpdk_vhost_send(netdev, qid, batch->packets, |
2859 | dp_packet_batch_size(batch)); | |
58397e6c KT |
2860 | } |
2861 | return 0; | |
2862 | } | |
2863 | ||
7251515e DV |
2864 | static inline void |
2865 | netdev_dpdk_send__(struct netdev_dpdk *dev, int qid, | |
b30896c9 | 2866 | struct dp_packet_batch *batch, |
324c8374 | 2867 | bool concurrent_txq) |
8a9562d2 | 2868 | { |
3b1fb077 | 2869 | if (OVS_UNLIKELY(!(dev->flags & NETDEV_UP))) { |
b30896c9 | 2870 | dp_packet_delete_batch(batch, true); |
3b1fb077 DDP |
2871 | return; |
2872 | } | |
2873 | ||
324c8374 | 2874 | if (OVS_UNLIKELY(concurrent_txq)) { |
81acebda | 2875 | qid = qid % dev->up.n_txq; |
a0cb2d66 DDP |
2876 | rte_spinlock_lock(&dev->tx_q[qid].tx_lock); |
2877 | } | |
2878 | ||
b30896c9 | 2879 | if (OVS_UNLIKELY(batch->packets[0]->source != DPBUF_DPDK)) { |
7251515e DV |
2880 | struct netdev *netdev = &dev->up; |
2881 | ||
64839cf4 | 2882 | dpdk_do_tx_copy(netdev, qid, batch); |
b30896c9 | 2883 | dp_packet_delete_batch(batch, true); |
8a9562d2 | 2884 | } else { |
2f862c71 | 2885 | struct netdev_dpdk_sw_stats *sw_stats = dev->sw_stats; |
29cf9c1b FL |
2886 | int dropped; |
2887 | int tx_failure, mtu_drops, qos_drops, hwol_drops; | |
fd57eeba | 2888 | int batch_cnt = dp_packet_batch_size(batch); |
2391135c | 2889 | struct rte_mbuf **pkts = (struct rte_mbuf **) batch->packets; |
8a9562d2 | 2890 | |
29cf9c1b FL |
2891 | hwol_drops = batch_cnt; |
2892 | if (userspace_tso_enabled()) { | |
2893 | batch_cnt = netdev_dpdk_prep_hwol_batch(dev, pkts, batch_cnt); | |
2894 | } | |
2895 | hwol_drops -= batch_cnt; | |
2896 | mtu_drops = batch_cnt; | |
2897 | batch_cnt = netdev_dpdk_filter_packet_len(dev, pkts, batch_cnt); | |
2898 | mtu_drops -= batch_cnt; | |
2899 | qos_drops = batch_cnt; | |
2900 | batch_cnt = netdev_dpdk_qos_run(dev, pkts, batch_cnt, true); | |
2901 | qos_drops -= batch_cnt; | |
1b99bb05 | 2902 | |
29cf9c1b | 2903 | tx_failure = netdev_dpdk_eth_tx_burst(dev, qid, pkts, batch_cnt); |
8a9562d2 | 2904 | |
29cf9c1b | 2905 | dropped = tx_failure + mtu_drops + qos_drops + hwol_drops; |
f4fd623c | 2906 | if (OVS_UNLIKELY(dropped)) { |
45d947c4 | 2907 | rte_spinlock_lock(&dev->stats_lock); |
f4fd623c | 2908 | dev->stats.tx_dropped += dropped; |
2f862c71 SV |
2909 | sw_stats->tx_failure_drops += tx_failure; |
2910 | sw_stats->tx_mtu_exceeded_drops += mtu_drops; | |
2911 | sw_stats->tx_qos_drops += qos_drops; | |
29cf9c1b | 2912 | sw_stats->tx_invalid_hwol_drops += hwol_drops; |
45d947c4 | 2913 | rte_spinlock_unlock(&dev->stats_lock); |
f4fd623c | 2914 | } |
8a9562d2 | 2915 | } |
a0cb2d66 | 2916 | |
324c8374 | 2917 | if (OVS_UNLIKELY(concurrent_txq)) { |
a0cb2d66 DDP |
2918 | rte_spinlock_unlock(&dev->tx_q[qid].tx_lock); |
2919 | } | |
7251515e DV |
2920 | } |
2921 | ||
2922 | static int | |
2923 | netdev_dpdk_eth_send(struct netdev *netdev, int qid, | |
b30896c9 | 2924 | struct dp_packet_batch *batch, bool concurrent_txq) |
7251515e DV |
2925 | { |
2926 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
8a9562d2 | 2927 | |
b30896c9 | 2928 | netdev_dpdk_send__(dev, qid, batch, concurrent_txq); |
7251515e | 2929 | return 0; |
8a9562d2 PS |
2930 | } |
2931 | ||
f9b0107d IM |
2932 | static int |
2933 | netdev_dpdk_set_etheraddr__(struct netdev_dpdk *dev, const struct eth_addr mac) | |
2934 | OVS_REQUIRES(dev->mutex) | |
2935 | { | |
2936 | int err = 0; | |
2937 | ||
2938 | if (dev->type == DPDK_DEV_ETH) { | |
2939 | struct rte_ether_addr ea; | |
2940 | ||
2941 | memcpy(ea.addr_bytes, mac.ea, ETH_ADDR_LEN); | |
2942 | err = -rte_eth_dev_default_mac_addr_set(dev->port_id, &ea); | |
2943 | } | |
2944 | if (!err) { | |
2945 | dev->hwaddr = mac; | |
2946 | } else { | |
2947 | VLOG_WARN("%s: Failed to set requested mac("ETH_ADDR_FMT"): %s", | |
2948 | netdev_get_name(&dev->up), ETH_ADDR_ARGS(mac), | |
2949 | rte_strerror(err)); | |
2950 | } | |
2951 | ||
2952 | return err; | |
2953 | } | |
2954 | ||
8a9562d2 | 2955 | static int |
74ff3298 | 2956 | netdev_dpdk_set_etheraddr(struct netdev *netdev, const struct eth_addr mac) |
8a9562d2 PS |
2957 | { |
2958 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
f9b0107d | 2959 | int err = 0; |
8a9562d2 PS |
2960 | |
2961 | ovs_mutex_lock(&dev->mutex); | |
2962 | if (!eth_addr_equals(dev->hwaddr, mac)) { | |
f9b0107d IM |
2963 | err = netdev_dpdk_set_etheraddr__(dev, mac); |
2964 | if (!err) { | |
2965 | netdev_change_seq_changed(netdev); | |
2966 | } | |
8a9562d2 PS |
2967 | } |
2968 | ovs_mutex_unlock(&dev->mutex); | |
2969 | ||
f9b0107d | 2970 | return err; |
8a9562d2 PS |
2971 | } |
2972 | ||
2973 | static int | |
74ff3298 | 2974 | netdev_dpdk_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac) |
8a9562d2 PS |
2975 | { |
2976 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
2977 | ||
2978 | ovs_mutex_lock(&dev->mutex); | |
74ff3298 | 2979 | *mac = dev->hwaddr; |
8a9562d2 PS |
2980 | ovs_mutex_unlock(&dev->mutex); |
2981 | ||
2982 | return 0; | |
2983 | } | |
2984 | ||
2985 | static int | |
2986 | netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup) | |
2987 | { | |
2988 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
2989 | ||
2990 | ovs_mutex_lock(&dev->mutex); | |
2991 | *mtup = dev->mtu; | |
2992 | ovs_mutex_unlock(&dev->mutex); | |
2993 | ||
2994 | return 0; | |
2995 | } | |
2996 | ||
0072e931 MK |
2997 | static int |
2998 | netdev_dpdk_set_mtu(struct netdev *netdev, int mtu) | |
2999 | { | |
3000 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3001 | ||
f6f50552 IS |
3002 | /* XXX: Ensure that the overall frame length of the requested MTU does not |
3003 | * surpass the NETDEV_DPDK_MAX_PKT_LEN. DPDK device drivers differ in how | |
3004 | * the L2 frame length is calculated for a given MTU when | |
3005 | * rte_eth_dev_set_mtu(mtu) is called e.g. i40e driver includes 2 x vlan | |
3006 | * headers, the em driver includes 1 x vlan header, the ixgbe driver does | |
3007 | * not include vlan headers. As such we should use | |
3008 | * MTU_TO_MAX_FRAME_LEN(mtu) which includes an additional 2 x vlan headers | |
3009 | * (8 bytes) for comparison. This avoids a failure later with | |
3010 | * rte_eth_dev_set_mtu(). This approach should be used until DPDK provides | |
3011 | * a method to retrieve the upper bound MTU for a given device. | |
3012 | */ | |
3013 | if (MTU_TO_MAX_FRAME_LEN(mtu) > NETDEV_DPDK_MAX_PKT_LEN | |
127b6a6e | 3014 | || mtu < RTE_ETHER_MIN_MTU) { |
0072e931 MK |
3015 | VLOG_WARN("%s: unsupported MTU %d\n", dev->up.name, mtu); |
3016 | return EINVAL; | |
3017 | } | |
3018 | ||
3019 | ovs_mutex_lock(&dev->mutex); | |
3020 | if (dev->requested_mtu != mtu) { | |
3021 | dev->requested_mtu = mtu; | |
3022 | netdev_request_reconfigure(netdev); | |
3023 | } | |
3024 | ovs_mutex_unlock(&dev->mutex); | |
3025 | ||
3026 | return 0; | |
3027 | } | |
3028 | ||
8a9562d2 | 3029 | static int |
d46285a2 | 3030 | netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier); |
8a9562d2 | 3031 | |
58397e6c KT |
3032 | static int |
3033 | netdev_dpdk_vhost_get_stats(const struct netdev *netdev, | |
3034 | struct netdev_stats *stats) | |
3035 | { | |
3036 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3037 | ||
3038 | ovs_mutex_lock(&dev->mutex); | |
58397e6c | 3039 | |
45d947c4 | 3040 | rte_spinlock_lock(&dev->stats_lock); |
58397e6c | 3041 | /* Supported Stats */ |
50986e78 | 3042 | stats->rx_packets = dev->stats.rx_packets; |
3043 | stats->tx_packets = dev->stats.tx_packets; | |
9509913a | 3044 | stats->rx_dropped = dev->stats.rx_dropped; |
50986e78 | 3045 | stats->tx_dropped = dev->stats.tx_dropped; |
9e3ddd45 TP |
3046 | stats->multicast = dev->stats.multicast; |
3047 | stats->rx_bytes = dev->stats.rx_bytes; | |
3048 | stats->tx_bytes = dev->stats.tx_bytes; | |
3049 | stats->rx_errors = dev->stats.rx_errors; | |
3050 | stats->rx_length_errors = dev->stats.rx_length_errors; | |
d6e3feb5 | 3051 | |
3052 | stats->rx_1_to_64_packets = dev->stats.rx_1_to_64_packets; | |
3053 | stats->rx_65_to_127_packets = dev->stats.rx_65_to_127_packets; | |
3054 | stats->rx_128_to_255_packets = dev->stats.rx_128_to_255_packets; | |
3055 | stats->rx_256_to_511_packets = dev->stats.rx_256_to_511_packets; | |
3056 | stats->rx_512_to_1023_packets = dev->stats.rx_512_to_1023_packets; | |
3057 | stats->rx_1024_to_1522_packets = dev->stats.rx_1024_to_1522_packets; | |
3058 | stats->rx_1523_to_max_packets = dev->stats.rx_1523_to_max_packets; | |
3059 | ||
45d947c4 | 3060 | rte_spinlock_unlock(&dev->stats_lock); |
9e3ddd45 | 3061 | |
58397e6c KT |
3062 | ovs_mutex_unlock(&dev->mutex); |
3063 | ||
3064 | return 0; | |
3065 | } | |
3066 | ||
d6e3feb5 | 3067 | static void |
3068 | netdev_dpdk_convert_xstats(struct netdev_stats *stats, | |
0a0f39df CL |
3069 | const struct rte_eth_xstat *xstats, |
3070 | const struct rte_eth_xstat_name *names, | |
d6e3feb5 | 3071 | const unsigned int size) |
3072 | { | |
18366d16 IM |
3073 | /* DPDK XSTATS Counter names definition. */ |
3074 | #define DPDK_XSTATS \ | |
3075 | DPDK_XSTAT(multicast, "rx_multicast_packets" ) \ | |
3076 | DPDK_XSTAT(tx_multicast_packets, "tx_multicast_packets" ) \ | |
3077 | DPDK_XSTAT(rx_broadcast_packets, "rx_broadcast_packets" ) \ | |
3078 | DPDK_XSTAT(tx_broadcast_packets, "tx_broadcast_packets" ) \ | |
3079 | DPDK_XSTAT(rx_undersized_errors, "rx_undersized_errors" ) \ | |
3080 | DPDK_XSTAT(rx_oversize_errors, "rx_oversize_errors" ) \ | |
3081 | DPDK_XSTAT(rx_fragmented_errors, "rx_fragmented_errors" ) \ | |
3082 | DPDK_XSTAT(rx_jabber_errors, "rx_jabber_errors" ) \ | |
3083 | DPDK_XSTAT(rx_1_to_64_packets, "rx_size_64_packets" ) \ | |
3084 | DPDK_XSTAT(rx_65_to_127_packets, "rx_size_65_to_127_packets" ) \ | |
3085 | DPDK_XSTAT(rx_128_to_255_packets, "rx_size_128_to_255_packets" ) \ | |
3086 | DPDK_XSTAT(rx_256_to_511_packets, "rx_size_256_to_511_packets" ) \ | |
3087 | DPDK_XSTAT(rx_512_to_1023_packets, "rx_size_512_to_1023_packets" ) \ | |
3088 | DPDK_XSTAT(rx_1024_to_1522_packets, "rx_size_1024_to_1522_packets" ) \ | |
3089 | DPDK_XSTAT(rx_1523_to_max_packets, "rx_size_1523_to_max_packets" ) \ | |
3090 | DPDK_XSTAT(tx_1_to_64_packets, "tx_size_64_packets" ) \ | |
3091 | DPDK_XSTAT(tx_65_to_127_packets, "tx_size_65_to_127_packets" ) \ | |
3092 | DPDK_XSTAT(tx_128_to_255_packets, "tx_size_128_to_255_packets" ) \ | |
3093 | DPDK_XSTAT(tx_256_to_511_packets, "tx_size_256_to_511_packets" ) \ | |
3094 | DPDK_XSTAT(tx_512_to_1023_packets, "tx_size_512_to_1023_packets" ) \ | |
3095 | DPDK_XSTAT(tx_1024_to_1522_packets, "tx_size_1024_to_1522_packets" ) \ | |
3096 | DPDK_XSTAT(tx_1523_to_max_packets, "tx_size_1523_to_max_packets" ) | |
3097 | ||
d6e3feb5 | 3098 | for (unsigned int i = 0; i < size; i++) { |
18366d16 IM |
3099 | #define DPDK_XSTAT(MEMBER, NAME) \ |
3100 | if (strcmp(NAME, names[i].name) == 0) { \ | |
3101 | stats->MEMBER = xstats[i].value; \ | |
3102 | continue; \ | |
d6e3feb5 | 3103 | } |
18366d16 IM |
3104 | DPDK_XSTATS; |
3105 | #undef DPDK_XSTAT | |
d6e3feb5 | 3106 | } |
18366d16 | 3107 | #undef DPDK_XSTATS |
d6e3feb5 | 3108 | } |
3109 | ||
8a9562d2 PS |
3110 | static int |
3111 | netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats) | |
3112 | { | |
3113 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3114 | struct rte_eth_stats rte_stats; | |
3115 | bool gg; | |
3116 | ||
3117 | netdev_dpdk_get_carrier(netdev, &gg); | |
3118 | ovs_mutex_lock(&dev->mutex); | |
8a9562d2 | 3119 | |
0a0f39df CL |
3120 | struct rte_eth_xstat *rte_xstats = NULL; |
3121 | struct rte_eth_xstat_name *rte_xstats_names = NULL; | |
3122 | int rte_xstats_len, rte_xstats_new_len, rte_xstats_ret; | |
d6e3feb5 | 3123 | |
3124 | if (rte_eth_stats_get(dev->port_id, &rte_stats)) { | |
fa9f4eeb IM |
3125 | VLOG_ERR("Can't get ETH statistics for port: "DPDK_PORT_ID_FMT, |
3126 | dev->port_id); | |
f9256822 | 3127 | ovs_mutex_unlock(&dev->mutex); |
d6e3feb5 | 3128 | return EPROTO; |
3129 | } | |
3130 | ||
0a0f39df CL |
3131 | /* Get length of statistics */ |
3132 | rte_xstats_len = rte_eth_xstats_get_names(dev->port_id, NULL, 0); | |
3133 | if (rte_xstats_len < 0) { | |
fa9f4eeb IM |
3134 | VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT, |
3135 | dev->port_id); | |
0a0f39df CL |
3136 | goto out; |
3137 | } | |
3138 | /* Reserve memory for xstats names and values */ | |
3139 | rte_xstats_names = xcalloc(rte_xstats_len, sizeof *rte_xstats_names); | |
3140 | rte_xstats = xcalloc(rte_xstats_len, sizeof *rte_xstats); | |
3141 | ||
3142 | /* Retreive xstats names */ | |
3143 | rte_xstats_new_len = rte_eth_xstats_get_names(dev->port_id, | |
3144 | rte_xstats_names, | |
3145 | rte_xstats_len); | |
3146 | if (rte_xstats_new_len != rte_xstats_len) { | |
fa9f4eeb IM |
3147 | VLOG_WARN("Cannot get XSTATS names for port: "DPDK_PORT_ID_FMT, |
3148 | dev->port_id); | |
0a0f39df CL |
3149 | goto out; |
3150 | } | |
3151 | /* Retreive xstats values */ | |
3152 | memset(rte_xstats, 0xff, sizeof *rte_xstats * rte_xstats_len); | |
3153 | rte_xstats_ret = rte_eth_xstats_get(dev->port_id, rte_xstats, | |
3154 | rte_xstats_len); | |
3155 | if (rte_xstats_ret > 0 && rte_xstats_ret <= rte_xstats_len) { | |
3156 | netdev_dpdk_convert_xstats(stats, rte_xstats, rte_xstats_names, | |
3157 | rte_xstats_len); | |
d6e3feb5 | 3158 | } else { |
fa9f4eeb IM |
3159 | VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT, |
3160 | dev->port_id); | |
d6e3feb5 | 3161 | } |
8a9562d2 | 3162 | |
0a0f39df CL |
3163 | out: |
3164 | free(rte_xstats); | |
3165 | free(rte_xstats_names); | |
3166 | ||
2f9dd77f PS |
3167 | stats->rx_packets = rte_stats.ipackets; |
3168 | stats->tx_packets = rte_stats.opackets; | |
3169 | stats->rx_bytes = rte_stats.ibytes; | |
3170 | stats->tx_bytes = rte_stats.obytes; | |
21e9844c | 3171 | stats->rx_errors = rte_stats.ierrors; |
2f9dd77f | 3172 | stats->tx_errors = rte_stats.oerrors; |
8a9562d2 | 3173 | |
45d947c4 | 3174 | rte_spinlock_lock(&dev->stats_lock); |
2f9dd77f | 3175 | stats->tx_dropped = dev->stats.tx_dropped; |
9509913a | 3176 | stats->rx_dropped = dev->stats.rx_dropped; |
45d947c4 | 3177 | rte_spinlock_unlock(&dev->stats_lock); |
9e3ddd45 TP |
3178 | |
3179 | /* These are the available DPDK counters for packets not received due to | |
3180 | * local resource constraints in DPDK and NIC respectively. */ | |
9509913a | 3181 | stats->rx_dropped += rte_stats.rx_nombuf + rte_stats.imissed; |
9e3ddd45 TP |
3182 | stats->rx_missed_errors = rte_stats.imissed; |
3183 | ||
8a9562d2 PS |
3184 | ovs_mutex_unlock(&dev->mutex); |
3185 | ||
3186 | return 0; | |
3187 | } | |
3188 | ||
971f4b39 MW |
3189 | static int |
3190 | netdev_dpdk_get_custom_stats(const struct netdev *netdev, | |
3191 | struct netdev_custom_stats *custom_stats) | |
3192 | { | |
3193 | ||
3194 | uint32_t i; | |
3195 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
b99ab8aa IM |
3196 | int rte_xstats_ret, sw_stats_size; |
3197 | ||
3198 | netdev_dpdk_get_sw_custom_stats(netdev, custom_stats); | |
971f4b39 MW |
3199 | |
3200 | ovs_mutex_lock(&dev->mutex); | |
3201 | ||
3202 | if (netdev_dpdk_configure_xstats(dev)) { | |
3203 | uint64_t *values = xcalloc(dev->rte_xstats_ids_size, | |
3204 | sizeof(uint64_t)); | |
3205 | ||
3206 | rte_xstats_ret = | |
3207 | rte_eth_xstats_get_by_id(dev->port_id, dev->rte_xstats_ids, | |
3208 | values, dev->rte_xstats_ids_size); | |
3209 | ||
3210 | if (rte_xstats_ret > 0 && | |
3211 | rte_xstats_ret <= dev->rte_xstats_ids_size) { | |
3212 | ||
b99ab8aa IM |
3213 | sw_stats_size = custom_stats->size; |
3214 | custom_stats->size += rte_xstats_ret; | |
3215 | custom_stats->counters = xrealloc(custom_stats->counters, | |
3216 | custom_stats->size * | |
3217 | sizeof *custom_stats->counters); | |
971f4b39 MW |
3218 | |
3219 | for (i = 0; i < rte_xstats_ret; i++) { | |
b99ab8aa | 3220 | ovs_strlcpy(custom_stats->counters[sw_stats_size + i].name, |
971f4b39 MW |
3221 | netdev_dpdk_get_xstat_name(dev, |
3222 | dev->rte_xstats_ids[i]), | |
3223 | NETDEV_CUSTOM_STATS_NAME_SIZE); | |
b99ab8aa | 3224 | custom_stats->counters[sw_stats_size + i].value = values[i]; |
971f4b39 MW |
3225 | } |
3226 | } else { | |
fa9f4eeb | 3227 | VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT, |
971f4b39 | 3228 | dev->port_id); |
971f4b39 MW |
3229 | /* Let's clear statistics cache, so it will be |
3230 | * reconfigured */ | |
3231 | netdev_dpdk_clear_xstats(dev); | |
3232 | } | |
526259f2 IM |
3233 | |
3234 | free(values); | |
971f4b39 MW |
3235 | } |
3236 | ||
3237 | ovs_mutex_unlock(&dev->mutex); | |
3238 | ||
3239 | return 0; | |
3240 | } | |
3241 | ||
c161357d | 3242 | static int |
b99ab8aa IM |
3243 | netdev_dpdk_get_sw_custom_stats(const struct netdev *netdev, |
3244 | struct netdev_custom_stats *custom_stats) | |
c161357d KT |
3245 | { |
3246 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
b99ab8aa | 3247 | int i, n; |
c161357d | 3248 | |
2f862c71 SV |
3249 | #define SW_CSTATS \ |
3250 | SW_CSTAT(tx_retries) \ | |
3251 | SW_CSTAT(tx_failure_drops) \ | |
3252 | SW_CSTAT(tx_mtu_exceeded_drops) \ | |
3253 | SW_CSTAT(tx_qos_drops) \ | |
29cf9c1b FL |
3254 | SW_CSTAT(rx_qos_drops) \ |
3255 | SW_CSTAT(tx_invalid_hwol_drops) | |
c161357d | 3256 | |
b99ab8aa IM |
3257 | #define SW_CSTAT(NAME) + 1 |
3258 | custom_stats->size = SW_CSTATS; | |
3259 | #undef SW_CSTAT | |
c161357d KT |
3260 | custom_stats->counters = xcalloc(custom_stats->size, |
3261 | sizeof *custom_stats->counters); | |
5c7ba90d IM |
3262 | |
3263 | ovs_mutex_lock(&dev->mutex); | |
c161357d KT |
3264 | |
3265 | rte_spinlock_lock(&dev->stats_lock); | |
5c7ba90d | 3266 | i = 0; |
b99ab8aa | 3267 | #define SW_CSTAT(NAME) \ |
2f862c71 | 3268 | custom_stats->counters[i++].value = dev->sw_stats->NAME; |
b99ab8aa IM |
3269 | SW_CSTATS; |
3270 | #undef SW_CSTAT | |
c161357d KT |
3271 | rte_spinlock_unlock(&dev->stats_lock); |
3272 | ||
3273 | ovs_mutex_unlock(&dev->mutex); | |
3274 | ||
b99ab8aa IM |
3275 | i = 0; |
3276 | n = 0; | |
3277 | #define SW_CSTAT(NAME) \ | |
3278 | if (custom_stats->counters[i].value != UINT64_MAX) { \ | |
2f862c71 SV |
3279 | ovs_strlcpy(custom_stats->counters[n].name, \ |
3280 | "ovs_"#NAME, NETDEV_CUSTOM_STATS_NAME_SIZE); \ | |
b99ab8aa IM |
3281 | custom_stats->counters[n].value = custom_stats->counters[i].value; \ |
3282 | n++; \ | |
3283 | } \ | |
3284 | i++; | |
3285 | SW_CSTATS; | |
3286 | #undef SW_CSTAT | |
3287 | ||
3288 | custom_stats->size = n; | |
c161357d KT |
3289 | return 0; |
3290 | } | |
3291 | ||
8a9562d2 | 3292 | static int |
d46285a2 | 3293 | netdev_dpdk_get_features(const struct netdev *netdev, |
8a9562d2 | 3294 | enum netdev_features *current, |
ca3d4f55 BX |
3295 | enum netdev_features *advertised, |
3296 | enum netdev_features *supported, | |
3297 | enum netdev_features *peer) | |
8a9562d2 | 3298 | { |
d46285a2 | 3299 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 | 3300 | struct rte_eth_link link; |
dfcb5b8a | 3301 | uint32_t feature = 0; |
8a9562d2 PS |
3302 | |
3303 | ovs_mutex_lock(&dev->mutex); | |
3304 | link = dev->link; | |
3305 | ovs_mutex_unlock(&dev->mutex); | |
3306 | ||
dfcb5b8a IS |
3307 | /* Match against OpenFlow defined link speed values. */ |
3308 | if (link.link_duplex == ETH_LINK_FULL_DUPLEX) { | |
3309 | switch (link.link_speed) { | |
3310 | case ETH_SPEED_NUM_10M: | |
3311 | feature |= NETDEV_F_10MB_FD; | |
3312 | break; | |
3313 | case ETH_SPEED_NUM_100M: | |
3314 | feature |= NETDEV_F_100MB_FD; | |
3315 | break; | |
3316 | case ETH_SPEED_NUM_1G: | |
3317 | feature |= NETDEV_F_1GB_FD; | |
3318 | break; | |
3319 | case ETH_SPEED_NUM_10G: | |
3320 | feature |= NETDEV_F_10GB_FD; | |
3321 | break; | |
3322 | case ETH_SPEED_NUM_40G: | |
3323 | feature |= NETDEV_F_40GB_FD; | |
3324 | break; | |
3325 | case ETH_SPEED_NUM_100G: | |
3326 | feature |= NETDEV_F_100GB_FD; | |
3327 | break; | |
3328 | default: | |
3329 | feature |= NETDEV_F_OTHER; | |
8a9562d2 | 3330 | } |
dfcb5b8a IS |
3331 | } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) { |
3332 | switch (link.link_speed) { | |
3333 | case ETH_SPEED_NUM_10M: | |
3334 | feature |= NETDEV_F_10MB_HD; | |
3335 | break; | |
3336 | case ETH_SPEED_NUM_100M: | |
3337 | feature |= NETDEV_F_100MB_HD; | |
3338 | break; | |
3339 | case ETH_SPEED_NUM_1G: | |
3340 | feature |= NETDEV_F_1GB_HD; | |
3341 | break; | |
3342 | default: | |
3343 | feature |= NETDEV_F_OTHER; | |
74cd69a4 | 3344 | } |
8a9562d2 PS |
3345 | } |
3346 | ||
362ca396 | 3347 | if (link.link_autoneg) { |
dfcb5b8a | 3348 | feature |= NETDEV_F_AUTONEG; |
362ca396 | 3349 | } |
3350 | ||
dfcb5b8a | 3351 | *current = feature; |
ca3d4f55 BX |
3352 | *advertised = *supported = *peer = 0; |
3353 | ||
8a9562d2 PS |
3354 | return 0; |
3355 | } | |
3356 | ||
9509913a IS |
3357 | static struct ingress_policer * |
3358 | netdev_dpdk_policer_construct(uint32_t rate, uint32_t burst) | |
3359 | { | |
3360 | struct ingress_policer *policer = NULL; | |
3361 | uint64_t rate_bytes; | |
3362 | uint64_t burst_bytes; | |
3363 | int err = 0; | |
3364 | ||
3365 | policer = xmalloc(sizeof *policer); | |
3366 | rte_spinlock_init(&policer->policer_lock); | |
3367 | ||
3368 | /* rte_meter requires bytes so convert kbits rate and burst to bytes. */ | |
602c8668 LR |
3369 | rate_bytes = rate * 1000ULL / 8; |
3370 | burst_bytes = burst * 1000ULL / 8; | |
9509913a IS |
3371 | |
3372 | policer->app_srtcm_params.cir = rate_bytes; | |
3373 | policer->app_srtcm_params.cbs = burst_bytes; | |
3374 | policer->app_srtcm_params.ebs = 0; | |
03f3f9c0 OM |
3375 | err = rte_meter_srtcm_profile_config(&policer->in_prof, |
3376 | &policer->app_srtcm_params); | |
3377 | if (!err) { | |
3378 | err = rte_meter_srtcm_config(&policer->in_policer, | |
3379 | &policer->in_prof); | |
3380 | } | |
58be5c0e | 3381 | if (err) { |
9509913a | 3382 | VLOG_ERR("Could not create rte meter for ingress policer"); |
4c47ddde | 3383 | free(policer); |
9509913a IS |
3384 | return NULL; |
3385 | } | |
3386 | ||
3387 | return policer; | |
3388 | } | |
3389 | ||
3390 | static int | |
3391 | netdev_dpdk_set_policing(struct netdev* netdev, uint32_t policer_rate, | |
3392 | uint32_t policer_burst) | |
3393 | { | |
3394 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3395 | struct ingress_policer *policer; | |
3396 | ||
3397 | /* Force to 0 if no rate specified, | |
3398 | * default to 8000 kbits if burst is 0, | |
3399 | * else stick with user-specified value. | |
3400 | */ | |
3401 | policer_burst = (!policer_rate ? 0 | |
3402 | : !policer_burst ? 8000 | |
3403 | : policer_burst); | |
3404 | ||
3405 | ovs_mutex_lock(&dev->mutex); | |
3406 | ||
3407 | policer = ovsrcu_get_protected(struct ingress_policer *, | |
3408 | &dev->ingress_policer); | |
3409 | ||
3410 | if (dev->policer_rate == policer_rate && | |
3411 | dev->policer_burst == policer_burst) { | |
3412 | /* Assume that settings haven't changed since we last set them. */ | |
3413 | ovs_mutex_unlock(&dev->mutex); | |
3414 | return 0; | |
3415 | } | |
3416 | ||
3417 | /* Destroy any existing ingress policer for the device if one exists */ | |
3418 | if (policer) { | |
3419 | ovsrcu_postpone(free, policer); | |
3420 | } | |
3421 | ||
3422 | if (policer_rate != 0) { | |
3423 | policer = netdev_dpdk_policer_construct(policer_rate, policer_burst); | |
3424 | } else { | |
3425 | policer = NULL; | |
3426 | } | |
3427 | ovsrcu_set(&dev->ingress_policer, policer); | |
3428 | dev->policer_rate = policer_rate; | |
3429 | dev->policer_burst = policer_burst; | |
3430 | ovs_mutex_unlock(&dev->mutex); | |
3431 | ||
3432 | return 0; | |
3433 | } | |
3434 | ||
8a9562d2 PS |
3435 | static int |
3436 | netdev_dpdk_get_ifindex(const struct netdev *netdev) | |
3437 | { | |
3438 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
8a9562d2 PS |
3439 | |
3440 | ovs_mutex_lock(&dev->mutex); | |
12d0d124 PL |
3441 | /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit |
3442 | * postive integer to meet RFC 2863 recommendations. | |
3443 | */ | |
3444 | int ifindex = hash_string(netdev->name, 0) % 0xfffffe + 1; | |
8a9562d2 PS |
3445 | ovs_mutex_unlock(&dev->mutex); |
3446 | ||
3447 | return ifindex; | |
3448 | } | |
3449 | ||
3450 | static int | |
d46285a2 | 3451 | netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier) |
8a9562d2 | 3452 | { |
d46285a2 | 3453 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
3454 | |
3455 | ovs_mutex_lock(&dev->mutex); | |
3456 | check_link_status(dev); | |
3457 | *carrier = dev->link.link_status; | |
58397e6c KT |
3458 | |
3459 | ovs_mutex_unlock(&dev->mutex); | |
3460 | ||
3461 | return 0; | |
3462 | } | |
3463 | ||
3464 | static int | |
d46285a2 | 3465 | netdev_dpdk_vhost_get_carrier(const struct netdev *netdev, bool *carrier) |
58397e6c | 3466 | { |
d46285a2 | 3467 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
58397e6c KT |
3468 | |
3469 | ovs_mutex_lock(&dev->mutex); | |
3470 | ||
0a0f39df | 3471 | if (is_vhost_running(dev)) { |
58397e6c KT |
3472 | *carrier = 1; |
3473 | } else { | |
3474 | *carrier = 0; | |
3475 | } | |
3476 | ||
8a9562d2 PS |
3477 | ovs_mutex_unlock(&dev->mutex); |
3478 | ||
3479 | return 0; | |
3480 | } | |
3481 | ||
3482 | static long long int | |
d46285a2 | 3483 | netdev_dpdk_get_carrier_resets(const struct netdev *netdev) |
8a9562d2 | 3484 | { |
d46285a2 | 3485 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
3486 | long long int carrier_resets; |
3487 | ||
3488 | ovs_mutex_lock(&dev->mutex); | |
3489 | carrier_resets = dev->link_reset_cnt; | |
3490 | ovs_mutex_unlock(&dev->mutex); | |
3491 | ||
3492 | return carrier_resets; | |
3493 | } | |
3494 | ||
3495 | static int | |
d46285a2 | 3496 | netdev_dpdk_set_miimon(struct netdev *netdev OVS_UNUSED, |
8a9562d2 PS |
3497 | long long int interval OVS_UNUSED) |
3498 | { | |
ee32150e | 3499 | return EOPNOTSUPP; |
8a9562d2 PS |
3500 | } |
3501 | ||
3502 | static int | |
3503 | netdev_dpdk_update_flags__(struct netdev_dpdk *dev, | |
3504 | enum netdev_flags off, enum netdev_flags on, | |
64839cf4 WT |
3505 | enum netdev_flags *old_flagsp) |
3506 | OVS_REQUIRES(dev->mutex) | |
8a9562d2 | 3507 | { |
8a9562d2 PS |
3508 | if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) { |
3509 | return EINVAL; | |
3510 | } | |
3511 | ||
3512 | *old_flagsp = dev->flags; | |
3513 | dev->flags |= on; | |
3514 | dev->flags &= ~off; | |
3515 | ||
3516 | if (dev->flags == *old_flagsp) { | |
3517 | return 0; | |
3518 | } | |
3519 | ||
58397e6c | 3520 | if (dev->type == DPDK_DEV_ETH) { |
2d37de73 EC |
3521 | |
3522 | if ((dev->flags ^ *old_flagsp) & NETDEV_UP) { | |
3523 | int err; | |
3524 | ||
3525 | if (dev->flags & NETDEV_UP) { | |
3526 | err = rte_eth_dev_set_link_up(dev->port_id); | |
3527 | } else { | |
3528 | err = rte_eth_dev_set_link_down(dev->port_id); | |
3529 | } | |
3530 | if (err == -ENOTSUP) { | |
3531 | VLOG_INFO("Interface %s does not support link state " | |
3532 | "configuration", netdev_get_name(&dev->up)); | |
3533 | } else if (err < 0) { | |
3534 | VLOG_ERR("Interface %s link change error: %s", | |
3535 | netdev_get_name(&dev->up), rte_strerror(-err)); | |
3536 | dev->flags = *old_flagsp; | |
3537 | return -err; | |
3538 | } | |
3539 | } | |
3540 | ||
58397e6c KT |
3541 | if (dev->flags & NETDEV_PROMISC) { |
3542 | rte_eth_promiscuous_enable(dev->port_id); | |
3543 | } | |
8a9562d2 | 3544 | |
314fb5ad | 3545 | netdev_change_seq_changed(&dev->up); |
e543851d ZB |
3546 | } else { |
3547 | /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is | |
3548 | * running then change netdev's change_seq to trigger link state | |
3549 | * update. */ | |
e543851d ZB |
3550 | |
3551 | if ((NETDEV_UP & ((*old_flagsp ^ on) | (*old_flagsp ^ off))) | |
0a0f39df | 3552 | && is_vhost_running(dev)) { |
e543851d ZB |
3553 | netdev_change_seq_changed(&dev->up); |
3554 | ||
3555 | /* Clear statistics if device is getting up. */ | |
3556 | if (NETDEV_UP & on) { | |
3557 | rte_spinlock_lock(&dev->stats_lock); | |
58be5c0e | 3558 | memset(&dev->stats, 0, sizeof dev->stats); |
e543851d ZB |
3559 | rte_spinlock_unlock(&dev->stats_lock); |
3560 | } | |
3561 | } | |
8a9562d2 PS |
3562 | } |
3563 | ||
3564 | return 0; | |
3565 | } | |
3566 | ||
3567 | static int | |
d46285a2 | 3568 | netdev_dpdk_update_flags(struct netdev *netdev, |
8a9562d2 PS |
3569 | enum netdev_flags off, enum netdev_flags on, |
3570 | enum netdev_flags *old_flagsp) | |
3571 | { | |
d46285a2 | 3572 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
3573 | int error; |
3574 | ||
d46285a2 DDP |
3575 | ovs_mutex_lock(&dev->mutex); |
3576 | error = netdev_dpdk_update_flags__(dev, off, on, old_flagsp); | |
3577 | ovs_mutex_unlock(&dev->mutex); | |
8a9562d2 PS |
3578 | |
3579 | return error; | |
3580 | } | |
3581 | ||
b2e8b12f FL |
3582 | static int |
3583 | netdev_dpdk_vhost_user_get_status(const struct netdev *netdev, | |
3584 | struct smap *args) | |
3585 | { | |
3586 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3587 | ||
3588 | ovs_mutex_lock(&dev->mutex); | |
3589 | ||
3590 | bool client_mode = dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT; | |
3591 | smap_add_format(args, "mode", "%s", client_mode ? "client" : "server"); | |
3592 | ||
3593 | int vid = netdev_dpdk_get_vid(dev); | |
3594 | if (vid < 0) { | |
3595 | smap_add_format(args, "status", "disconnected"); | |
3596 | ovs_mutex_unlock(&dev->mutex); | |
3597 | return 0; | |
3598 | } else { | |
3599 | smap_add_format(args, "status", "connected"); | |
3600 | } | |
3601 | ||
3602 | char socket_name[PATH_MAX]; | |
3603 | if (!rte_vhost_get_ifname(vid, socket_name, PATH_MAX)) { | |
3604 | smap_add_format(args, "socket", "%s", socket_name); | |
3605 | } | |
3606 | ||
3607 | uint64_t features; | |
3608 | if (!rte_vhost_get_negotiated_features(vid, &features)) { | |
3609 | smap_add_format(args, "features", "0x%016"PRIx64, features); | |
3610 | } | |
3611 | ||
3612 | uint16_t mtu; | |
3613 | if (!rte_vhost_get_mtu(vid, &mtu)) { | |
3614 | smap_add_format(args, "mtu", "%d", mtu); | |
3615 | } | |
3616 | ||
3617 | int numa = rte_vhost_get_numa_node(vid); | |
3618 | if (numa >= 0) { | |
3619 | smap_add_format(args, "numa", "%d", numa); | |
3620 | } | |
3621 | ||
3622 | uint16_t vring_num = rte_vhost_get_vring_num(vid); | |
3623 | if (vring_num) { | |
3624 | smap_add_format(args, "num_of_vrings", "%d", vring_num); | |
3625 | } | |
3626 | ||
3627 | for (int i = 0; i < vring_num; i++) { | |
3628 | struct rte_vhost_vring vring; | |
b2e8b12f FL |
3629 | |
3630 | rte_vhost_get_vhost_vring(vid, i, &vring); | |
b9a3183d AC |
3631 | smap_add_nocopy(args, xasprintf("vring_%d_size", i), |
3632 | xasprintf("%d", vring.size)); | |
b2e8b12f FL |
3633 | } |
3634 | ||
3635 | ovs_mutex_unlock(&dev->mutex); | |
3636 | return 0; | |
3637 | } | |
3638 | ||
31154f95 IS |
3639 | /* |
3640 | * Convert a given uint32_t link speed defined in DPDK to a string | |
3641 | * equivalent. | |
3642 | */ | |
3643 | static const char * | |
3644 | netdev_dpdk_link_speed_to_str__(uint32_t link_speed) | |
3645 | { | |
3646 | switch (link_speed) { | |
3647 | case ETH_SPEED_NUM_10M: return "10Mbps"; | |
3648 | case ETH_SPEED_NUM_100M: return "100Mbps"; | |
3649 | case ETH_SPEED_NUM_1G: return "1Gbps"; | |
3650 | case ETH_SPEED_NUM_2_5G: return "2.5Gbps"; | |
3651 | case ETH_SPEED_NUM_5G: return "5Gbps"; | |
3652 | case ETH_SPEED_NUM_10G: return "10Gbps"; | |
3653 | case ETH_SPEED_NUM_20G: return "20Gbps"; | |
3654 | case ETH_SPEED_NUM_25G: return "25Gbps"; | |
3655 | case ETH_SPEED_NUM_40G: return "40Gbps"; | |
3656 | case ETH_SPEED_NUM_50G: return "50Gbps"; | |
3657 | case ETH_SPEED_NUM_56G: return "56Gbps"; | |
3658 | case ETH_SPEED_NUM_100G: return "100Gbps"; | |
3659 | default: return "Not Defined"; | |
3660 | } | |
3661 | } | |
3662 | ||
8a9562d2 | 3663 | static int |
d46285a2 | 3664 | netdev_dpdk_get_status(const struct netdev *netdev, struct smap *args) |
8a9562d2 | 3665 | { |
d46285a2 | 3666 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 | 3667 | struct rte_eth_dev_info dev_info; |
31154f95 | 3668 | uint32_t link_speed; |
f4336f50 | 3669 | uint32_t dev_flags; |
8a9562d2 | 3670 | |
7cd1261d | 3671 | if (!rte_eth_dev_is_valid_port(dev->port_id)) { |
8a9562d2 | 3672 | return ENODEV; |
7cd1261d | 3673 | } |
8a9562d2 | 3674 | |
03f3f9c0 | 3675 | ovs_mutex_lock(&dpdk_mutex); |
8a9562d2 PS |
3676 | ovs_mutex_lock(&dev->mutex); |
3677 | rte_eth_dev_info_get(dev->port_id, &dev_info); | |
31154f95 | 3678 | link_speed = dev->link.link_speed; |
f4336f50 | 3679 | dev_flags = *dev_info.dev_flags; |
8a9562d2 | 3680 | ovs_mutex_unlock(&dev->mutex); |
03f3f9c0 OM |
3681 | const struct rte_bus *bus; |
3682 | const struct rte_pci_device *pci_dev; | |
3683 | uint16_t vendor_id = PCI_ANY_ID; | |
3684 | uint16_t device_id = PCI_ANY_ID; | |
3685 | bus = rte_bus_find_by_device(dev_info.device); | |
3686 | if (bus && !strcmp(bus->name, "pci")) { | |
3687 | pci_dev = RTE_DEV_TO_PCI(dev_info.device); | |
3688 | if (pci_dev) { | |
3689 | vendor_id = pci_dev->id.vendor_id; | |
3690 | device_id = pci_dev->id.device_id; | |
3691 | } | |
3692 | } | |
3693 | ovs_mutex_unlock(&dpdk_mutex); | |
8a9562d2 | 3694 | |
fa9f4eeb | 3695 | smap_add_format(args, "port_no", DPDK_PORT_ID_FMT, dev->port_id); |
58be5c0e MK |
3696 | smap_add_format(args, "numa_id", "%d", |
3697 | rte_eth_dev_socket_id(dev->port_id)); | |
8a9562d2 PS |
3698 | smap_add_format(args, "driver_name", "%s", dev_info.driver_name); |
3699 | smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize); | |
4be4d22c | 3700 | smap_add_format(args, "max_rx_pktlen", "%u", dev->max_packet_len); |
8a9562d2 PS |
3701 | smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues); |
3702 | smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues); | |
3703 | smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs); | |
58be5c0e MK |
3704 | smap_add_format(args, "max_hash_mac_addrs", "%u", |
3705 | dev_info.max_hash_mac_addrs); | |
8a9562d2 PS |
3706 | smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs); |
3707 | smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools); | |
3708 | ||
3eb8d4fa MW |
3709 | /* Querying the DPDK library for iftype may be done in future, pending |
3710 | * support; cf. RFC 3635 Section 3.2.4. */ | |
3711 | enum { IF_TYPE_ETHERNETCSMACD = 6 }; | |
3712 | ||
3713 | smap_add_format(args, "if_type", "%"PRIu32, IF_TYPE_ETHERNETCSMACD); | |
3714 | smap_add_format(args, "if_descr", "%s %s", rte_version(), | |
3715 | dev_info.driver_name); | |
03f3f9c0 OM |
3716 | smap_add_format(args, "pci-vendor_id", "0x%x", vendor_id); |
3717 | smap_add_format(args, "pci-device_id", "0x%x", device_id); | |
8a9562d2 | 3718 | |
31154f95 IS |
3719 | /* Not all link speeds are defined in the OpenFlow specs e.g. 25 Gbps. |
3720 | * In that case the speed will not be reported as part of the usual | |
3721 | * call to get_features(). Get the link speed of the device and add it | |
3722 | * to the device status in an easy to read string format. | |
3723 | */ | |
3724 | smap_add(args, "link_speed", | |
3725 | netdev_dpdk_link_speed_to_str__(link_speed)); | |
3726 | ||
f4336f50 GR |
3727 | if (dev_flags & RTE_ETH_DEV_REPRESENTOR) { |
3728 | smap_add_format(args, "dpdk-vf-mac", ETH_ADDR_FMT, | |
3729 | ETH_ADDR_ARGS(dev->hwaddr)); | |
3730 | } | |
3731 | ||
8a9562d2 PS |
3732 | return 0; |
3733 | } | |
3734 | ||
3735 | static void | |
3736 | netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state) | |
3737 | OVS_REQUIRES(dev->mutex) | |
3738 | { | |
3739 | enum netdev_flags old_flags; | |
3740 | ||
3741 | if (admin_state) { | |
3742 | netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags); | |
3743 | } else { | |
3744 | netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags); | |
3745 | } | |
3746 | } | |
3747 | ||
3748 | static void | |
3749 | netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc, | |
3750 | const char *argv[], void *aux OVS_UNUSED) | |
3751 | { | |
3752 | bool up; | |
3753 | ||
3754 | if (!strcasecmp(argv[argc - 1], "up")) { | |
3755 | up = true; | |
3756 | } else if ( !strcasecmp(argv[argc - 1], "down")) { | |
3757 | up = false; | |
3758 | } else { | |
3759 | unixctl_command_reply_error(conn, "Invalid Admin State"); | |
3760 | return; | |
3761 | } | |
3762 | ||
3763 | if (argc > 2) { | |
3764 | struct netdev *netdev = netdev_from_name(argv[1]); | |
3d0d5ab1 | 3765 | |
8a9562d2 | 3766 | if (netdev && is_dpdk_class(netdev->netdev_class)) { |
3d0d5ab1 | 3767 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 | 3768 | |
3d0d5ab1 IM |
3769 | ovs_mutex_lock(&dev->mutex); |
3770 | netdev_dpdk_set_admin_state__(dev, up); | |
3771 | ovs_mutex_unlock(&dev->mutex); | |
8a9562d2 PS |
3772 | |
3773 | netdev_close(netdev); | |
3774 | } else { | |
3775 | unixctl_command_reply_error(conn, "Not a DPDK Interface"); | |
3776 | netdev_close(netdev); | |
3777 | return; | |
3778 | } | |
3779 | } else { | |
3d0d5ab1 | 3780 | struct netdev_dpdk *dev; |
8a9562d2 PS |
3781 | |
3782 | ovs_mutex_lock(&dpdk_mutex); | |
3d0d5ab1 IM |
3783 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { |
3784 | ovs_mutex_lock(&dev->mutex); | |
3785 | netdev_dpdk_set_admin_state__(dev, up); | |
3786 | ovs_mutex_unlock(&dev->mutex); | |
8a9562d2 PS |
3787 | } |
3788 | ovs_mutex_unlock(&dpdk_mutex); | |
3789 | } | |
3790 | unixctl_command_reply(conn, "OK"); | |
3791 | } | |
3792 | ||
0ee821c2 DB |
3793 | static void |
3794 | netdev_dpdk_detach(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
3795 | const char *argv[], void *aux OVS_UNUSED) | |
3796 | { | |
0ee821c2 | 3797 | char *response; |
7ee94cba | 3798 | dpdk_port_t port_id; |
0ee821c2 | 3799 | struct netdev_dpdk *dev; |
40e940e4 OM |
3800 | struct rte_device *rte_dev; |
3801 | struct ds used_interfaces = DS_EMPTY_INITIALIZER; | |
3802 | bool used = false; | |
0ee821c2 DB |
3803 | |
3804 | ovs_mutex_lock(&dpdk_mutex); | |
3805 | ||
40e940e4 OM |
3806 | port_id = netdev_dpdk_get_port_by_devargs(argv[1]); |
3807 | if (!rte_eth_dev_is_valid_port(port_id)) { | |
0ee821c2 DB |
3808 | response = xasprintf("Device '%s' not found in DPDK", argv[1]); |
3809 | goto error; | |
3810 | } | |
3811 | ||
40e940e4 OM |
3812 | rte_dev = rte_eth_devices[port_id].device; |
3813 | ds_put_format(&used_interfaces, | |
3814 | "Device '%s' is being used by the following interfaces:", | |
3815 | argv[1]); | |
3816 | ||
3817 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { | |
3818 | /* FIXME: avoid direct access to DPDK array rte_eth_devices. */ | |
3819 | if (rte_eth_devices[dev->port_id].device == rte_dev | |
3820 | && rte_eth_devices[dev->port_id].state != RTE_ETH_DEV_UNUSED) { | |
3821 | used = true; | |
3822 | ds_put_format(&used_interfaces, " %s", | |
3823 | netdev_get_name(&dev->up)); | |
3824 | } | |
3825 | } | |
3826 | ||
3827 | if (used) { | |
3828 | ds_put_cstr(&used_interfaces, ". Remove them before detaching."); | |
3829 | response = ds_steal_cstr(&used_interfaces); | |
3830 | ds_destroy(&used_interfaces); | |
0ee821c2 DB |
3831 | goto error; |
3832 | } | |
40e940e4 | 3833 | ds_destroy(&used_interfaces); |
0ee821c2 DB |
3834 | |
3835 | rte_eth_dev_close(port_id); | |
40e940e4 | 3836 | if (rte_dev_remove(rte_dev) < 0) { |
0ee821c2 DB |
3837 | response = xasprintf("Device '%s' can not be detached", argv[1]); |
3838 | goto error; | |
3839 | } | |
3840 | ||
40e940e4 OM |
3841 | response = xasprintf("All devices shared with device '%s' " |
3842 | "have been detached", argv[1]); | |
0ee821c2 DB |
3843 | |
3844 | ovs_mutex_unlock(&dpdk_mutex); | |
3845 | unixctl_command_reply(conn, response); | |
3846 | free(response); | |
3847 | return; | |
3848 | ||
3849 | error: | |
3850 | ovs_mutex_unlock(&dpdk_mutex); | |
3851 | unixctl_command_reply_error(conn, response); | |
3852 | free(response); | |
3853 | } | |
3854 | ||
be481733 IM |
3855 | static void |
3856 | netdev_dpdk_get_mempool_info(struct unixctl_conn *conn, | |
3857 | int argc, const char *argv[], | |
3858 | void *aux OVS_UNUSED) | |
3859 | { | |
3860 | size_t size; | |
3861 | FILE *stream; | |
3862 | char *response = NULL; | |
3863 | struct netdev *netdev = NULL; | |
3864 | ||
3865 | if (argc == 2) { | |
3866 | netdev = netdev_from_name(argv[1]); | |
3867 | if (!netdev || !is_dpdk_class(netdev->netdev_class)) { | |
3868 | unixctl_command_reply_error(conn, "Not a DPDK Interface"); | |
3869 | goto out; | |
3870 | } | |
3871 | } | |
3872 | ||
3873 | stream = open_memstream(&response, &size); | |
3874 | if (!stream) { | |
3875 | response = xasprintf("Unable to open memstream: %s.", | |
3876 | ovs_strerror(errno)); | |
3877 | unixctl_command_reply_error(conn, response); | |
3878 | goto out; | |
3879 | } | |
3880 | ||
3881 | if (netdev) { | |
3882 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3883 | ||
3884 | ovs_mutex_lock(&dev->mutex); | |
3885 | ovs_mutex_lock(&dpdk_mp_mutex); | |
3886 | ||
43307ad0 | 3887 | rte_mempool_dump(stream, dev->dpdk_mp->mp); |
be481733 IM |
3888 | |
3889 | ovs_mutex_unlock(&dpdk_mp_mutex); | |
3890 | ovs_mutex_unlock(&dev->mutex); | |
3891 | } else { | |
3892 | ovs_mutex_lock(&dpdk_mp_mutex); | |
3893 | rte_mempool_list_dump(stream); | |
3894 | ovs_mutex_unlock(&dpdk_mp_mutex); | |
3895 | } | |
3896 | ||
3897 | fclose(stream); | |
3898 | ||
3899 | unixctl_command_reply(conn, response); | |
3900 | out: | |
3901 | free(response); | |
3902 | netdev_close(netdev); | |
3903 | } | |
3904 | ||
58397e6c KT |
3905 | /* |
3906 | * Set virtqueue flags so that we do not receive interrupts. | |
3907 | */ | |
3908 | static void | |
0a0f39df | 3909 | set_irq_status(int vid) |
58397e6c | 3910 | { |
4573fbd3 | 3911 | uint32_t i; |
4573fbd3 | 3912 | |
f3e7ec25 MW |
3913 | for (i = 0; i < rte_vhost_get_vring_num(vid); i++) { |
3914 | rte_vhost_enable_guest_notification(vid, i, 0); | |
4573fbd3 FL |
3915 | } |
3916 | } | |
3917 | ||
585a5bea IM |
3918 | /* |
3919 | * Fixes mapping for vhost-user tx queues. Must be called after each | |
81acebda | 3920 | * enabling/disabling of queues and n_txq modifications. |
585a5bea IM |
3921 | */ |
3922 | static void | |
d46285a2 DDP |
3923 | netdev_dpdk_remap_txqs(struct netdev_dpdk *dev) |
3924 | OVS_REQUIRES(dev->mutex) | |
585a5bea IM |
3925 | { |
3926 | int *enabled_queues, n_enabled = 0; | |
81acebda | 3927 | int i, k, total_txqs = dev->up.n_txq; |
585a5bea | 3928 | |
eff23640 | 3929 | enabled_queues = xcalloc(total_txqs, sizeof *enabled_queues); |
585a5bea IM |
3930 | |
3931 | for (i = 0; i < total_txqs; i++) { | |
3932 | /* Enabled queues always mapped to themselves. */ | |
d46285a2 | 3933 | if (dev->tx_q[i].map == i) { |
585a5bea IM |
3934 | enabled_queues[n_enabled++] = i; |
3935 | } | |
3936 | } | |
3937 | ||
3938 | if (n_enabled == 0 && total_txqs != 0) { | |
f3ea2ad2 | 3939 | enabled_queues[0] = OVS_VHOST_QUEUE_DISABLED; |
585a5bea IM |
3940 | n_enabled = 1; |
3941 | } | |
3942 | ||
3943 | k = 0; | |
3944 | for (i = 0; i < total_txqs; i++) { | |
d46285a2 DDP |
3945 | if (dev->tx_q[i].map != i) { |
3946 | dev->tx_q[i].map = enabled_queues[k]; | |
585a5bea IM |
3947 | k = (k + 1) % n_enabled; |
3948 | } | |
3949 | } | |
3950 | ||
170ef726 IM |
3951 | if (VLOG_IS_DBG_ENABLED()) { |
3952 | struct ds mapping = DS_EMPTY_INITIALIZER; | |
3953 | ||
3954 | ds_put_format(&mapping, "TX queue mapping for port '%s':\n", | |
3955 | netdev_get_name(&dev->up)); | |
3956 | for (i = 0; i < total_txqs; i++) { | |
3957 | ds_put_format(&mapping, "%2d --> %2d\n", i, dev->tx_q[i].map); | |
3958 | } | |
3959 | ||
3960 | VLOG_DBG("%s", ds_cstr(&mapping)); | |
3961 | ds_destroy(&mapping); | |
585a5bea IM |
3962 | } |
3963 | ||
eff23640 | 3964 | free(enabled_queues); |
585a5bea | 3965 | } |
4573fbd3 | 3966 | |
58397e6c KT |
3967 | /* |
3968 | * A new virtio-net device is added to a vhost port. | |
3969 | */ | |
3970 | static int | |
0a0f39df | 3971 | new_device(int vid) |
58397e6c | 3972 | { |
d46285a2 | 3973 | struct netdev_dpdk *dev; |
58397e6c | 3974 | bool exists = false; |
db8f13b0 | 3975 | int newnode = 0; |
0a0f39df CL |
3976 | char ifname[IF_NAME_SZ]; |
3977 | ||
58be5c0e | 3978 | rte_vhost_get_ifname(vid, ifname, sizeof ifname); |
58397e6c KT |
3979 | |
3980 | ovs_mutex_lock(&dpdk_mutex); | |
3981 | /* Add device to the vhost port with the same name as that passed down. */ | |
d46285a2 | 3982 | LIST_FOR_EACH(dev, list_node, &dpdk_list) { |
c1ff66ac | 3983 | ovs_mutex_lock(&dev->mutex); |
bb9d2623 IM |
3984 | if (nullable_string_is_equal(ifname, dev->vhost_id)) { |
3985 | uint32_t qp_num = rte_vhost_get_vring_num(vid) / VIRTIO_QNUM; | |
db8f13b0 CL |
3986 | |
3987 | /* Get NUMA information */ | |
0a0f39df CL |
3988 | newnode = rte_vhost_get_numa_node(vid); |
3989 | if (newnode == -1) { | |
5b9bf9e0 | 3990 | #ifdef VHOST_NUMA |
db8f13b0 | 3991 | VLOG_INFO("Error getting NUMA info for vHost Device '%s'", |
0a0f39df | 3992 | ifname); |
5b9bf9e0 | 3993 | #endif |
db8f13b0 | 3994 | newnode = dev->socket_id; |
db8f13b0 CL |
3995 | } |
3996 | ||
7235cd20 DM |
3997 | if (dev->requested_n_txq < qp_num |
3998 | || dev->requested_n_rxq < qp_num | |
7f5f2bd0 IM |
3999 | || dev->requested_socket_id != newnode) { |
4000 | dev->requested_socket_id = newnode; | |
4001 | dev->requested_n_rxq = qp_num; | |
4002 | dev->requested_n_txq = qp_num; | |
4003 | netdev_request_reconfigure(&dev->up); | |
4004 | } else { | |
4005 | /* Reconfiguration not required. */ | |
4006 | dev->vhost_reconfigured = true; | |
4007 | } | |
81acebda | 4008 | |
0a0f39df | 4009 | ovsrcu_index_set(&dev->vid, vid); |
81acebda IM |
4010 | exists = true; |
4011 | ||
58397e6c | 4012 | /* Disable notifications. */ |
0a0f39df | 4013 | set_irq_status(vid); |
e543851d | 4014 | netdev_change_seq_changed(&dev->up); |
d46285a2 | 4015 | ovs_mutex_unlock(&dev->mutex); |
58397e6c KT |
4016 | break; |
4017 | } | |
c1ff66ac | 4018 | ovs_mutex_unlock(&dev->mutex); |
58397e6c KT |
4019 | } |
4020 | ovs_mutex_unlock(&dpdk_mutex); | |
4021 | ||
4022 | if (!exists) { | |
0a0f39df | 4023 | VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname); |
58397e6c KT |
4024 | |
4025 | return -1; | |
4026 | } | |
4027 | ||
0a0f39df CL |
4028 | VLOG_INFO("vHost Device '%s' has been added on numa node %i", |
4029 | ifname, newnode); | |
4030 | ||
58397e6c KT |
4031 | return 0; |
4032 | } | |
4033 | ||
f3ea2ad2 IM |
4034 | /* Clears mapping for all available queues of vhost interface. */ |
4035 | static void | |
4036 | netdev_dpdk_txq_map_clear(struct netdev_dpdk *dev) | |
4037 | OVS_REQUIRES(dev->mutex) | |
4038 | { | |
4039 | int i; | |
4040 | ||
81acebda | 4041 | for (i = 0; i < dev->up.n_txq; i++) { |
f3ea2ad2 IM |
4042 | dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN; |
4043 | } | |
4044 | } | |
4045 | ||
58397e6c KT |
4046 | /* |
4047 | * Remove a virtio-net device from the specific vhost port. Use dev->remove | |
4048 | * flag to stop any more packets from being sent or received to/from a VM and | |
4049 | * ensure all currently queued packets have been sent/received before removing | |
4050 | * the device. | |
4051 | */ | |
4052 | static void | |
0a0f39df | 4053 | destroy_device(int vid) |
58397e6c | 4054 | { |
d46285a2 | 4055 | struct netdev_dpdk *dev; |
afee281f | 4056 | bool exists = false; |
0a0f39df CL |
4057 | char ifname[IF_NAME_SZ]; |
4058 | ||
58be5c0e | 4059 | rte_vhost_get_ifname(vid, ifname, sizeof ifname); |
58397e6c KT |
4060 | |
4061 | ovs_mutex_lock(&dpdk_mutex); | |
d46285a2 | 4062 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { |
0a0f39df | 4063 | if (netdev_dpdk_get_vid(dev) == vid) { |
58397e6c | 4064 | |
d46285a2 | 4065 | ovs_mutex_lock(&dev->mutex); |
0a0f39df CL |
4066 | dev->vhost_reconfigured = false; |
4067 | ovsrcu_index_set(&dev->vid, -1); | |
35c91567 DM |
4068 | memset(dev->vhost_rxq_enabled, 0, |
4069 | dev->up.n_rxq * sizeof *dev->vhost_rxq_enabled); | |
d46285a2 | 4070 | netdev_dpdk_txq_map_clear(dev); |
81acebda | 4071 | |
e543851d | 4072 | netdev_change_seq_changed(&dev->up); |
d46285a2 | 4073 | ovs_mutex_unlock(&dev->mutex); |
81acebda | 4074 | exists = true; |
afee281f | 4075 | break; |
58397e6c KT |
4076 | } |
4077 | } | |
afee281f | 4078 | |
58397e6c KT |
4079 | ovs_mutex_unlock(&dpdk_mutex); |
4080 | ||
0a0f39df | 4081 | if (exists) { |
afee281f KT |
4082 | /* |
4083 | * Wait for other threads to quiesce after setting the 'virtio_dev' | |
4084 | * to NULL, before returning. | |
4085 | */ | |
4086 | ovsrcu_synchronize(); | |
4087 | /* | |
4088 | * As call to ovsrcu_synchronize() will end the quiescent state, | |
4089 | * put thread back into quiescent state before returning. | |
4090 | */ | |
4091 | ovsrcu_quiesce_start(); | |
0a0f39df | 4092 | VLOG_INFO("vHost Device '%s' has been removed", ifname); |
afee281f | 4093 | } else { |
0a0f39df | 4094 | VLOG_INFO("vHost Device '%s' not found", ifname); |
afee281f | 4095 | } |
58397e6c KT |
4096 | } |
4097 | ||
585a5bea | 4098 | static int |
0a0f39df | 4099 | vring_state_changed(int vid, uint16_t queue_id, int enable) |
585a5bea | 4100 | { |
d46285a2 | 4101 | struct netdev_dpdk *dev; |
585a5bea IM |
4102 | bool exists = false; |
4103 | int qid = queue_id / VIRTIO_QNUM; | |
35c91567 | 4104 | bool is_rx = (queue_id % VIRTIO_QNUM) == VIRTIO_TXQ; |
0a0f39df CL |
4105 | char ifname[IF_NAME_SZ]; |
4106 | ||
58be5c0e | 4107 | rte_vhost_get_ifname(vid, ifname, sizeof ifname); |
585a5bea | 4108 | |
585a5bea | 4109 | ovs_mutex_lock(&dpdk_mutex); |
d46285a2 | 4110 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { |
c1ff66ac | 4111 | ovs_mutex_lock(&dev->mutex); |
bb9d2623 | 4112 | if (nullable_string_is_equal(ifname, dev->vhost_id)) { |
35c91567 DM |
4113 | if (is_rx) { |
4114 | bool old_state = dev->vhost_rxq_enabled[qid]; | |
4115 | ||
4116 | dev->vhost_rxq_enabled[qid] = enable != 0; | |
4117 | if (old_state != dev->vhost_rxq_enabled[qid]) { | |
4118 | netdev_change_seq_changed(&dev->up); | |
4119 | } | |
585a5bea | 4120 | } else { |
35c91567 DM |
4121 | if (enable) { |
4122 | dev->tx_q[qid].map = qid; | |
4123 | } else { | |
4124 | dev->tx_q[qid].map = OVS_VHOST_QUEUE_DISABLED; | |
4125 | } | |
4126 | netdev_dpdk_remap_txqs(dev); | |
585a5bea | 4127 | } |
585a5bea | 4128 | exists = true; |
d46285a2 | 4129 | ovs_mutex_unlock(&dev->mutex); |
585a5bea IM |
4130 | break; |
4131 | } | |
c1ff66ac | 4132 | ovs_mutex_unlock(&dev->mutex); |
585a5bea IM |
4133 | } |
4134 | ovs_mutex_unlock(&dpdk_mutex); | |
4135 | ||
4136 | if (exists) { | |
35c91567 DM |
4137 | VLOG_INFO("State of queue %d ( %s_qid %d ) of vhost device '%s' " |
4138 | "changed to \'%s\'", queue_id, is_rx == true ? "rx" : "tx", | |
4139 | qid, ifname, (enable == 1) ? "enabled" : "disabled"); | |
585a5bea | 4140 | } else { |
0a0f39df | 4141 | VLOG_INFO("vHost Device '%s' not found", ifname); |
585a5bea IM |
4142 | return -1; |
4143 | } | |
4144 | ||
4145 | return 0; | |
4146 | } | |
4147 | ||
61473a0e DM |
4148 | static void |
4149 | destroy_connection(int vid) | |
4150 | { | |
4151 | struct netdev_dpdk *dev; | |
4152 | char ifname[IF_NAME_SZ]; | |
4153 | bool exists = false; | |
4154 | ||
4155 | rte_vhost_get_ifname(vid, ifname, sizeof ifname); | |
4156 | ||
4157 | ovs_mutex_lock(&dpdk_mutex); | |
4158 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { | |
4159 | ovs_mutex_lock(&dev->mutex); | |
4160 | if (nullable_string_is_equal(ifname, dev->vhost_id)) { | |
4161 | uint32_t qp_num = NR_QUEUE; | |
4162 | ||
4163 | if (netdev_dpdk_get_vid(dev) >= 0) { | |
4164 | VLOG_ERR("Connection on socket '%s' destroyed while vhost " | |
4165 | "device still attached.", dev->vhost_id); | |
4166 | } | |
4167 | ||
4168 | /* Restore the number of queue pairs to default. */ | |
4169 | if (dev->requested_n_txq != qp_num | |
4170 | || dev->requested_n_rxq != qp_num) { | |
4171 | dev->requested_n_rxq = qp_num; | |
4172 | dev->requested_n_txq = qp_num; | |
4173 | netdev_request_reconfigure(&dev->up); | |
4174 | } | |
4175 | ovs_mutex_unlock(&dev->mutex); | |
4176 | exists = true; | |
4177 | break; | |
4178 | } | |
4179 | ovs_mutex_unlock(&dev->mutex); | |
4180 | } | |
4181 | ovs_mutex_unlock(&dpdk_mutex); | |
4182 | ||
4183 | if (exists) { | |
4184 | VLOG_INFO("vHost Device '%s' connection has been destroyed", ifname); | |
4185 | } else { | |
4186 | VLOG_INFO("vHost Device '%s' not found", ifname); | |
4187 | } | |
4188 | } | |
4189 | ||
3d56e4ac EC |
4190 | static |
4191 | void vhost_guest_notified(int vid OVS_UNUSED) | |
4192 | { | |
4193 | COVERAGE_INC(vhost_notification); | |
4194 | } | |
4195 | ||
8492adc2 JS |
4196 | /* |
4197 | * Retrieve the DPDK virtio device ID (vid) associated with a vhostuser | |
4198 | * or vhostuserclient netdev. | |
4199 | * | |
4200 | * Returns a value greater or equal to zero for a valid vid or '-1' if | |
4201 | * there is no valid vid associated. A vid of '-1' must not be used in | |
4202 | * rte_vhost_ APi calls. | |
4203 | * | |
4204 | * Once obtained and validated, a vid can be used by a PMD for multiple | |
4205 | * subsequent rte_vhost API calls until the PMD quiesces. A PMD should | |
4206 | * not fetch the vid again for each of a series of API calls. | |
4207 | */ | |
4208 | ||
0a0f39df CL |
4209 | int |
4210 | netdev_dpdk_get_vid(const struct netdev_dpdk *dev) | |
58397e6c | 4211 | { |
0a0f39df | 4212 | return ovsrcu_index_get(&dev->vid); |
58397e6c KT |
4213 | } |
4214 | ||
9509913a IS |
4215 | struct ingress_policer * |
4216 | netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev) | |
4217 | { | |
4218 | return ovsrcu_get(struct ingress_policer *, &dev->ingress_policer); | |
4219 | } | |
4220 | ||
58397e6c | 4221 | static int |
ecc1a34e | 4222 | netdev_dpdk_class_init(void) |
7d1ced01 | 4223 | { |
ecc1a34e DDP |
4224 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
4225 | ||
4226 | /* This function can be called for different classes. The initialization | |
4227 | * needs to be done only once */ | |
4228 | if (ovsthread_once_start(&once)) { | |
988fd463 EC |
4229 | int ret; |
4230 | ||
ecc1a34e DDP |
4231 | ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL); |
4232 | unixctl_command_register("netdev-dpdk/set-admin-state", | |
4233 | "[netdev] up|down", 1, 2, | |
4234 | netdev_dpdk_set_admin_state, NULL); | |
4235 | ||
0ee821c2 DB |
4236 | unixctl_command_register("netdev-dpdk/detach", |
4237 | "pci address of device", 1, 1, | |
4238 | netdev_dpdk_detach, NULL); | |
4239 | ||
be481733 IM |
4240 | unixctl_command_register("netdev-dpdk/get-mempool-info", |
4241 | "[netdev]", 0, 1, | |
4242 | netdev_dpdk_get_mempool_info, NULL); | |
4243 | ||
988fd463 EC |
4244 | ret = rte_eth_dev_callback_register(RTE_ETH_ALL, |
4245 | RTE_ETH_EVENT_INTR_RESET, | |
4246 | dpdk_eth_event_callback, NULL); | |
4247 | if (ret != 0) { | |
4248 | VLOG_ERR("Ethernet device callback register error: %s", | |
4249 | rte_strerror(-ret)); | |
4250 | } | |
4251 | ||
ecc1a34e DDP |
4252 | ovsthread_once_done(&once); |
4253 | } | |
362ca396 | 4254 | |
7d1ced01 CL |
4255 | return 0; |
4256 | } | |
4257 | ||
0bf765f7 IS |
4258 | /* QoS Functions */ |
4259 | ||
4260 | /* | |
4261 | * Initialize QoS configuration operations. | |
4262 | */ | |
4263 | static void | |
4264 | qos_conf_init(struct qos_conf *conf, const struct dpdk_qos_ops *ops) | |
4265 | { | |
4266 | conf->ops = ops; | |
78bd47cf | 4267 | rte_spinlock_init(&conf->lock); |
0bf765f7 IS |
4268 | } |
4269 | ||
4270 | /* | |
4271 | * Search existing QoS operations in qos_ops and compare each set of | |
4272 | * operations qos_name to name. Return a dpdk_qos_ops pointer to a match, | |
4273 | * else return NULL | |
4274 | */ | |
4275 | static const struct dpdk_qos_ops * | |
4276 | qos_lookup_name(const char *name) | |
4277 | { | |
4278 | const struct dpdk_qos_ops *const *opsp; | |
4279 | ||
4280 | for (opsp = qos_confs; *opsp != NULL; opsp++) { | |
4281 | const struct dpdk_qos_ops *ops = *opsp; | |
4282 | if (!strcmp(name, ops->qos_name)) { | |
4283 | return ops; | |
4284 | } | |
4285 | } | |
4286 | return NULL; | |
4287 | } | |
4288 | ||
0bf765f7 IS |
4289 | static int |
4290 | netdev_dpdk_get_qos_types(const struct netdev *netdev OVS_UNUSED, | |
4291 | struct sset *types) | |
4292 | { | |
4293 | const struct dpdk_qos_ops *const *opsp; | |
4294 | ||
4295 | for (opsp = qos_confs; *opsp != NULL; opsp++) { | |
4296 | const struct dpdk_qos_ops *ops = *opsp; | |
4297 | if (ops->qos_construct && ops->qos_name[0] != '\0') { | |
4298 | sset_add(types, ops->qos_name); | |
4299 | } | |
4300 | } | |
4301 | return 0; | |
4302 | } | |
4303 | ||
4304 | static int | |
d46285a2 | 4305 | netdev_dpdk_get_qos(const struct netdev *netdev, |
0bf765f7 IS |
4306 | const char **typep, struct smap *details) |
4307 | { | |
d46285a2 | 4308 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
78bd47cf | 4309 | struct qos_conf *qos_conf; |
0bf765f7 IS |
4310 | int error = 0; |
4311 | ||
d46285a2 | 4312 | ovs_mutex_lock(&dev->mutex); |
78bd47cf DDP |
4313 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); |
4314 | if (qos_conf) { | |
4315 | *typep = qos_conf->ops->qos_name; | |
4316 | error = (qos_conf->ops->qos_get | |
4317 | ? qos_conf->ops->qos_get(qos_conf, details): 0); | |
d03603c4 MC |
4318 | } else { |
4319 | /* No QoS configuration set, return an empty string */ | |
4320 | *typep = ""; | |
0bf765f7 | 4321 | } |
d46285a2 | 4322 | ovs_mutex_unlock(&dev->mutex); |
0bf765f7 IS |
4323 | |
4324 | return error; | |
4325 | } | |
4326 | ||
4327 | static int | |
78bd47cf DDP |
4328 | netdev_dpdk_set_qos(struct netdev *netdev, const char *type, |
4329 | const struct smap *details) | |
0bf765f7 | 4330 | { |
d46285a2 | 4331 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
0bf765f7 | 4332 | const struct dpdk_qos_ops *new_ops = NULL; |
78bd47cf | 4333 | struct qos_conf *qos_conf, *new_qos_conf = NULL; |
0bf765f7 IS |
4334 | int error = 0; |
4335 | ||
d46285a2 | 4336 | ovs_mutex_lock(&dev->mutex); |
0bf765f7 | 4337 | |
78bd47cf | 4338 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); |
0bf765f7 | 4339 | |
78bd47cf DDP |
4340 | new_ops = qos_lookup_name(type); |
4341 | ||
4342 | if (!new_ops || !new_ops->qos_construct) { | |
4343 | new_qos_conf = NULL; | |
4344 | if (type && type[0]) { | |
4345 | error = EOPNOTSUPP; | |
0bf765f7 | 4346 | } |
44975bb0 | 4347 | } else if (qos_conf && qos_conf->ops == new_ops |
78bd47cf DDP |
4348 | && qos_conf->ops->qos_is_equal(qos_conf, details)) { |
4349 | new_qos_conf = qos_conf; | |
0bf765f7 | 4350 | } else { |
78bd47cf | 4351 | error = new_ops->qos_construct(details, &new_qos_conf); |
7ea266e9 IS |
4352 | } |
4353 | ||
7ea266e9 | 4354 | if (error) { |
78bd47cf DDP |
4355 | VLOG_ERR("Failed to set QoS type %s on port %s: %s", |
4356 | type, netdev->name, rte_strerror(error)); | |
4357 | } | |
4358 | ||
4359 | if (new_qos_conf != qos_conf) { | |
4360 | ovsrcu_set(&dev->qos_conf, new_qos_conf); | |
4361 | if (qos_conf) { | |
4362 | ovsrcu_postpone(qos_conf->ops->qos_destruct, qos_conf); | |
4363 | } | |
0bf765f7 IS |
4364 | } |
4365 | ||
d46285a2 | 4366 | ovs_mutex_unlock(&dev->mutex); |
78bd47cf | 4367 | |
0bf765f7 IS |
4368 | return error; |
4369 | } | |
4370 | ||
23c01b19 EC |
4371 | static int |
4372 | netdev_dpdk_get_queue(const struct netdev *netdev, uint32_t queue_id, | |
4373 | struct smap *details) | |
4374 | { | |
4375 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
4376 | struct qos_conf *qos_conf; | |
4377 | int error = 0; | |
4378 | ||
4379 | ovs_mutex_lock(&dev->mutex); | |
4380 | ||
4381 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); | |
4382 | if (!qos_conf || !qos_conf->ops || !qos_conf->ops->qos_queue_get) { | |
4383 | error = EOPNOTSUPP; | |
4384 | } else { | |
4385 | error = qos_conf->ops->qos_queue_get(details, queue_id, qos_conf); | |
4386 | } | |
4387 | ||
4388 | ovs_mutex_unlock(&dev->mutex); | |
4389 | ||
4390 | return error; | |
4391 | } | |
4392 | ||
4393 | static int | |
4394 | netdev_dpdk_set_queue(struct netdev *netdev, uint32_t queue_id, | |
4395 | const struct smap *details) | |
4396 | { | |
4397 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
4398 | struct qos_conf *qos_conf; | |
4399 | int error = 0; | |
4400 | ||
4401 | ovs_mutex_lock(&dev->mutex); | |
4402 | ||
4403 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); | |
4404 | if (!qos_conf || !qos_conf->ops || !qos_conf->ops->qos_queue_construct) { | |
4405 | error = EOPNOTSUPP; | |
4406 | } else { | |
4407 | error = qos_conf->ops->qos_queue_construct(details, queue_id, | |
4408 | qos_conf); | |
4409 | } | |
4410 | ||
4411 | if (error && error != EOPNOTSUPP) { | |
4412 | VLOG_ERR("Failed to set QoS queue %d on port %s: %s", | |
4413 | queue_id, netdev_get_name(netdev), rte_strerror(error)); | |
4414 | } | |
4415 | ||
4416 | ovs_mutex_unlock(&dev->mutex); | |
4417 | ||
4418 | return error; | |
4419 | } | |
4420 | ||
4421 | static int | |
4422 | netdev_dpdk_delete_queue(struct netdev *netdev, uint32_t queue_id) | |
4423 | { | |
4424 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
4425 | struct qos_conf *qos_conf; | |
4426 | int error = 0; | |
4427 | ||
4428 | ovs_mutex_lock(&dev->mutex); | |
4429 | ||
4430 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); | |
4431 | if (qos_conf && qos_conf->ops && qos_conf->ops->qos_queue_destruct) { | |
4432 | qos_conf->ops->qos_queue_destruct(qos_conf, queue_id); | |
4433 | } else { | |
4434 | error = EOPNOTSUPP; | |
4435 | } | |
4436 | ||
4437 | ovs_mutex_unlock(&dev->mutex); | |
4438 | ||
4439 | return error; | |
4440 | } | |
4441 | ||
4442 | static int | |
4443 | netdev_dpdk_get_queue_stats(const struct netdev *netdev, uint32_t queue_id, | |
4444 | struct netdev_queue_stats *stats) | |
4445 | { | |
4446 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
4447 | struct qos_conf *qos_conf; | |
4448 | int error = 0; | |
4449 | ||
4450 | ovs_mutex_lock(&dev->mutex); | |
4451 | ||
4452 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); | |
4453 | if (qos_conf && qos_conf->ops && qos_conf->ops->qos_queue_get_stats) { | |
4454 | qos_conf->ops->qos_queue_get_stats(qos_conf, queue_id, stats); | |
4455 | } else { | |
4456 | error = EOPNOTSUPP; | |
4457 | } | |
4458 | ||
4459 | ovs_mutex_unlock(&dev->mutex); | |
4460 | ||
4461 | return error; | |
4462 | } | |
4463 | ||
4464 | static int | |
4465 | netdev_dpdk_queue_dump_start(const struct netdev *netdev, void **statep) | |
4466 | { | |
4467 | int error = 0; | |
4468 | struct qos_conf *qos_conf; | |
4469 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
4470 | ||
4471 | ovs_mutex_lock(&dev->mutex); | |
4472 | ||
4473 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); | |
4474 | if (qos_conf && qos_conf->ops | |
4475 | && qos_conf->ops->qos_queue_dump_state_init) { | |
4476 | struct netdev_dpdk_queue_state *state; | |
4477 | ||
4478 | *statep = state = xmalloc(sizeof *state); | |
4479 | error = qos_conf->ops->qos_queue_dump_state_init(qos_conf, state); | |
4480 | } else { | |
4481 | error = EOPNOTSUPP; | |
4482 | } | |
4483 | ||
4484 | ovs_mutex_unlock(&dev->mutex); | |
4485 | ||
4486 | return error; | |
4487 | } | |
4488 | ||
4489 | static int | |
4490 | netdev_dpdk_queue_dump_next(const struct netdev *netdev, void *state_, | |
4491 | uint32_t *queue_idp, struct smap *details) | |
4492 | { | |
4493 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
4494 | struct netdev_dpdk_queue_state *state = state_; | |
4495 | struct qos_conf *qos_conf; | |
4496 | int error = EOF; | |
4497 | ||
4498 | ovs_mutex_lock(&dev->mutex); | |
4499 | ||
4500 | while (state->cur_queue < state->n_queues) { | |
4501 | uint32_t queue_id = state->queues[state->cur_queue++]; | |
4502 | ||
4503 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); | |
4504 | if (qos_conf && qos_conf->ops && qos_conf->ops->qos_queue_get) { | |
4505 | *queue_idp = queue_id; | |
4506 | error = qos_conf->ops->qos_queue_get(details, queue_id, qos_conf); | |
4507 | break; | |
4508 | } | |
4509 | } | |
4510 | ||
4511 | ovs_mutex_unlock(&dev->mutex); | |
4512 | ||
4513 | return error; | |
4514 | } | |
4515 | ||
4516 | static int | |
4517 | netdev_dpdk_queue_dump_done(const struct netdev *netdev OVS_UNUSED, | |
4518 | void *state_) | |
4519 | { | |
4520 | struct netdev_dpdk_queue_state *state = state_; | |
4521 | ||
4522 | free(state->queues); | |
4523 | free(state); | |
4524 | return 0; | |
4525 | } | |
4526 | ||
4527 | ||
4528 | ||
0bf765f7 IS |
4529 | /* egress-policer details */ |
4530 | ||
4531 | struct egress_policer { | |
4532 | struct qos_conf qos_conf; | |
4533 | struct rte_meter_srtcm_params app_srtcm_params; | |
4534 | struct rte_meter_srtcm egress_meter; | |
03f3f9c0 | 4535 | struct rte_meter_srtcm_profile egress_prof; |
0bf765f7 IS |
4536 | }; |
4537 | ||
78bd47cf DDP |
4538 | static void |
4539 | egress_policer_details_to_param(const struct smap *details, | |
4540 | struct rte_meter_srtcm_params *params) | |
0bf765f7 | 4541 | { |
78bd47cf DDP |
4542 | memset(params, 0, sizeof *params); |
4543 | params->cir = smap_get_ullong(details, "cir", 0); | |
4544 | params->cbs = smap_get_ullong(details, "cbs", 0); | |
4545 | params->ebs = 0; | |
0bf765f7 IS |
4546 | } |
4547 | ||
4548 | static int | |
78bd47cf DDP |
4549 | egress_policer_qos_construct(const struct smap *details, |
4550 | struct qos_conf **conf) | |
0bf765f7 | 4551 | { |
0bf765f7 | 4552 | struct egress_policer *policer; |
0bf765f7 IS |
4553 | int err = 0; |
4554 | ||
0bf765f7 IS |
4555 | policer = xmalloc(sizeof *policer); |
4556 | qos_conf_init(&policer->qos_conf, &egress_policer_ops); | |
78bd47cf | 4557 | egress_policer_details_to_param(details, &policer->app_srtcm_params); |
03f3f9c0 OM |
4558 | err = rte_meter_srtcm_profile_config(&policer->egress_prof, |
4559 | &policer->app_srtcm_params); | |
4560 | if (!err) { | |
4561 | err = rte_meter_srtcm_config(&policer->egress_meter, | |
4562 | &policer->egress_prof); | |
4563 | } | |
4564 | ||
78bd47cf DDP |
4565 | if (!err) { |
4566 | *conf = &policer->qos_conf; | |
4567 | } else { | |
03f3f9c0 | 4568 | VLOG_ERR("Could not create rte meter for egress policer"); |
7ea266e9 | 4569 | free(policer); |
78bd47cf | 4570 | *conf = NULL; |
7ea266e9 IS |
4571 | err = -err; |
4572 | } | |
0bf765f7 IS |
4573 | |
4574 | return err; | |
4575 | } | |
4576 | ||
4577 | static void | |
78bd47cf | 4578 | egress_policer_qos_destruct(struct qos_conf *conf) |
0bf765f7 IS |
4579 | { |
4580 | struct egress_policer *policer = CONTAINER_OF(conf, struct egress_policer, | |
78bd47cf | 4581 | qos_conf); |
0bf765f7 IS |
4582 | free(policer); |
4583 | } | |
4584 | ||
4585 | static int | |
78bd47cf | 4586 | egress_policer_qos_get(const struct qos_conf *conf, struct smap *details) |
0bf765f7 | 4587 | { |
78bd47cf DDP |
4588 | struct egress_policer *policer = |
4589 | CONTAINER_OF(conf, struct egress_policer, qos_conf); | |
4590 | ||
4591 | smap_add_format(details, "cir", "%"PRIu64, policer->app_srtcm_params.cir); | |
4592 | smap_add_format(details, "cbs", "%"PRIu64, policer->app_srtcm_params.cbs); | |
050c60bf | 4593 | |
0bf765f7 IS |
4594 | return 0; |
4595 | } | |
4596 | ||
78bd47cf | 4597 | static bool |
47a45d86 KT |
4598 | egress_policer_qos_is_equal(const struct qos_conf *conf, |
4599 | const struct smap *details) | |
0bf765f7 | 4600 | { |
78bd47cf DDP |
4601 | struct egress_policer *policer = |
4602 | CONTAINER_OF(conf, struct egress_policer, qos_conf); | |
4603 | struct rte_meter_srtcm_params params; | |
0bf765f7 | 4604 | |
78bd47cf | 4605 | egress_policer_details_to_param(details, ¶ms); |
7ea266e9 | 4606 | |
78bd47cf | 4607 | return !memcmp(¶ms, &policer->app_srtcm_params, sizeof params); |
0bf765f7 IS |
4608 | } |
4609 | ||
0bf765f7 | 4610 | static int |
3e90f7d7 | 4611 | egress_policer_run(struct qos_conf *conf, struct rte_mbuf **pkts, int pkt_cnt, |
7d7ded7a | 4612 | bool should_steal) |
0bf765f7 | 4613 | { |
0bf765f7 | 4614 | int cnt = 0; |
78bd47cf DDP |
4615 | struct egress_policer *policer = |
4616 | CONTAINER_OF(conf, struct egress_policer, qos_conf); | |
0bf765f7 | 4617 | |
e61bdffc EC |
4618 | cnt = srtcm_policer_run_single_packet(&policer->egress_meter, |
4619 | &policer->egress_prof, pkts, | |
4620 | pkt_cnt, should_steal); | |
0bf765f7 IS |
4621 | |
4622 | return cnt; | |
4623 | } | |
4624 | ||
4625 | static const struct dpdk_qos_ops egress_policer_ops = { | |
23c01b19 EC |
4626 | .qos_name = "egress-policer", /* qos_name */ |
4627 | .qos_construct = egress_policer_qos_construct, | |
4628 | .qos_destruct = egress_policer_qos_destruct, | |
4629 | .qos_get = egress_policer_qos_get, | |
4630 | .qos_is_equal = egress_policer_qos_is_equal, | |
4631 | .qos_run = egress_policer_run | |
0bf765f7 IS |
4632 | }; |
4633 | ||
e61bdffc EC |
4634 | /* trtcm-policer details */ |
4635 | ||
4636 | struct trtcm_policer { | |
4637 | struct qos_conf qos_conf; | |
4638 | struct rte_meter_trtcm_rfc4115_params meter_params; | |
4639 | struct rte_meter_trtcm_rfc4115_profile meter_profile; | |
4640 | struct rte_meter_trtcm_rfc4115 meter; | |
4641 | struct netdev_queue_stats stats; | |
4642 | struct hmap queues; | |
4643 | }; | |
4644 | ||
4645 | struct trtcm_policer_queue { | |
4646 | struct hmap_node hmap_node; | |
4647 | uint32_t queue_id; | |
4648 | struct rte_meter_trtcm_rfc4115_params meter_params; | |
4649 | struct rte_meter_trtcm_rfc4115_profile meter_profile; | |
4650 | struct rte_meter_trtcm_rfc4115 meter; | |
4651 | struct netdev_queue_stats stats; | |
4652 | }; | |
4653 | ||
4654 | static void | |
4655 | trtcm_policer_details_to_param(const struct smap *details, | |
4656 | struct rte_meter_trtcm_rfc4115_params *params) | |
4657 | { | |
4658 | memset(params, 0, sizeof *params); | |
4659 | params->cir = smap_get_ullong(details, "cir", 0); | |
4660 | params->eir = smap_get_ullong(details, "eir", 0); | |
4661 | params->cbs = smap_get_ullong(details, "cbs", 0); | |
4662 | params->ebs = smap_get_ullong(details, "ebs", 0); | |
4663 | } | |
4664 | ||
4665 | static void | |
4666 | trtcm_policer_param_to_detail( | |
4667 | const struct rte_meter_trtcm_rfc4115_params *params, | |
4668 | struct smap *details) | |
4669 | { | |
4670 | smap_add_format(details, "cir", "%"PRIu64, params->cir); | |
4671 | smap_add_format(details, "eir", "%"PRIu64, params->eir); | |
4672 | smap_add_format(details, "cbs", "%"PRIu64, params->cbs); | |
4673 | smap_add_format(details, "ebs", "%"PRIu64, params->ebs); | |
4674 | } | |
4675 | ||
4676 | ||
4677 | static int | |
4678 | trtcm_policer_qos_construct(const struct smap *details, | |
4679 | struct qos_conf **conf) | |
4680 | { | |
4681 | struct trtcm_policer *policer; | |
4682 | int err = 0; | |
4683 | ||
4684 | policer = xmalloc(sizeof *policer); | |
4685 | qos_conf_init(&policer->qos_conf, &trtcm_policer_ops); | |
4686 | trtcm_policer_details_to_param(details, &policer->meter_params); | |
4687 | err = rte_meter_trtcm_rfc4115_profile_config(&policer->meter_profile, | |
4688 | &policer->meter_params); | |
4689 | if (!err) { | |
4690 | err = rte_meter_trtcm_rfc4115_config(&policer->meter, | |
4691 | &policer->meter_profile); | |
4692 | } | |
4693 | ||
4694 | if (!err) { | |
4695 | *conf = &policer->qos_conf; | |
4696 | memset(&policer->stats, 0, sizeof policer->stats); | |
4697 | hmap_init(&policer->queues); | |
4698 | } else { | |
4699 | free(policer); | |
4700 | *conf = NULL; | |
4701 | err = -err; | |
4702 | } | |
4703 | ||
4704 | return err; | |
4705 | } | |
4706 | ||
4707 | static void | |
4708 | trtcm_policer_qos_destruct(struct qos_conf *conf) | |
4709 | { | |
4710 | struct trtcm_policer_queue *queue, *next_queue; | |
4711 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4712 | qos_conf); | |
4713 | ||
4714 | HMAP_FOR_EACH_SAFE (queue, next_queue, hmap_node, &policer->queues) { | |
4715 | hmap_remove(&policer->queues, &queue->hmap_node); | |
4716 | free(queue); | |
4717 | } | |
4718 | hmap_destroy(&policer->queues); | |
4719 | free(policer); | |
4720 | } | |
4721 | ||
4722 | static int | |
4723 | trtcm_policer_qos_get(const struct qos_conf *conf, struct smap *details) | |
4724 | { | |
4725 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4726 | qos_conf); | |
4727 | ||
4728 | trtcm_policer_param_to_detail(&policer->meter_params, details); | |
4729 | return 0; | |
4730 | } | |
4731 | ||
4732 | static bool | |
4733 | trtcm_policer_qos_is_equal(const struct qos_conf *conf, | |
4734 | const struct smap *details) | |
4735 | { | |
4736 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4737 | qos_conf); | |
4738 | struct rte_meter_trtcm_rfc4115_params params; | |
4739 | ||
4740 | trtcm_policer_details_to_param(details, ¶ms); | |
4741 | ||
4742 | return !memcmp(¶ms, &policer->meter_params, sizeof params); | |
4743 | } | |
4744 | ||
4745 | static struct trtcm_policer_queue * | |
4746 | trtcm_policer_qos_find_queue(struct trtcm_policer *policer, uint32_t queue_id) | |
4747 | { | |
4748 | struct trtcm_policer_queue *queue; | |
4749 | HMAP_FOR_EACH_WITH_HASH (queue, hmap_node, hash_2words(queue_id, 0), | |
4750 | &policer->queues) { | |
4751 | if (queue->queue_id == queue_id) { | |
4752 | return queue; | |
4753 | } | |
4754 | } | |
4755 | return NULL; | |
4756 | } | |
4757 | ||
4758 | static inline bool | |
4759 | trtcm_policer_run_single_packet(struct trtcm_policer *policer, | |
4760 | struct rte_mbuf *pkt, uint64_t time) | |
4761 | { | |
4762 | enum rte_color pkt_color; | |
4763 | struct trtcm_policer_queue *queue; | |
4764 | uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct rte_ether_hdr); | |
4765 | struct dp_packet *dpkt = CONTAINER_OF(pkt, struct dp_packet, mbuf); | |
4766 | ||
4767 | queue = trtcm_policer_qos_find_queue(policer, dpkt->md.skb_priority); | |
4768 | if (!queue) { | |
4769 | /* If no queue is found, use the default queue, which MUST exist. */ | |
4770 | queue = trtcm_policer_qos_find_queue(policer, 0); | |
4771 | if (!queue) { | |
4772 | return false; | |
4773 | } | |
4774 | } | |
4775 | ||
4776 | pkt_color = rte_meter_trtcm_rfc4115_color_blind_check(&queue->meter, | |
4777 | &queue->meter_profile, | |
4778 | time, | |
4779 | pkt_len); | |
4780 | ||
4781 | if (pkt_color == RTE_COLOR_RED) { | |
4782 | queue->stats.tx_errors++; | |
4783 | } else { | |
4784 | queue->stats.tx_bytes += pkt_len; | |
4785 | queue->stats.tx_packets++; | |
4786 | } | |
4787 | ||
4788 | pkt_color = rte_meter_trtcm_rfc4115_color_aware_check(&policer->meter, | |
4789 | &policer->meter_profile, | |
4790 | time, pkt_len, | |
4791 | pkt_color); | |
4792 | ||
4793 | if (pkt_color == RTE_COLOR_RED) { | |
4794 | policer->stats.tx_errors++; | |
4795 | return false; | |
4796 | } | |
4797 | ||
4798 | policer->stats.tx_bytes += pkt_len; | |
4799 | policer->stats.tx_packets++; | |
4800 | return true; | |
4801 | } | |
4802 | ||
4803 | static int | |
4804 | trtcm_policer_run(struct qos_conf *conf, struct rte_mbuf **pkts, int pkt_cnt, | |
4805 | bool should_steal) | |
4806 | { | |
4807 | int i = 0; | |
4808 | int cnt = 0; | |
4809 | struct rte_mbuf *pkt = NULL; | |
4810 | uint64_t current_time = rte_rdtsc(); | |
4811 | ||
4812 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4813 | qos_conf); | |
4814 | ||
4815 | for (i = 0; i < pkt_cnt; i++) { | |
4816 | pkt = pkts[i]; | |
4817 | ||
4818 | if (trtcm_policer_run_single_packet(policer, pkt, current_time)) { | |
4819 | if (cnt != i) { | |
4820 | pkts[cnt] = pkt; | |
4821 | } | |
4822 | cnt++; | |
4823 | } else { | |
4824 | if (should_steal) { | |
4825 | rte_pktmbuf_free(pkt); | |
4826 | } | |
4827 | } | |
4828 | } | |
4829 | return cnt; | |
4830 | } | |
4831 | ||
4832 | static int | |
4833 | trtcm_policer_qos_queue_construct(const struct smap *details, | |
4834 | uint32_t queue_id, struct qos_conf *conf) | |
4835 | { | |
4836 | int err = 0; | |
4837 | struct trtcm_policer_queue *queue; | |
4838 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4839 | qos_conf); | |
4840 | ||
4841 | queue = trtcm_policer_qos_find_queue(policer, queue_id); | |
4842 | if (!queue) { | |
4843 | queue = xmalloc(sizeof *queue); | |
4844 | queue->queue_id = queue_id; | |
4845 | memset(&queue->stats, 0, sizeof queue->stats); | |
4846 | queue->stats.created = time_msec(); | |
4847 | hmap_insert(&policer->queues, &queue->hmap_node, | |
4848 | hash_2words(queue_id, 0)); | |
4849 | } | |
4850 | if (queue_id == 0 && smap_is_empty(details)) { | |
4851 | /* No default queue configured, use port values */ | |
4852 | memcpy(&queue->meter_params, &policer->meter_params, | |
4853 | sizeof queue->meter_params); | |
4854 | } else { | |
4855 | trtcm_policer_details_to_param(details, &queue->meter_params); | |
4856 | } | |
4857 | ||
4858 | err = rte_meter_trtcm_rfc4115_profile_config(&queue->meter_profile, | |
4859 | &queue->meter_params); | |
4860 | ||
4861 | if (!err) { | |
4862 | err = rte_meter_trtcm_rfc4115_config(&queue->meter, | |
4863 | &queue->meter_profile); | |
4864 | } | |
4865 | if (err) { | |
4866 | hmap_remove(&policer->queues, &queue->hmap_node); | |
4867 | free(queue); | |
4868 | err = -err; | |
4869 | } | |
4870 | return err; | |
4871 | } | |
4872 | ||
4873 | static void | |
4874 | trtcm_policer_qos_queue_destruct(struct qos_conf *conf, uint32_t queue_id) | |
4875 | { | |
4876 | struct trtcm_policer_queue *queue; | |
4877 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4878 | qos_conf); | |
4879 | ||
4880 | queue = trtcm_policer_qos_find_queue(policer, queue_id); | |
4881 | if (queue) { | |
4882 | hmap_remove(&policer->queues, &queue->hmap_node); | |
4883 | free(queue); | |
4884 | } | |
4885 | } | |
4886 | ||
4887 | static int | |
4888 | trtcm_policer_qos_queue_get(struct smap *details, uint32_t queue_id, | |
4889 | const struct qos_conf *conf) | |
4890 | { | |
4891 | struct trtcm_policer_queue *queue; | |
4892 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4893 | qos_conf); | |
4894 | ||
4895 | queue = trtcm_policer_qos_find_queue(policer, queue_id); | |
4896 | if (!queue) { | |
4897 | return EINVAL; | |
4898 | } | |
4899 | ||
4900 | trtcm_policer_param_to_detail(&queue->meter_params, details); | |
4901 | return 0; | |
4902 | } | |
4903 | ||
4904 | static int | |
4905 | trtcm_policer_qos_queue_get_stats(const struct qos_conf *conf, | |
4906 | uint32_t queue_id, | |
4907 | struct netdev_queue_stats *stats) | |
4908 | { | |
4909 | struct trtcm_policer_queue *queue; | |
4910 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4911 | qos_conf); | |
4912 | ||
4913 | queue = trtcm_policer_qos_find_queue(policer, queue_id); | |
4914 | if (!queue) { | |
4915 | return EINVAL; | |
4916 | } | |
4917 | memcpy(stats, &queue->stats, sizeof *stats); | |
4918 | return 0; | |
4919 | } | |
4920 | ||
4921 | static int | |
4922 | trtcm_policer_qos_queue_dump_state_init(const struct qos_conf *conf, | |
4923 | struct netdev_dpdk_queue_state *state) | |
4924 | { | |
4925 | uint32_t i = 0; | |
4926 | struct trtcm_policer_queue *queue; | |
4927 | struct trtcm_policer *policer = CONTAINER_OF(conf, struct trtcm_policer, | |
4928 | qos_conf); | |
4929 | ||
4930 | state->n_queues = hmap_count(&policer->queues); | |
4931 | state->cur_queue = 0; | |
4932 | state->queues = xmalloc(state->n_queues * sizeof *state->queues); | |
4933 | ||
4934 | HMAP_FOR_EACH (queue, hmap_node, &policer->queues) { | |
4935 | state->queues[i++] = queue->queue_id; | |
4936 | } | |
4937 | return 0; | |
4938 | } | |
4939 | ||
4940 | static const struct dpdk_qos_ops trtcm_policer_ops = { | |
4941 | .qos_name = "trtcm-policer", | |
4942 | .qos_construct = trtcm_policer_qos_construct, | |
4943 | .qos_destruct = trtcm_policer_qos_destruct, | |
4944 | .qos_get = trtcm_policer_qos_get, | |
4945 | .qos_is_equal = trtcm_policer_qos_is_equal, | |
4946 | .qos_run = trtcm_policer_run, | |
4947 | .qos_queue_construct = trtcm_policer_qos_queue_construct, | |
4948 | .qos_queue_destruct = trtcm_policer_qos_queue_destruct, | |
4949 | .qos_queue_get = trtcm_policer_qos_queue_get, | |
4950 | .qos_queue_get_stats = trtcm_policer_qos_queue_get_stats, | |
4951 | .qos_queue_dump_state_init = trtcm_policer_qos_queue_dump_state_init | |
4952 | }; | |
4953 | ||
050c60bf DDP |
4954 | static int |
4955 | netdev_dpdk_reconfigure(struct netdev *netdev) | |
4956 | { | |
4957 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
4958 | int err = 0; | |
4959 | ||
050c60bf DDP |
4960 | ovs_mutex_lock(&dev->mutex); |
4961 | ||
4962 | if (netdev->n_txq == dev->requested_n_txq | |
0072e931 | 4963 | && netdev->n_rxq == dev->requested_n_rxq |
b685696b | 4964 | && dev->mtu == dev->requested_mtu |
f8b64a61 | 4965 | && dev->lsc_interrupt_mode == dev->requested_lsc_interrupt_mode |
b685696b | 4966 | && dev->rxq_size == dev->requested_rxq_size |
bd4e172b | 4967 | && dev->txq_size == dev->requested_txq_size |
f4336f50 | 4968 | && eth_addr_equals(dev->hwaddr, dev->requested_hwaddr) |
606f6650 | 4969 | && dev->socket_id == dev->requested_socket_id |
988fd463 | 4970 | && dev->started && !dev->reset_needed) { |
050c60bf DDP |
4971 | /* Reconfiguration is unnecessary */ |
4972 | ||
4973 | goto out; | |
4974 | } | |
4975 | ||
988fd463 EC |
4976 | if (dev->reset_needed) { |
4977 | rte_eth_dev_reset(dev->port_id); | |
4978 | if_notifier_manual_report(); | |
4979 | dev->reset_needed = false; | |
4980 | } else { | |
4981 | rte_eth_dev_stop(dev->port_id); | |
4982 | } | |
4983 | ||
606f6650 | 4984 | dev->started = false; |
050c60bf | 4985 | |
d555d9bd | 4986 | err = netdev_dpdk_mempool_configure(dev); |
b6b26021 | 4987 | if (err && err != EEXIST) { |
d555d9bd | 4988 | goto out; |
0072e931 MK |
4989 | } |
4990 | ||
f8b64a61 RM |
4991 | dev->lsc_interrupt_mode = dev->requested_lsc_interrupt_mode; |
4992 | ||
050c60bf DDP |
4993 | netdev->n_txq = dev->requested_n_txq; |
4994 | netdev->n_rxq = dev->requested_n_rxq; | |
4995 | ||
b685696b CL |
4996 | dev->rxq_size = dev->requested_rxq_size; |
4997 | dev->txq_size = dev->requested_txq_size; | |
4998 | ||
050c60bf | 4999 | rte_free(dev->tx_q); |
f4336f50 GR |
5000 | |
5001 | if (!eth_addr_equals(dev->hwaddr, dev->requested_hwaddr)) { | |
5002 | err = netdev_dpdk_set_etheraddr__(dev, dev->requested_hwaddr); | |
5003 | if (err) { | |
5004 | goto out; | |
5005 | } | |
5006 | } | |
5007 | ||
050c60bf | 5008 | err = dpdk_eth_dev_init(dev); |
29cf9c1b FL |
5009 | if (dev->hw_ol_features & NETDEV_TX_TSO_OFFLOAD) { |
5010 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_TCP_TSO; | |
5011 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_TCP_CKSUM; | |
8c5163fe | 5012 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_UDP_CKSUM; |
29cf9c1b | 5013 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_IPV4_CKSUM; |
35b5586b FL |
5014 | if (dev->hw_ol_features & NETDEV_TX_SCTP_CHECKSUM_OFFLOAD) { |
5015 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_SCTP_CKSUM; | |
5016 | } | |
29cf9c1b FL |
5017 | } |
5018 | ||
f4336f50 GR |
5019 | /* If both requested and actual hwaddr were previously |
5020 | * unset (initialized to 0), then first device init above | |
5021 | * will have set actual hwaddr to something new. | |
5022 | * This would trigger spurious MAC reconfiguration unless | |
5023 | * the requested MAC is kept in sync. | |
5024 | * | |
5025 | * This is harmless in case requested_hwaddr was | |
5026 | * configured by the user, as netdev_dpdk_set_etheraddr__() | |
5027 | * will have succeeded to get to this point. | |
5028 | */ | |
5029 | dev->requested_hwaddr = dev->hwaddr; | |
5030 | ||
eff23640 DDP |
5031 | dev->tx_q = netdev_dpdk_alloc_txq(netdev->n_txq); |
5032 | if (!dev->tx_q) { | |
5033 | err = ENOMEM; | |
5034 | } | |
050c60bf | 5035 | |
0072e931 MK |
5036 | netdev_change_seq_changed(netdev); |
5037 | ||
050c60bf | 5038 | out: |
050c60bf | 5039 | ovs_mutex_unlock(&dev->mutex); |
050c60bf DDP |
5040 | return err; |
5041 | } | |
5042 | ||
7f381c2e | 5043 | static int |
2d24d165 | 5044 | dpdk_vhost_reconfigure_helper(struct netdev_dpdk *dev) |
2d24d165 | 5045 | OVS_REQUIRES(dev->mutex) |
050c60bf | 5046 | { |
2d24d165 CL |
5047 | dev->up.n_txq = dev->requested_n_txq; |
5048 | dev->up.n_rxq = dev->requested_n_rxq; | |
96e9b168 | 5049 | int err; |
050c60bf | 5050 | |
35c91567 DM |
5051 | /* Always keep RX queue 0 enabled for implementations that won't |
5052 | * report vring states. */ | |
5053 | dev->vhost_rxq_enabled[0] = true; | |
5054 | ||
81acebda IM |
5055 | /* Enable TX queue 0 by default if it wasn't disabled. */ |
5056 | if (dev->tx_q[0].map == OVS_VHOST_QUEUE_MAP_UNKNOWN) { | |
5057 | dev->tx_q[0].map = 0; | |
5058 | } | |
5059 | ||
29cf9c1b FL |
5060 | if (userspace_tso_enabled()) { |
5061 | dev->hw_ol_features |= NETDEV_TX_TSO_OFFLOAD; | |
5062 | VLOG_DBG("%s: TSO enabled on vhost port", netdev_get_name(&dev->up)); | |
5063 | } | |
5064 | ||
81acebda IM |
5065 | netdev_dpdk_remap_txqs(dev); |
5066 | ||
d555d9bd | 5067 | err = netdev_dpdk_mempool_configure(dev); |
b6b26021 | 5068 | if (!err) { |
43307ad0 | 5069 | /* A new mempool was created or re-used. */ |
d555d9bd | 5070 | netdev_change_seq_changed(&dev->up); |
03f3f9c0 | 5071 | } else if (err != EEXIST) { |
b6b26021 | 5072 | return err; |
db8f13b0 | 5073 | } |
0a0f39df | 5074 | if (netdev_dpdk_get_vid(dev) >= 0) { |
894af647 | 5075 | if (dev->vhost_reconfigured == false) { |
5076 | dev->vhost_reconfigured = true; | |
5077 | /* Carrier status may need updating. */ | |
5078 | netdev_change_seq_changed(&dev->up); | |
5079 | } | |
81acebda | 5080 | } |
7f381c2e DDP |
5081 | |
5082 | return 0; | |
2d24d165 CL |
5083 | } |
5084 | ||
5085 | static int | |
5086 | netdev_dpdk_vhost_reconfigure(struct netdev *netdev) | |
5087 | { | |
5088 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
7f381c2e | 5089 | int err; |
2d24d165 | 5090 | |
2d24d165 | 5091 | ovs_mutex_lock(&dev->mutex); |
7f381c2e | 5092 | err = dpdk_vhost_reconfigure_helper(dev); |
2d24d165 | 5093 | ovs_mutex_unlock(&dev->mutex); |
7f381c2e DDP |
5094 | |
5095 | return err; | |
2d24d165 CL |
5096 | } |
5097 | ||
5098 | static int | |
5099 | netdev_dpdk_vhost_client_reconfigure(struct netdev *netdev) | |
5100 | { | |
5101 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
7f381c2e | 5102 | int err; |
a14d1cc8 | 5103 | uint64_t vhost_flags = 0; |
514950d3 | 5104 | uint64_t vhost_unsup_flags; |
2d24d165 | 5105 | |
2d24d165 CL |
5106 | ovs_mutex_lock(&dev->mutex); |
5107 | ||
c1ff66ac CL |
5108 | /* Configure vHost client mode if requested and if the following criteria |
5109 | * are met: | |
2d24d165 CL |
5110 | * 1. Device hasn't been registered yet. |
5111 | * 2. A path has been specified. | |
c1ff66ac | 5112 | */ |
bb9d2623 | 5113 | if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT) && dev->vhost_id) { |
a14d1cc8 MK |
5114 | /* Register client-mode device. */ |
5115 | vhost_flags |= RTE_VHOST_USER_CLIENT; | |
5116 | ||
e666e8e0 FL |
5117 | /* There is no support for multi-segments buffers. */ |
5118 | vhost_flags |= RTE_VHOST_USER_LINEARBUF_SUPPORT; | |
5119 | ||
a14d1cc8 MK |
5120 | /* Enable IOMMU support, if explicitly requested. */ |
5121 | if (dpdk_vhost_iommu_enabled()) { | |
5122 | vhost_flags |= RTE_VHOST_USER_IOMMU_SUPPORT; | |
5123 | } | |
10087cba | 5124 | |
30e834dc LB |
5125 | /* Enable POSTCOPY support, if explicitly requested. */ |
5126 | if (dpdk_vhost_postcopy_enabled()) { | |
5127 | vhost_flags |= RTE_VHOST_USER_POSTCOPY_SUPPORT; | |
5128 | } | |
5129 | ||
29cf9c1b FL |
5130 | /* Enable External Buffers if TCP Segmentation Offload is enabled. */ |
5131 | if (userspace_tso_enabled()) { | |
5132 | vhost_flags |= RTE_VHOST_USER_EXTBUF_SUPPORT; | |
5133 | } | |
5134 | ||
a14d1cc8 | 5135 | err = rte_vhost_driver_register(dev->vhost_id, vhost_flags); |
c1ff66ac | 5136 | if (err) { |
2d24d165 CL |
5137 | VLOG_ERR("vhost-user device setup failure for device %s\n", |
5138 | dev->vhost_id); | |
7f381c2e | 5139 | goto unlock; |
c1ff66ac | 5140 | } else { |
2d24d165 | 5141 | /* Configuration successful */ |
a14d1cc8 | 5142 | dev->vhost_driver_flags |= vhost_flags; |
2d24d165 CL |
5143 | VLOG_INFO("vHost User device '%s' created in 'client' mode, " |
5144 | "using client socket '%s'", | |
5145 | dev->up.name, dev->vhost_id); | |
c1ff66ac | 5146 | } |
f3e7ec25 MW |
5147 | |
5148 | err = rte_vhost_driver_callback_register(dev->vhost_id, | |
5149 | &virtio_net_device_ops); | |
5150 | if (err) { | |
5151 | VLOG_ERR("rte_vhost_driver_callback_register failed for " | |
5152 | "vhost user client port: %s\n", dev->up.name); | |
5153 | goto unlock; | |
5154 | } | |
5155 | ||
29cf9c1b FL |
5156 | if (userspace_tso_enabled()) { |
5157 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_TCP_TSO; | |
5158 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_TCP_CKSUM; | |
8c5163fe | 5159 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_UDP_CKSUM; |
35b5586b | 5160 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_SCTP_CKSUM; |
29cf9c1b | 5161 | netdev->ol_flags |= NETDEV_TX_OFFLOAD_IPV4_CKSUM; |
514950d3 FL |
5162 | vhost_unsup_flags = 1ULL << VIRTIO_NET_F_HOST_ECN |
5163 | | 1ULL << VIRTIO_NET_F_HOST_UFO; | |
29cf9c1b | 5164 | } else { |
514950d3 FL |
5165 | /* This disables checksum offloading and all the features |
5166 | * that depends on it (TSO, UFO, ECN) according to virtio | |
5167 | * specification. */ | |
5168 | vhost_unsup_flags = 1ULL << VIRTIO_NET_F_CSUM; | |
5169 | } | |
5170 | ||
5171 | err = rte_vhost_driver_disable_features(dev->vhost_id, | |
5172 | vhost_unsup_flags); | |
5173 | if (err) { | |
5174 | VLOG_ERR("rte_vhost_driver_disable_features failed for " | |
5175 | "vhost user client port: %s\n", dev->up.name); | |
5176 | goto unlock; | |
f3e7ec25 MW |
5177 | } |
5178 | ||
5179 | err = rte_vhost_driver_start(dev->vhost_id); | |
5180 | if (err) { | |
5181 | VLOG_ERR("rte_vhost_driver_start failed for vhost user " | |
5182 | "client port: %s\n", dev->up.name); | |
5183 | goto unlock; | |
5184 | } | |
c1ff66ac CL |
5185 | } |
5186 | ||
7f381c2e DDP |
5187 | err = dpdk_vhost_reconfigure_helper(dev); |
5188 | ||
5189 | unlock: | |
050c60bf | 5190 | ovs_mutex_unlock(&dev->mutex); |
050c60bf | 5191 | |
7f381c2e | 5192 | return err; |
050c60bf DDP |
5193 | } |
5194 | ||
2f7f9284 EB |
5195 | int |
5196 | netdev_dpdk_get_port_id(struct netdev *netdev) | |
5197 | { | |
5198 | struct netdev_dpdk *dev; | |
5199 | int ret = -1; | |
5200 | ||
5201 | if (!is_dpdk_class(netdev->netdev_class)) { | |
5202 | goto out; | |
5203 | } | |
5204 | ||
5205 | dev = netdev_dpdk_cast(netdev); | |
5206 | ovs_mutex_lock(&dev->mutex); | |
5207 | ret = dev->port_id; | |
5208 | ovs_mutex_unlock(&dev->mutex); | |
5209 | out: | |
5210 | return ret; | |
5211 | } | |
5212 | ||
5fc5c50f IM |
5213 | bool |
5214 | netdev_dpdk_flow_api_supported(struct netdev *netdev) | |
5215 | { | |
5216 | struct netdev_dpdk *dev; | |
5217 | bool ret = false; | |
5218 | ||
5219 | if (!is_dpdk_class(netdev->netdev_class)) { | |
5220 | goto out; | |
5221 | } | |
5222 | ||
5223 | dev = netdev_dpdk_cast(netdev); | |
5224 | ovs_mutex_lock(&dev->mutex); | |
5225 | if (dev->type == DPDK_DEV_ETH) { | |
5226 | /* TODO: Check if we able to offload some minimal flow. */ | |
5227 | ret = true; | |
5228 | } | |
5229 | ovs_mutex_unlock(&dev->mutex); | |
5230 | out: | |
5231 | return ret; | |
5232 | } | |
5233 | ||
6775bdfc RBY |
5234 | int |
5235 | netdev_dpdk_rte_flow_destroy(struct netdev *netdev, | |
5236 | struct rte_flow *rte_flow, | |
5237 | struct rte_flow_error *error) | |
5238 | { | |
5239 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
5240 | int ret; | |
5241 | ||
5242 | ovs_mutex_lock(&dev->mutex); | |
5243 | ret = rte_flow_destroy(dev->port_id, rte_flow, error); | |
5244 | ovs_mutex_unlock(&dev->mutex); | |
5245 | return ret; | |
5246 | } | |
5247 | ||
5248 | struct rte_flow * | |
5249 | netdev_dpdk_rte_flow_create(struct netdev *netdev, | |
5250 | const struct rte_flow_attr *attr, | |
5251 | const struct rte_flow_item *items, | |
5252 | const struct rte_flow_action *actions, | |
5253 | struct rte_flow_error *error) | |
5254 | { | |
5255 | struct rte_flow *flow; | |
5256 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
5257 | ||
5258 | ovs_mutex_lock(&dev->mutex); | |
5259 | flow = rte_flow_create(dev->port_id, attr, items, actions, error); | |
5260 | ovs_mutex_unlock(&dev->mutex); | |
5261 | return flow; | |
5262 | } | |
e8a2b5bf | 5263 | |
63556d85 EB |
5264 | int |
5265 | netdev_dpdk_rte_flow_query_count(struct netdev *netdev, | |
5266 | struct rte_flow *rte_flow, | |
5267 | struct rte_flow_query_count *query, | |
5268 | struct rte_flow_error *error) | |
5269 | { | |
5270 | struct rte_flow_action_count count = { .shared = 0, .id = 0 }; | |
5271 | const struct rte_flow_action actions[] = { | |
5272 | { | |
5273 | .type = RTE_FLOW_ACTION_TYPE_COUNT, | |
5274 | .conf = &count, | |
5275 | }, | |
5276 | { | |
5277 | .type = RTE_FLOW_ACTION_TYPE_END, | |
5278 | }, | |
5279 | }; | |
5280 | struct netdev_dpdk *dev; | |
5281 | int ret; | |
5282 | ||
5283 | if (!is_dpdk_class(netdev->netdev_class)) { | |
5284 | return -1; | |
5285 | } | |
5286 | ||
5287 | dev = netdev_dpdk_cast(netdev); | |
5288 | ovs_mutex_lock(&dev->mutex); | |
5289 | ret = rte_flow_query(dev->port_id, rte_flow, actions, query, error); | |
5290 | ovs_mutex_unlock(&dev->mutex); | |
5291 | return ret; | |
5292 | } | |
5293 | ||
89c09c1c BP |
5294 | #define NETDEV_DPDK_CLASS_COMMON \ |
5295 | .is_pmd = true, \ | |
5296 | .alloc = netdev_dpdk_alloc, \ | |
5297 | .dealloc = netdev_dpdk_dealloc, \ | |
5298 | .get_config = netdev_dpdk_get_config, \ | |
5299 | .get_numa_id = netdev_dpdk_get_numa_id, \ | |
5300 | .set_etheraddr = netdev_dpdk_set_etheraddr, \ | |
5301 | .get_etheraddr = netdev_dpdk_get_etheraddr, \ | |
5302 | .get_mtu = netdev_dpdk_get_mtu, \ | |
5303 | .set_mtu = netdev_dpdk_set_mtu, \ | |
5304 | .get_ifindex = netdev_dpdk_get_ifindex, \ | |
5305 | .get_carrier_resets = netdev_dpdk_get_carrier_resets, \ | |
5306 | .set_miimon_interval = netdev_dpdk_set_miimon, \ | |
5307 | .set_policing = netdev_dpdk_set_policing, \ | |
5308 | .get_qos_types = netdev_dpdk_get_qos_types, \ | |
5309 | .get_qos = netdev_dpdk_get_qos, \ | |
5310 | .set_qos = netdev_dpdk_set_qos, \ | |
23c01b19 EC |
5311 | .get_queue = netdev_dpdk_get_queue, \ |
5312 | .set_queue = netdev_dpdk_set_queue, \ | |
5313 | .delete_queue = netdev_dpdk_delete_queue, \ | |
5314 | .get_queue_stats = netdev_dpdk_get_queue_stats, \ | |
5315 | .queue_dump_start = netdev_dpdk_queue_dump_start, \ | |
5316 | .queue_dump_next = netdev_dpdk_queue_dump_next, \ | |
5317 | .queue_dump_done = netdev_dpdk_queue_dump_done, \ | |
89c09c1c BP |
5318 | .update_flags = netdev_dpdk_update_flags, \ |
5319 | .rxq_alloc = netdev_dpdk_rxq_alloc, \ | |
5320 | .rxq_construct = netdev_dpdk_rxq_construct, \ | |
5321 | .rxq_destruct = netdev_dpdk_rxq_destruct, \ | |
c0af6425 | 5322 | .rxq_dealloc = netdev_dpdk_rxq_dealloc |
89c09c1c BP |
5323 | |
5324 | #define NETDEV_DPDK_CLASS_BASE \ | |
5325 | NETDEV_DPDK_CLASS_COMMON, \ | |
5326 | .init = netdev_dpdk_class_init, \ | |
5327 | .destruct = netdev_dpdk_destruct, \ | |
5328 | .set_tx_multiq = netdev_dpdk_set_tx_multiq, \ | |
5329 | .get_carrier = netdev_dpdk_get_carrier, \ | |
5330 | .get_stats = netdev_dpdk_get_stats, \ | |
5331 | .get_custom_stats = netdev_dpdk_get_custom_stats, \ | |
5332 | .get_features = netdev_dpdk_get_features, \ | |
5333 | .get_status = netdev_dpdk_get_status, \ | |
5334 | .reconfigure = netdev_dpdk_reconfigure, \ | |
5fc5c50f | 5335 | .rxq_recv = netdev_dpdk_rxq_recv |
89c09c1c BP |
5336 | |
5337 | static const struct netdev_class dpdk_class = { | |
5338 | .type = "dpdk", | |
5339 | NETDEV_DPDK_CLASS_BASE, | |
5340 | .construct = netdev_dpdk_construct, | |
5341 | .set_config = netdev_dpdk_set_config, | |
5342 | .send = netdev_dpdk_eth_send, | |
5343 | }; | |
5344 | ||
89c09c1c BP |
5345 | static const struct netdev_class dpdk_vhost_class = { |
5346 | .type = "dpdkvhostuser", | |
5347 | NETDEV_DPDK_CLASS_COMMON, | |
5348 | .construct = netdev_dpdk_vhost_construct, | |
5349 | .destruct = netdev_dpdk_vhost_destruct, | |
5350 | .send = netdev_dpdk_vhost_send, | |
5351 | .get_carrier = netdev_dpdk_vhost_get_carrier, | |
5352 | .get_stats = netdev_dpdk_vhost_get_stats, | |
b99ab8aa | 5353 | .get_custom_stats = netdev_dpdk_get_sw_custom_stats, |
89c09c1c BP |
5354 | .get_status = netdev_dpdk_vhost_user_get_status, |
5355 | .reconfigure = netdev_dpdk_vhost_reconfigure, | |
35c91567 DM |
5356 | .rxq_recv = netdev_dpdk_vhost_rxq_recv, |
5357 | .rxq_enabled = netdev_dpdk_vhost_rxq_enabled, | |
89c09c1c BP |
5358 | }; |
5359 | ||
5360 | static const struct netdev_class dpdk_vhost_client_class = { | |
5361 | .type = "dpdkvhostuserclient", | |
5362 | NETDEV_DPDK_CLASS_COMMON, | |
5363 | .construct = netdev_dpdk_vhost_client_construct, | |
5364 | .destruct = netdev_dpdk_vhost_destruct, | |
5365 | .set_config = netdev_dpdk_vhost_client_set_config, | |
5366 | .send = netdev_dpdk_vhost_send, | |
5367 | .get_carrier = netdev_dpdk_vhost_get_carrier, | |
5368 | .get_stats = netdev_dpdk_vhost_get_stats, | |
b99ab8aa | 5369 | .get_custom_stats = netdev_dpdk_get_sw_custom_stats, |
89c09c1c BP |
5370 | .get_status = netdev_dpdk_vhost_user_get_status, |
5371 | .reconfigure = netdev_dpdk_vhost_client_reconfigure, | |
35c91567 DM |
5372 | .rxq_recv = netdev_dpdk_vhost_rxq_recv, |
5373 | .rxq_enabled = netdev_dpdk_vhost_rxq_enabled, | |
89c09c1c | 5374 | }; |
95fb793a | 5375 | |
8a9562d2 PS |
5376 | void |
5377 | netdev_dpdk_register(void) | |
5378 | { | |
bab69409 | 5379 | netdev_register_provider(&dpdk_class); |
53f50d24 | 5380 | netdev_register_provider(&dpdk_vhost_class); |
2d24d165 | 5381 | netdev_register_provider(&dpdk_vhost_client_class); |
8a9562d2 | 5382 | } |