]>
Commit | Line | Data |
---|---|---|
8a9562d2 | 1 | /* |
0d4af148 | 2 | * Copyright (c) 2014, 2015, 2016 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 | |
8a9562d2 PS |
20 | #include <string.h> |
21 | #include <signal.h> | |
22 | #include <stdlib.h> | |
8a9562d2 | 23 | #include <errno.h> |
8a9562d2 | 24 | #include <unistd.h> |
01961bbd DDP |
25 | |
26 | #include <rte_config.h> | |
27 | #include <rte_cycles.h> | |
28 | #include <rte_errno.h> | |
29 | #include <rte_eth_ring.h> | |
30 | #include <rte_ethdev.h> | |
31 | #include <rte_malloc.h> | |
32 | #include <rte_mbuf.h> | |
33 | #include <rte_meter.h> | |
01961bbd | 34 | #include <rte_virtio_net.h> |
8a9562d2 | 35 | |
7d1ced01 | 36 | #include "dirs.h" |
e14deea0 | 37 | #include "dp-packet.h" |
01961bbd | 38 | #include "dpdk.h" |
8a9562d2 | 39 | #include "dpif-netdev.h" |
e5c0f5a4 | 40 | #include "fatal-signal.h" |
8a9562d2 PS |
41 | #include "netdev-provider.h" |
42 | #include "netdev-vport.h" | |
43 | #include "odp-util.h" | |
eac84432 | 44 | #include "openvswitch/dynamic-string.h" |
25d436fb BW |
45 | #include "openvswitch/list.h" |
46 | #include "openvswitch/ofp-print.h" | |
47 | #include "openvswitch/vlog.h" | |
94143fc4 | 48 | #include "ovs-numa.h" |
8a9562d2 PS |
49 | #include "ovs-thread.h" |
50 | #include "ovs-rcu.h" | |
51 | #include "packets.h" | |
ee89ea7b | 52 | #include "openvswitch/shash.h" |
0bf765f7 | 53 | #include "smap.h" |
8a9562d2 PS |
54 | #include "sset.h" |
55 | #include "unaligned.h" | |
56 | #include "timeval.h" | |
57 | #include "unixctl.h" | |
8a9562d2 | 58 | |
05b49df6 | 59 | VLOG_DEFINE_THIS_MODULE(netdev_dpdk); |
8a9562d2 PS |
60 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); |
61 | ||
62 | #define DPDK_PORT_WATCHDOG_INTERVAL 5 | |
63 | ||
64 | #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE | |
65 | #define OVS_VPORT_DPDK "ovs_dpdk" | |
66 | ||
67 | /* | |
68 | * need to reserve tons of extra space in the mbufs so we can align the | |
69 | * DMA addresses to 4KB. | |
18f777b2 TP |
70 | * The minimum mbuf size is limited to avoid scatter behaviour and drop in |
71 | * performance for standard Ethernet MTU. | |
8a9562d2 | 72 | */ |
58be5c0e MK |
73 | #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \ |
74 | + (2 * VLAN_HEADER_LEN)) | |
4be4d22c MK |
75 | #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN) |
76 | #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN) | |
58be5c0e MK |
77 | #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \ |
78 | - ETHER_HDR_LEN - ETHER_CRC_LEN) | |
79 | #define MBUF_SIZE(mtu) (MTU_TO_MAX_FRAME_LEN(mtu) \ | |
80 | + sizeof(struct dp_packet) \ | |
81 | + RTE_PKTMBUF_HEADROOM) | |
4be4d22c | 82 | #define NETDEV_DPDK_MBUF_ALIGN 1024 |
0072e931 | 83 | #define NETDEV_DPDK_MAX_PKT_LEN 9728 |
8a9562d2 | 84 | |
da79ce2b DDP |
85 | /* Max and min number of packets in the mempool. OVS tries to allocate a |
86 | * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have | |
87 | * enough hugepages) we keep halving the number until the allocation succeeds | |
88 | * or we reach MIN_NB_MBUF */ | |
89 | ||
90 | #define MAX_NB_MBUF (4096 * 64) | |
91 | #define MIN_NB_MBUF (4096 * 4) | |
92 | #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE | |
93 | ||
94 | /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */ | |
95 | BUILD_ASSERT_DECL(MAX_NB_MBUF % ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF) == 0); | |
96 | ||
97 | /* The smallest possible NB_MBUF that we're going to try should be a multiple | |
98 | * of MP_CACHE_SZ. This is advised by DPDK documentation. */ | |
99 | BUILD_ASSERT_DECL((MAX_NB_MBUF / ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF)) | |
100 | % MP_CACHE_SZ == 0); | |
101 | ||
d6e3feb5 | 102 | /* |
103 | * DPDK XSTATS Counter names definition | |
104 | */ | |
105 | #define XSTAT_RX_64_PACKETS "rx_size_64_packets" | |
106 | #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets" | |
107 | #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets" | |
108 | #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets" | |
109 | #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets" | |
110 | #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets" | |
111 | #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets" | |
112 | ||
113 | #define XSTAT_TX_64_PACKETS "tx_size_64_packets" | |
114 | #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets" | |
115 | #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets" | |
116 | #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets" | |
117 | #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets" | |
118 | #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets" | |
119 | #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets" | |
120 | ||
121 | #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets" | |
122 | #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets" | |
123 | #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets" | |
124 | #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors" | |
125 | #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors" | |
126 | #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors" | |
127 | #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors" | |
128 | ||
8a9562d2 PS |
129 | #define SOCKET0 0 |
130 | ||
b685696b CL |
131 | /* Default size of Physical NIC RXQ */ |
132 | #define NIC_PORT_DEFAULT_RXQ_SIZE 2048 | |
133 | /* Default size of Physical NIC TXQ */ | |
134 | #define NIC_PORT_DEFAULT_TXQ_SIZE 2048 | |
135 | /* Maximum size of Physical NIC Queues */ | |
136 | #define NIC_PORT_MAX_Q_SIZE 4096 | |
79f5354c | 137 | |
585a5bea | 138 | #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */ |
f3ea2ad2 IM |
139 | #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */ |
140 | #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not | |
141 | * yet mapped to another queue. */ | |
585a5bea | 142 | |
31871ee3 | 143 | #define VHOST_ENQ_RETRY_NUM 8 |
0a0f39df | 144 | #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ) |
95e9881f | 145 | |
8a9562d2 | 146 | static const struct rte_eth_conf port_conf = { |
a28ddd11 DDP |
147 | .rxmode = { |
148 | .mq_mode = ETH_MQ_RX_RSS, | |
149 | .split_hdr_size = 0, | |
150 | .header_split = 0, /* Header Split disabled */ | |
151 | .hw_ip_checksum = 0, /* IP checksum offload disabled */ | |
152 | .hw_vlan_filter = 0, /* VLAN filtering disabled */ | |
153 | .jumbo_frame = 0, /* Jumbo Frame Support disabled */ | |
154 | .hw_strip_crc = 0, | |
155 | }, | |
156 | .rx_adv_conf = { | |
157 | .rss_conf = { | |
158 | .rss_key = NULL, | |
543342a4 | 159 | .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP, |
8a9562d2 | 160 | }, |
a28ddd11 DDP |
161 | }, |
162 | .txmode = { | |
163 | .mq_mode = ETH_MQ_TX_NONE, | |
164 | }, | |
8a9562d2 PS |
165 | }; |
166 | ||
58f7c37b DDP |
167 | enum { DPDK_RING_SIZE = 256 }; |
168 | BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE)); | |
8a9562d2 PS |
169 | enum { DRAIN_TSC = 200000ULL }; |
170 | ||
58397e6c KT |
171 | enum dpdk_dev_type { |
172 | DPDK_DEV_ETH = 0, | |
7d1ced01 | 173 | DPDK_DEV_VHOST = 1, |
58397e6c KT |
174 | }; |
175 | ||
0bf765f7 IS |
176 | /* Quality of Service */ |
177 | ||
178 | /* An instance of a QoS configuration. Always associated with a particular | |
179 | * network device. | |
180 | * | |
181 | * Each QoS implementation subclasses this with whatever additional data it | |
182 | * needs. | |
183 | */ | |
184 | struct qos_conf { | |
185 | const struct dpdk_qos_ops *ops; | |
78bd47cf | 186 | rte_spinlock_t lock; |
0bf765f7 IS |
187 | }; |
188 | ||
189 | /* A particular implementation of dpdk QoS operations. | |
190 | * | |
191 | * The functions below return 0 if successful or a positive errno value on | |
192 | * failure, except where otherwise noted. All of them must be provided, except | |
193 | * where otherwise noted. | |
194 | */ | |
195 | struct dpdk_qos_ops { | |
196 | ||
197 | /* Name of the QoS type */ | |
198 | const char *qos_name; | |
199 | ||
78bd47cf DDP |
200 | /* Called to construct a qos_conf object. The implementation should make |
201 | * the appropriate calls to configure QoS according to 'details'. | |
0bf765f7 IS |
202 | * |
203 | * The contents of 'details' should be documented as valid for 'ovs_name' | |
204 | * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml | |
205 | * (which is built as ovs-vswitchd.conf.db(8)). | |
206 | * | |
78bd47cf DDP |
207 | * This function must return 0 if and only if it sets '*conf' to an |
208 | * initialized 'struct qos_conf'. | |
0bf765f7 IS |
209 | * |
210 | * For all QoS implementations it should always be non-null. | |
211 | */ | |
78bd47cf | 212 | int (*qos_construct)(const struct smap *details, struct qos_conf **conf); |
0bf765f7 IS |
213 | |
214 | /* Destroys the data structures allocated by the implementation as part of | |
78bd47cf | 215 | * 'qos_conf'. |
0bf765f7 IS |
216 | * |
217 | * For all QoS implementations it should always be non-null. | |
218 | */ | |
78bd47cf | 219 | void (*qos_destruct)(struct qos_conf *conf); |
0bf765f7 | 220 | |
78bd47cf | 221 | /* Retrieves details of 'conf' configuration into 'details'. |
0bf765f7 IS |
222 | * |
223 | * The contents of 'details' should be documented as valid for 'ovs_name' | |
224 | * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml | |
225 | * (which is built as ovs-vswitchd.conf.db(8)). | |
226 | */ | |
78bd47cf | 227 | int (*qos_get)(const struct qos_conf *conf, struct smap *details); |
0bf765f7 | 228 | |
78bd47cf | 229 | /* Returns true if 'conf' is already configured according to 'details'. |
0bf765f7 IS |
230 | * |
231 | * The contents of 'details' should be documented as valid for 'ovs_name' | |
232 | * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml | |
233 | * (which is built as ovs-vswitchd.conf.db(8)). | |
234 | * | |
78bd47cf | 235 | * For all QoS implementations it should always be non-null. |
0bf765f7 | 236 | */ |
78bd47cf DDP |
237 | bool (*qos_is_equal)(const struct qos_conf *conf, |
238 | const struct smap *details); | |
0bf765f7 IS |
239 | |
240 | /* Modify an array of rte_mbufs. The modification is specific to | |
241 | * each qos implementation. | |
242 | * | |
243 | * The function should take and array of mbufs and an int representing | |
244 | * the current number of mbufs present in the array. | |
245 | * | |
246 | * After the function has performed a qos modification to the array of | |
247 | * mbufs it returns an int representing the number of mbufs now present in | |
248 | * the array. This value is can then be passed to the port send function | |
249 | * along with the modified array for transmission. | |
250 | * | |
251 | * For all QoS implementations it should always be non-null. | |
252 | */ | |
78bd47cf DDP |
253 | int (*qos_run)(struct qos_conf *qos_conf, struct rte_mbuf **pkts, |
254 | int pkt_cnt); | |
0bf765f7 IS |
255 | }; |
256 | ||
257 | /* dpdk_qos_ops for each type of user space QoS implementation */ | |
258 | static const struct dpdk_qos_ops egress_policer_ops; | |
259 | ||
260 | /* | |
261 | * Array of dpdk_qos_ops, contains pointer to all supported QoS | |
262 | * operations. | |
263 | */ | |
264 | static const struct dpdk_qos_ops *const qos_confs[] = { | |
265 | &egress_policer_ops, | |
266 | NULL | |
267 | }; | |
268 | ||
c2adb102 IM |
269 | static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER; |
270 | ||
8a9562d2 | 271 | /* Contains all 'struct dpdk_dev's. */ |
ca6ba700 | 272 | static struct ovs_list dpdk_list OVS_GUARDED_BY(dpdk_mutex) |
55951e15 | 273 | = OVS_LIST_INITIALIZER(&dpdk_list); |
8a9562d2 | 274 | |
c2adb102 IM |
275 | static struct ovs_mutex dpdk_mp_mutex OVS_ACQ_AFTER(dpdk_mutex) |
276 | = OVS_MUTEX_INITIALIZER; | |
277 | ||
278 | static struct ovs_list dpdk_mp_list OVS_GUARDED_BY(dpdk_mp_mutex) | |
55951e15 | 279 | = OVS_LIST_INITIALIZER(&dpdk_mp_list); |
8a9562d2 | 280 | |
8a9562d2 PS |
281 | struct dpdk_mp { |
282 | struct rte_mempool *mp; | |
283 | int mtu; | |
284 | int socket_id; | |
285 | int refcount; | |
c2adb102 | 286 | struct ovs_list list_node OVS_GUARDED_BY(dpdk_mp_mutex); |
8a9562d2 PS |
287 | }; |
288 | ||
5a034064 AW |
289 | /* There should be one 'struct dpdk_tx_queue' created for |
290 | * each cpu core. */ | |
8a9562d2 | 291 | struct dpdk_tx_queue { |
a0cb2d66 DDP |
292 | rte_spinlock_t tx_lock; /* Protects the members and the NIC queue |
293 | * from concurrent access. It is used only | |
294 | * if the queue is shared among different | |
324c8374 | 295 | * pmd threads (see 'concurrent_txq'). */ |
585a5bea IM |
296 | int map; /* Mapping of configured vhost-user queues |
297 | * to enabled by guest. */ | |
8a9562d2 PS |
298 | }; |
299 | ||
95fb793a | 300 | /* dpdk has no way to remove dpdk ring ethernet devices |
301 | so we have to keep them around once they've been created | |
302 | */ | |
303 | ||
ca6ba700 | 304 | static struct ovs_list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex) |
55951e15 | 305 | = OVS_LIST_INITIALIZER(&dpdk_ring_list); |
95fb793a | 306 | |
307 | struct dpdk_ring { | |
308 | /* For the client rings */ | |
309 | struct rte_ring *cring_tx; | |
310 | struct rte_ring *cring_rx; | |
b83a2df1 | 311 | unsigned int user_port_id; /* User given port no, parsed from port name */ |
95fb793a | 312 | int eth_port_id; /* ethernet device port id */ |
ca6ba700 | 313 | struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex); |
95fb793a | 314 | }; |
315 | ||
9509913a IS |
316 | struct ingress_policer { |
317 | struct rte_meter_srtcm_params app_srtcm_params; | |
318 | struct rte_meter_srtcm in_policer; | |
319 | rte_spinlock_t policer_lock; | |
320 | }; | |
321 | ||
1a2bb118 SC |
322 | enum dpdk_hw_ol_features { |
323 | NETDEV_RX_CHECKSUM_OFFLOAD = 1 << 0, | |
324 | }; | |
325 | ||
8a9562d2 PS |
326 | struct netdev_dpdk { |
327 | struct netdev up; | |
328 | int port_id; | |
329 | int max_packet_len; | |
58397e6c | 330 | enum dpdk_dev_type type; |
8a9562d2 | 331 | |
5a034064 | 332 | struct dpdk_tx_queue *tx_q; |
8a9562d2 PS |
333 | |
334 | struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex); | |
335 | ||
336 | struct dpdk_mp *dpdk_mp; | |
337 | int mtu; | |
338 | int socket_id; | |
339 | int buf_size; | |
8a9562d2 | 340 | struct netdev_stats stats; |
45d947c4 DDP |
341 | /* Protects stats */ |
342 | rte_spinlock_t stats_lock; | |
8a9562d2 | 343 | |
74ff3298 | 344 | struct eth_addr hwaddr; |
8a9562d2 PS |
345 | enum netdev_flags flags; |
346 | ||
347 | struct rte_eth_link link; | |
348 | int link_reset_cnt; | |
349 | ||
0a0f39df CL |
350 | /* virtio identifier for vhost devices */ |
351 | ovsrcu_index vid; | |
352 | ||
353 | /* True if vHost device is 'up' and has been reconfigured at least once */ | |
354 | bool vhost_reconfigured; | |
58397e6c | 355 | |
2d24d165 CL |
356 | /* Identifier used to distinguish vhost devices from each other. */ |
357 | char vhost_id[PATH_MAX]; | |
7d1ced01 | 358 | |
8a9562d2 | 359 | /* In dpdk_list. */ |
ca6ba700 | 360 | struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex); |
0bf765f7 IS |
361 | |
362 | /* QoS configuration and lock for the device */ | |
78bd47cf | 363 | OVSRCU_TYPE(struct qos_conf *) qos_conf; |
0bf765f7 | 364 | |
050c60bf DDP |
365 | /* The following properties cannot be changed when a device is running, |
366 | * so we remember the request and update them next time | |
367 | * netdev_dpdk*_reconfigure() is called */ | |
0072e931 | 368 | int requested_mtu; |
050c60bf DDP |
369 | int requested_n_txq; |
370 | int requested_n_rxq; | |
b685696b CL |
371 | int requested_rxq_size; |
372 | int requested_txq_size; | |
373 | ||
374 | /* Number of rx/tx descriptors for physical devices */ | |
375 | int rxq_size; | |
376 | int txq_size; | |
9509913a | 377 | |
db8f13b0 CL |
378 | /* Socket ID detected when vHost device is brought up */ |
379 | int requested_socket_id; | |
380 | ||
c1ff66ac CL |
381 | /* Denotes whether vHost port is client/server mode */ |
382 | uint64_t vhost_driver_flags; | |
383 | ||
9509913a IS |
384 | /* Ingress Policer */ |
385 | OVSRCU_TYPE(struct ingress_policer *) ingress_policer; | |
386 | uint32_t policer_rate; | |
387 | uint32_t policer_burst; | |
9fd39370 SC |
388 | |
389 | /* DPDK-ETH Flow control */ | |
390 | struct rte_eth_fc_conf fc_conf; | |
1a2bb118 SC |
391 | |
392 | /* DPDK-ETH hardware offload features, | |
393 | * from the enum set 'dpdk_hw_ol_features' */ | |
394 | uint32_t hw_ol_features; | |
8a9562d2 PS |
395 | }; |
396 | ||
397 | struct netdev_rxq_dpdk { | |
398 | struct netdev_rxq up; | |
399 | int port_id; | |
400 | }; | |
401 | ||
b3722dca | 402 | static int netdev_dpdk_class_init(void); |
403 | static int netdev_dpdk_vhost_class_init(void); | |
8a9562d2 | 404 | |
0a0f39df | 405 | int netdev_dpdk_get_vid(const struct netdev_dpdk *dev); |
58397e6c | 406 | |
9509913a IS |
407 | struct ingress_policer * |
408 | netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev); | |
409 | ||
8a9562d2 PS |
410 | static bool |
411 | is_dpdk_class(const struct netdev_class *class) | |
412 | { | |
b3722dca | 413 | return class->init == netdev_dpdk_class_init |
414 | || class->init == netdev_dpdk_vhost_class_init; | |
8a9562d2 PS |
415 | } |
416 | ||
4be4d22c MK |
417 | /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically |
418 | * aligned at 1k or less. If a declared mbuf size is not a multiple of this | |
419 | * value, insufficient buffers are allocated to accomodate the packet in its | |
420 | * entirety. Furthermore, certain drivers need to ensure that there is also | |
421 | * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ | |
422 | * frames). If the RX buffer is too small, then the driver enables scatter RX | |
58be5c0e MK |
423 | * behaviour, which reduces performance. To prevent this, use a buffer size |
424 | * that is closest to 'mtu', but which satisfies the aforementioned criteria. | |
4be4d22c MK |
425 | */ |
426 | static uint32_t | |
427 | dpdk_buf_size(int mtu) | |
428 | { | |
429 | return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) + RTE_PKTMBUF_HEADROOM), | |
430 | NETDEV_DPDK_MBUF_ALIGN); | |
431 | } | |
432 | ||
eff23640 DDP |
433 | /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed. |
434 | * | |
435 | * Unlike xmalloc(), this function can return NULL on failure. */ | |
8a9562d2 PS |
436 | static void * |
437 | dpdk_rte_mzalloc(size_t sz) | |
438 | { | |
eff23640 | 439 | return rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE); |
8a9562d2 PS |
440 | } |
441 | ||
442 | void | |
e14deea0 | 443 | free_dpdk_buf(struct dp_packet *p) |
8a9562d2 | 444 | { |
db73f716 | 445 | struct rte_mbuf *pkt = (struct rte_mbuf *) p; |
8a9562d2 | 446 | |
b00b4a81 | 447 | rte_pktmbuf_free(pkt); |
8a9562d2 PS |
448 | } |
449 | ||
b3cd9f9d PS |
450 | static void |
451 | ovs_rte_pktmbuf_init(struct rte_mempool *mp, | |
452 | void *opaque_arg OVS_UNUSED, | |
2391135c | 453 | void *_p, |
b3cd9f9d PS |
454 | unsigned i OVS_UNUSED) |
455 | { | |
2391135c | 456 | struct rte_mbuf *pkt = _p; |
b3cd9f9d | 457 | |
2391135c | 458 | rte_pktmbuf_init(mp, opaque_arg, _p, i); |
b3cd9f9d | 459 | |
2391135c | 460 | dp_packet_init_dpdk((struct dp_packet *) pkt, pkt->buf_len); |
b3cd9f9d PS |
461 | } |
462 | ||
8a9562d2 | 463 | static struct dpdk_mp * |
2ae3d542 | 464 | dpdk_mp_create(int socket_id, int mtu) |
8a9562d2 | 465 | { |
2ae3d542 | 466 | struct rte_pktmbuf_pool_private mbp_priv; |
2ae3d542 | 467 | struct dpdk_mp *dmp; |
da79ce2b | 468 | unsigned mp_size; |
0c6f39e5 | 469 | char *mp_name; |
8a9562d2 PS |
470 | |
471 | dmp = dpdk_rte_mzalloc(sizeof *dmp); | |
eff23640 DDP |
472 | if (!dmp) { |
473 | return NULL; | |
474 | } | |
8a9562d2 PS |
475 | dmp->socket_id = socket_id; |
476 | dmp->mtu = mtu; | |
477 | dmp->refcount = 1; | |
4be4d22c | 478 | mbp_priv.mbuf_data_room_size = MBUF_SIZE(mtu) - sizeof(struct dp_packet); |
58be5c0e MK |
479 | mbp_priv.mbuf_priv_size = sizeof(struct dp_packet) |
480 | - sizeof(struct rte_mbuf); | |
0072e931 | 481 | /* XXX: this is a really rough method of provisioning memory. |
2ae3d542 | 482 | * It's impossible to determine what the exact memory requirements are |
58be5c0e MK |
483 | * when the number of ports and rxqs that utilize a particular mempool can |
484 | * change dynamically at runtime. For now, use this rough heurisitic. | |
0072e931 MK |
485 | */ |
486 | if (mtu >= ETHER_MTU) { | |
487 | mp_size = MAX_NB_MBUF; | |
488 | } else { | |
489 | mp_size = MIN_NB_MBUF; | |
490 | } | |
8a9562d2 | 491 | |
da79ce2b | 492 | do { |
0c6f39e5 DDP |
493 | mp_name = xasprintf("ovs_mp_%d_%d_%u", dmp->mtu, dmp->socket_id, |
494 | mp_size); | |
95fb793a | 495 | |
da79ce2b DDP |
496 | dmp->mp = rte_mempool_create(mp_name, mp_size, MBUF_SIZE(mtu), |
497 | MP_CACHE_SZ, | |
498 | sizeof(struct rte_pktmbuf_pool_private), | |
4be4d22c | 499 | rte_pktmbuf_pool_init, &mbp_priv, |
da79ce2b DDP |
500 | ovs_rte_pktmbuf_init, NULL, |
501 | socket_id, 0); | |
0c6f39e5 DDP |
502 | if (dmp->mp) { |
503 | VLOG_DBG("Allocated \"%s\" mempool with %u mbufs", | |
504 | mp_name, mp_size); | |
505 | } | |
506 | free(mp_name); | |
507 | if (dmp->mp) { | |
508 | return dmp; | |
509 | } | |
510 | } while (rte_errno == ENOMEM && (mp_size /= 2) >= MIN_NB_MBUF); | |
2ae3d542 | 511 | |
2ae3d542 DDP |
512 | rte_free(dmp); |
513 | return NULL; | |
514 | } | |
515 | ||
516 | static struct dpdk_mp * | |
517 | dpdk_mp_get(int socket_id, int mtu) | |
518 | { | |
519 | struct dpdk_mp *dmp; | |
520 | ||
521 | ovs_mutex_lock(&dpdk_mp_mutex); | |
522 | LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) { | |
523 | if (dmp->socket_id == socket_id && dmp->mtu == mtu) { | |
524 | dmp->refcount++; | |
525 | goto out; | |
526 | } | |
527 | } | |
528 | ||
529 | dmp = dpdk_mp_create(socket_id, mtu); | |
417e7e66 | 530 | ovs_list_push_back(&dpdk_mp_list, &dmp->list_node); |
5f88de0d | 531 | |
c2adb102 IM |
532 | out: |
533 | ovs_mutex_unlock(&dpdk_mp_mutex); | |
534 | ||
c2adb102 | 535 | return dmp; |
8a9562d2 PS |
536 | } |
537 | ||
538 | static void | |
c2adb102 | 539 | dpdk_mp_put(struct dpdk_mp *dmp) |
8a9562d2 | 540 | { |
8a9562d2 PS |
541 | if (!dmp) { |
542 | return; | |
543 | } | |
544 | ||
c2adb102 | 545 | ovs_mutex_lock(&dpdk_mp_mutex); |
8d38823b | 546 | ovs_assert(dmp->refcount); |
8a9562d2 | 547 | |
8d38823b MK |
548 | if (!--dmp->refcount) { |
549 | ovs_list_remove(&dmp->list_node); | |
550 | rte_mempool_free(dmp->mp); | |
5f88de0d | 551 | rte_free(dmp); |
8a9562d2 | 552 | } |
c2adb102 | 553 | ovs_mutex_unlock(&dpdk_mp_mutex); |
8a9562d2 PS |
554 | } |
555 | ||
0072e931 MK |
556 | /* Tries to allocate new mempool on requested_socket_id with |
557 | * mbuf size corresponding to requested_mtu. | |
558 | * On success new configuration will be applied. | |
559 | * On error, device will be left unchanged. */ | |
560 | static int | |
561 | netdev_dpdk_mempool_configure(struct netdev_dpdk *dev) | |
0072e931 MK |
562 | OVS_REQUIRES(dev->mutex) |
563 | { | |
564 | uint32_t buf_size = dpdk_buf_size(dev->requested_mtu); | |
565 | struct dpdk_mp *mp; | |
566 | ||
567 | mp = dpdk_mp_get(dev->requested_socket_id, FRAME_LEN_TO_MTU(buf_size)); | |
568 | if (!mp) { | |
569 | VLOG_ERR("Insufficient memory to create memory pool for netdev " | |
570 | "%s, with MTU %d on socket %d\n", | |
571 | dev->up.name, dev->requested_mtu, dev->requested_socket_id); | |
572 | return ENOMEM; | |
573 | } else { | |
574 | dpdk_mp_put(dev->dpdk_mp); | |
575 | dev->dpdk_mp = mp; | |
576 | dev->mtu = dev->requested_mtu; | |
577 | dev->socket_id = dev->requested_socket_id; | |
578 | dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu); | |
579 | } | |
580 | ||
581 | return 0; | |
582 | } | |
583 | ||
8a9562d2 PS |
584 | static void |
585 | check_link_status(struct netdev_dpdk *dev) | |
586 | { | |
587 | struct rte_eth_link link; | |
588 | ||
589 | rte_eth_link_get_nowait(dev->port_id, &link); | |
590 | ||
591 | if (dev->link.link_status != link.link_status) { | |
3e912ffc | 592 | netdev_change_seq_changed(&dev->up); |
8a9562d2 PS |
593 | |
594 | dev->link_reset_cnt++; | |
595 | dev->link = link; | |
596 | if (dev->link.link_status) { | |
597 | VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s", | |
58be5c0e | 598 | dev->port_id, (unsigned) dev->link.link_speed, |
8a9562d2 PS |
599 | (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ? |
600 | ("full-duplex") : ("half-duplex")); | |
601 | } else { | |
602 | VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id); | |
603 | } | |
604 | } | |
605 | } | |
606 | ||
607 | static void * | |
608 | dpdk_watchdog(void *dummy OVS_UNUSED) | |
609 | { | |
610 | struct netdev_dpdk *dev; | |
611 | ||
612 | pthread_detach(pthread_self()); | |
613 | ||
614 | for (;;) { | |
615 | ovs_mutex_lock(&dpdk_mutex); | |
616 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { | |
617 | ovs_mutex_lock(&dev->mutex); | |
1f5b157e IM |
618 | if (dev->type == DPDK_DEV_ETH) { |
619 | check_link_status(dev); | |
620 | } | |
8a9562d2 PS |
621 | ovs_mutex_unlock(&dev->mutex); |
622 | } | |
623 | ovs_mutex_unlock(&dpdk_mutex); | |
624 | xsleep(DPDK_PORT_WATCHDOG_INTERVAL); | |
625 | } | |
626 | ||
627 | return NULL; | |
628 | } | |
629 | ||
b98d7669 DDP |
630 | static int |
631 | dpdk_eth_dev_queue_setup(struct netdev_dpdk *dev, int n_rxq, int n_txq) | |
632 | { | |
633 | int diag = 0; | |
634 | int i; | |
0072e931 | 635 | struct rte_eth_conf conf = port_conf; |
b98d7669 | 636 | |
0072e931 MK |
637 | if (dev->mtu > ETHER_MTU) { |
638 | conf.rxmode.jumbo_frame = 1; | |
639 | conf.rxmode.max_rx_pkt_len = dev->max_packet_len; | |
640 | } else { | |
641 | conf.rxmode.jumbo_frame = 0; | |
642 | conf.rxmode.max_rx_pkt_len = 0; | |
643 | } | |
1a2bb118 SC |
644 | conf.rxmode.hw_ip_checksum = (dev->hw_ol_features & |
645 | NETDEV_RX_CHECKSUM_OFFLOAD) != 0; | |
b98d7669 DDP |
646 | /* A device may report more queues than it makes available (this has |
647 | * been observed for Intel xl710, which reserves some of them for | |
648 | * SRIOV): rte_eth_*_queue_setup will fail if a queue is not | |
649 | * available. When this happens we can retry the configuration | |
650 | * and request less queues */ | |
651 | while (n_rxq && n_txq) { | |
652 | if (diag) { | |
653 | VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq, n_txq); | |
654 | } | |
655 | ||
0072e931 | 656 | diag = rte_eth_dev_configure(dev->port_id, n_rxq, n_txq, &conf); |
b98d7669 | 657 | if (diag) { |
0072e931 MK |
658 | VLOG_WARN("Interface %s eth_dev setup error %s\n", |
659 | dev->up.name, rte_strerror(-diag)); | |
b98d7669 DDP |
660 | break; |
661 | } | |
662 | ||
663 | for (i = 0; i < n_txq; i++) { | |
b685696b | 664 | diag = rte_eth_tx_queue_setup(dev->port_id, i, dev->txq_size, |
b98d7669 DDP |
665 | dev->socket_id, NULL); |
666 | if (diag) { | |
667 | VLOG_INFO("Interface %s txq(%d) setup error: %s", | |
668 | dev->up.name, i, rte_strerror(-diag)); | |
669 | break; | |
670 | } | |
671 | } | |
672 | ||
673 | if (i != n_txq) { | |
674 | /* Retry with less tx queues */ | |
675 | n_txq = i; | |
676 | continue; | |
677 | } | |
678 | ||
679 | for (i = 0; i < n_rxq; i++) { | |
b685696b | 680 | diag = rte_eth_rx_queue_setup(dev->port_id, i, dev->rxq_size, |
b98d7669 DDP |
681 | dev->socket_id, NULL, |
682 | dev->dpdk_mp->mp); | |
683 | if (diag) { | |
684 | VLOG_INFO("Interface %s rxq(%d) setup error: %s", | |
685 | dev->up.name, i, rte_strerror(-diag)); | |
686 | break; | |
687 | } | |
688 | } | |
689 | ||
690 | if (i != n_rxq) { | |
691 | /* Retry with less rx queues */ | |
692 | n_rxq = i; | |
693 | continue; | |
694 | } | |
695 | ||
696 | dev->up.n_rxq = n_rxq; | |
81acebda | 697 | dev->up.n_txq = n_txq; |
b98d7669 DDP |
698 | |
699 | return 0; | |
700 | } | |
701 | ||
702 | return diag; | |
703 | } | |
704 | ||
1a2bb118 SC |
705 | static void |
706 | dpdk_eth_checksum_offload_configure(struct netdev_dpdk *dev) | |
707 | OVS_REQUIRES(dev->mutex) | |
708 | { | |
709 | struct rte_eth_dev_info info; | |
710 | bool rx_csum_ol_flag = false; | |
711 | uint32_t rx_chksm_offload_capa = DEV_RX_OFFLOAD_UDP_CKSUM | | |
712 | DEV_RX_OFFLOAD_TCP_CKSUM | | |
713 | DEV_RX_OFFLOAD_IPV4_CKSUM; | |
714 | rte_eth_dev_info_get(dev->port_id, &info); | |
715 | rx_csum_ol_flag = (dev->hw_ol_features & NETDEV_RX_CHECKSUM_OFFLOAD) != 0; | |
716 | ||
717 | if (rx_csum_ol_flag && | |
718 | (info.rx_offload_capa & rx_chksm_offload_capa) != | |
719 | rx_chksm_offload_capa) { | |
720 | VLOG_WARN_ONCE("Rx checksum offload is not supported on device %d", | |
721 | dev->port_id); | |
722 | dev->hw_ol_features &= ~NETDEV_RX_CHECKSUM_OFFLOAD; | |
723 | return; | |
724 | } | |
725 | netdev_request_reconfigure(&dev->up); | |
726 | } | |
727 | ||
9fd39370 SC |
728 | static void |
729 | dpdk_eth_flow_ctrl_setup(struct netdev_dpdk *dev) OVS_REQUIRES(dev->mutex) | |
730 | { | |
731 | if (rte_eth_dev_flow_ctrl_set(dev->port_id, &dev->fc_conf)) { | |
732 | VLOG_WARN("Failed to enable flow control on device %d", dev->port_id); | |
733 | } | |
734 | } | |
b98d7669 | 735 | |
8a9562d2 | 736 | static int |
c2adb102 IM |
737 | dpdk_eth_dev_init(struct netdev_dpdk *dev) |
738 | OVS_REQUIRES(dev->mutex) | |
8a9562d2 PS |
739 | { |
740 | struct rte_pktmbuf_pool_private *mbp_priv; | |
a0cb2d66 | 741 | struct rte_eth_dev_info info; |
8a9562d2 PS |
742 | struct ether_addr eth_addr; |
743 | int diag; | |
b98d7669 | 744 | int n_rxq, n_txq; |
8a9562d2 | 745 | |
7cd1261d | 746 | if (!rte_eth_dev_is_valid_port(dev->port_id)) { |
95fb793a | 747 | return ENODEV; |
8a9562d2 PS |
748 | } |
749 | ||
a0cb2d66 | 750 | rte_eth_dev_info_get(dev->port_id, &info); |
a0cb2d66 | 751 | |
b98d7669 DDP |
752 | n_rxq = MIN(info.max_rx_queues, dev->up.n_rxq); |
753 | n_txq = MIN(info.max_tx_queues, dev->up.n_txq); | |
754 | ||
755 | diag = dpdk_eth_dev_queue_setup(dev, n_rxq, n_txq); | |
8a9562d2 | 756 | if (diag) { |
b98d7669 DDP |
757 | VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s", |
758 | dev->up.name, n_rxq, n_txq, rte_strerror(-diag)); | |
95fb793a | 759 | return -diag; |
8a9562d2 PS |
760 | } |
761 | ||
8a9562d2 PS |
762 | diag = rte_eth_dev_start(dev->port_id); |
763 | if (diag) { | |
b98d7669 DDP |
764 | VLOG_ERR("Interface %s start error: %s", dev->up.name, |
765 | rte_strerror(-diag)); | |
95fb793a | 766 | return -diag; |
8a9562d2 PS |
767 | } |
768 | ||
769 | rte_eth_promiscuous_enable(dev->port_id); | |
770 | rte_eth_allmulticast_enable(dev->port_id); | |
771 | ||
772 | memset(ð_addr, 0x0, sizeof(eth_addr)); | |
773 | rte_eth_macaddr_get(dev->port_id, ð_addr); | |
774 | VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"", | |
ca92d173 | 775 | dev->port_id, ETH_ADDR_BYTES_ARGS(eth_addr.addr_bytes)); |
8a9562d2 | 776 | |
ca92d173 | 777 | memcpy(dev->hwaddr.ea, eth_addr.addr_bytes, ETH_ADDR_LEN); |
8a9562d2 PS |
778 | rte_eth_link_get_nowait(dev->port_id, &dev->link); |
779 | ||
780 | mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp); | |
781 | dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM; | |
782 | ||
783 | dev->flags = NETDEV_UP | NETDEV_PROMISC; | |
9fd39370 SC |
784 | |
785 | /* Get the Flow control configuration for DPDK-ETH */ | |
786 | diag = rte_eth_dev_flow_ctrl_get(dev->port_id, &dev->fc_conf); | |
787 | if (diag) { | |
788 | VLOG_DBG("cannot get flow control parameters on port=%d, err=%d", | |
789 | dev->port_id, diag); | |
790 | } | |
791 | ||
8a9562d2 PS |
792 | return 0; |
793 | } | |
794 | ||
795 | static struct netdev_dpdk * | |
796 | netdev_dpdk_cast(const struct netdev *netdev) | |
797 | { | |
798 | return CONTAINER_OF(netdev, struct netdev_dpdk, up); | |
799 | } | |
800 | ||
801 | static struct netdev * | |
802 | netdev_dpdk_alloc(void) | |
803 | { | |
bab69409 AC |
804 | struct netdev_dpdk *dev; |
805 | ||
65e19e70 DDP |
806 | dev = dpdk_rte_mzalloc(sizeof *dev); |
807 | if (dev) { | |
808 | return &dev->up; | |
bab69409 | 809 | } |
65e19e70 | 810 | |
bab69409 | 811 | return NULL; |
8a9562d2 PS |
812 | } |
813 | ||
eff23640 DDP |
814 | static struct dpdk_tx_queue * |
815 | netdev_dpdk_alloc_txq(unsigned int n_txqs) | |
5a034064 | 816 | { |
eff23640 | 817 | struct dpdk_tx_queue *txqs; |
bd5131ba | 818 | unsigned i; |
5a034064 | 819 | |
eff23640 DDP |
820 | txqs = dpdk_rte_mzalloc(n_txqs * sizeof *txqs); |
821 | if (txqs) { | |
822 | for (i = 0; i < n_txqs; i++) { | |
823 | /* Initialize map for vhost devices. */ | |
824 | txqs[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN; | |
825 | rte_spinlock_init(&txqs[i].tx_lock); | |
826 | } | |
5a034064 | 827 | } |
eff23640 DDP |
828 | |
829 | return txqs; | |
5a034064 AW |
830 | } |
831 | ||
8a9562d2 | 832 | static int |
d46285a2 | 833 | netdev_dpdk_init(struct netdev *netdev, unsigned int port_no, |
58397e6c | 834 | enum dpdk_dev_type type) |
5a034064 | 835 | OVS_REQUIRES(dpdk_mutex) |
8a9562d2 | 836 | { |
d46285a2 | 837 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
1b7a04e0 | 838 | int sid; |
95fb793a | 839 | int err = 0; |
8a9562d2 | 840 | |
d46285a2 DDP |
841 | ovs_mutex_init(&dev->mutex); |
842 | ovs_mutex_lock(&dev->mutex); | |
8a9562d2 | 843 | |
d46285a2 | 844 | rte_spinlock_init(&dev->stats_lock); |
45d947c4 | 845 | |
1b7a04e0 AW |
846 | /* If the 'sid' is negative, it means that the kernel fails |
847 | * to obtain the pci numa info. In that situation, always | |
848 | * use 'SOCKET0'. */ | |
58397e6c KT |
849 | if (type == DPDK_DEV_ETH) { |
850 | sid = rte_eth_dev_socket_id(port_no); | |
851 | } else { | |
852 | sid = rte_lcore_to_socket_id(rte_get_master_lcore()); | |
853 | } | |
854 | ||
d46285a2 | 855 | dev->socket_id = sid < 0 ? SOCKET0 : sid; |
db8f13b0 | 856 | dev->requested_socket_id = dev->socket_id; |
d46285a2 DDP |
857 | dev->port_id = port_no; |
858 | dev->type = type; | |
859 | dev->flags = 0; | |
0072e931 | 860 | dev->requested_mtu = dev->mtu = ETHER_MTU; |
d46285a2 | 861 | dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu); |
0a0f39df CL |
862 | ovsrcu_index_init(&dev->vid, -1); |
863 | dev->vhost_reconfigured = false; | |
8a9562d2 | 864 | |
0072e931 MK |
865 | err = netdev_dpdk_mempool_configure(dev); |
866 | if (err) { | |
95fb793a | 867 | goto unlock; |
8a9562d2 PS |
868 | } |
869 | ||
78bd47cf | 870 | ovsrcu_init(&dev->qos_conf, NULL); |
0bf765f7 | 871 | |
9509913a IS |
872 | ovsrcu_init(&dev->ingress_policer, NULL); |
873 | dev->policer_rate = 0; | |
874 | dev->policer_burst = 0; | |
875 | ||
d46285a2 | 876 | netdev->n_rxq = NR_QUEUE; |
81acebda IM |
877 | netdev->n_txq = NR_QUEUE; |
878 | dev->requested_n_rxq = netdev->n_rxq; | |
879 | dev->requested_n_txq = netdev->n_txq; | |
b685696b CL |
880 | dev->rxq_size = NIC_PORT_DEFAULT_RXQ_SIZE; |
881 | dev->txq_size = NIC_PORT_DEFAULT_TXQ_SIZE; | |
882 | dev->requested_rxq_size = dev->rxq_size; | |
883 | dev->requested_txq_size = dev->txq_size; | |
58397e6c | 884 | |
9fd39370 SC |
885 | /* Initialize the flow control to NULL */ |
886 | memset(&dev->fc_conf, 0, sizeof dev->fc_conf); | |
1a2bb118 SC |
887 | |
888 | /* Initilize the hardware offload flags to 0 */ | |
889 | dev->hw_ol_features = 0; | |
58397e6c | 890 | if (type == DPDK_DEV_ETH) { |
d46285a2 | 891 | err = dpdk_eth_dev_init(dev); |
1b99bb05 MG |
892 | if (err) { |
893 | goto unlock; | |
894 | } | |
eff23640 | 895 | dev->tx_q = netdev_dpdk_alloc_txq(netdev->n_txq); |
585a5bea | 896 | } else { |
eff23640 | 897 | dev->tx_q = netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM); |
e543851d ZB |
898 | /* Enable DPDK_DEV_VHOST device and set promiscuous mode flag. */ |
899 | dev->flags = NETDEV_UP | NETDEV_PROMISC; | |
8a9562d2 | 900 | } |
8a9562d2 | 901 | |
eff23640 DDP |
902 | if (!dev->tx_q) { |
903 | err = ENOMEM; | |
904 | goto unlock; | |
905 | } | |
906 | ||
d46285a2 | 907 | ovs_list_push_back(&dpdk_list, &dev->list_node); |
8a9562d2 | 908 | |
95fb793a | 909 | unlock: |
d46285a2 | 910 | ovs_mutex_unlock(&dev->mutex); |
95fb793a | 911 | return err; |
912 | } | |
913 | ||
b83a2df1 MV |
914 | /* dev_name must be the prefix followed by a positive decimal number. |
915 | * (no leading + or - signs are allowed) */ | |
95fb793a | 916 | static int |
917 | dpdk_dev_parse_name(const char dev_name[], const char prefix[], | |
918 | unsigned int *port_no) | |
919 | { | |
920 | const char *cport; | |
921 | ||
922 | if (strncmp(dev_name, prefix, strlen(prefix))) { | |
923 | return ENODEV; | |
924 | } | |
925 | ||
926 | cport = dev_name + strlen(prefix); | |
b83a2df1 MV |
927 | |
928 | if (str_to_uint(cport, 10, port_no)) { | |
929 | return 0; | |
930 | } else { | |
931 | return ENODEV; | |
932 | } | |
95fb793a | 933 | } |
934 | ||
7d1ced01 | 935 | static int |
53f50d24 | 936 | netdev_dpdk_vhost_construct(struct netdev *netdev) |
7d1ced01 | 937 | { |
d46285a2 DDP |
938 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
939 | const char *name = netdev->name; | |
7d1ced01 | 940 | int err; |
a0cb2d66 | 941 | |
1af27e8a DDP |
942 | /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in |
943 | * the file system. '/' or '\' would traverse directories, so they're not | |
944 | * acceptable in 'name'. */ | |
945 | if (strchr(name, '/') || strchr(name, '\\')) { | |
946 | VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. " | |
947 | "A valid name must not include '/' or '\\'", | |
948 | name); | |
949 | return EINVAL; | |
950 | } | |
951 | ||
7d1ced01 CL |
952 | ovs_mutex_lock(&dpdk_mutex); |
953 | /* Take the name of the vhost-user port and append it to the location where | |
2d24d165 | 954 | * the socket is to be created, then register the socket. |
7d1ced01 | 955 | */ |
2d24d165 | 956 | snprintf(dev->vhost_id, sizeof dev->vhost_id, "%s/%s", |
01961bbd | 957 | dpdk_get_vhost_sock_dir(), name); |
1af27e8a | 958 | |
2d24d165 CL |
959 | dev->vhost_driver_flags &= ~RTE_VHOST_USER_CLIENT; |
960 | err = rte_vhost_driver_register(dev->vhost_id, dev->vhost_driver_flags); | |
7d1ced01 CL |
961 | if (err) { |
962 | VLOG_ERR("vhost-user socket device setup failure for socket %s\n", | |
2d24d165 | 963 | dev->vhost_id); |
e5c0f5a4 | 964 | } else { |
2d24d165 CL |
965 | fatal_signal_add_file_to_unlink(dev->vhost_id); |
966 | VLOG_INFO("Socket %s created for vhost-user port %s\n", | |
967 | dev->vhost_id, name); | |
968 | } | |
969 | err = netdev_dpdk_init(netdev, -1, DPDK_DEV_VHOST); | |
970 | ||
971 | ovs_mutex_unlock(&dpdk_mutex); | |
972 | return err; | |
973 | } | |
974 | ||
975 | static int | |
976 | netdev_dpdk_vhost_client_construct(struct netdev *netdev) | |
977 | { | |
978 | int err; | |
979 | ||
2d24d165 CL |
980 | ovs_mutex_lock(&dpdk_mutex); |
981 | err = netdev_dpdk_init(netdev, -1, DPDK_DEV_VHOST); | |
7d1ced01 | 982 | ovs_mutex_unlock(&dpdk_mutex); |
58397e6c KT |
983 | return err; |
984 | } | |
985 | ||
95fb793a | 986 | static int |
987 | netdev_dpdk_construct(struct netdev *netdev) | |
988 | { | |
989 | unsigned int port_no; | |
990 | int err; | |
991 | ||
95fb793a | 992 | /* Names always start with "dpdk" */ |
993 | err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no); | |
994 | if (err) { | |
995 | return err; | |
996 | } | |
997 | ||
998 | ovs_mutex_lock(&dpdk_mutex); | |
58397e6c | 999 | err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH); |
8a9562d2 PS |
1000 | ovs_mutex_unlock(&dpdk_mutex); |
1001 | return err; | |
1002 | } | |
1003 | ||
1004 | static void | |
d46285a2 | 1005 | netdev_dpdk_destruct(struct netdev *netdev) |
8a9562d2 | 1006 | { |
d46285a2 | 1007 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 | 1008 | |
8d38823b | 1009 | ovs_mutex_lock(&dpdk_mutex); |
8a9562d2 | 1010 | ovs_mutex_lock(&dev->mutex); |
8d38823b | 1011 | |
8a9562d2 | 1012 | rte_eth_dev_stop(dev->port_id); |
9509913a IS |
1013 | free(ovsrcu_get_protected(struct ingress_policer *, |
1014 | &dev->ingress_policer)); | |
8a9562d2 | 1015 | |
5a034064 | 1016 | rte_free(dev->tx_q); |
417e7e66 | 1017 | ovs_list_remove(&dev->list_node); |
8a9562d2 | 1018 | dpdk_mp_put(dev->dpdk_mp); |
8d38823b MK |
1019 | |
1020 | ovs_mutex_unlock(&dev->mutex); | |
8a9562d2 | 1021 | ovs_mutex_unlock(&dpdk_mutex); |
58397e6c | 1022 | } |
8a9562d2 | 1023 | |
3f891bbe DDP |
1024 | /* rte_vhost_driver_unregister() can call back destroy_device(), which will |
1025 | * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a | |
1026 | * deadlock, none of the mutexes must be held while calling this function. */ | |
1027 | static int | |
c1ff66ac CL |
1028 | dpdk_vhost_driver_unregister(struct netdev_dpdk *dev OVS_UNUSED, |
1029 | char *vhost_id) | |
3f891bbe DDP |
1030 | OVS_EXCLUDED(dpdk_mutex) |
1031 | OVS_EXCLUDED(dev->mutex) | |
1032 | { | |
c1ff66ac | 1033 | return rte_vhost_driver_unregister(vhost_id); |
3f891bbe DDP |
1034 | } |
1035 | ||
58397e6c | 1036 | static void |
d46285a2 | 1037 | netdev_dpdk_vhost_destruct(struct netdev *netdev) |
58397e6c | 1038 | { |
d46285a2 | 1039 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
c1ff66ac | 1040 | char *vhost_id; |
58397e6c | 1041 | |
8d38823b MK |
1042 | ovs_mutex_lock(&dpdk_mutex); |
1043 | ovs_mutex_lock(&dev->mutex); | |
1044 | ||
c62da695 | 1045 | /* Guest becomes an orphan if still attached. */ |
c1ff66ac CL |
1046 | if (netdev_dpdk_get_vid(dev) >= 0 |
1047 | && !(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) { | |
c62da695 | 1048 | VLOG_ERR("Removing port '%s' while vhost device still attached.", |
d46285a2 | 1049 | netdev->name); |
58be5c0e MK |
1050 | VLOG_ERR("To restore connectivity after re-adding of port, VM on " |
1051 | "socket '%s' must be restarted.", dev->vhost_id); | |
58397e6c KT |
1052 | } |
1053 | ||
9509913a IS |
1054 | free(ovsrcu_get_protected(struct ingress_policer *, |
1055 | &dev->ingress_policer)); | |
9509913a | 1056 | |
6019cb63 | 1057 | rte_free(dev->tx_q); |
417e7e66 | 1058 | ovs_list_remove(&dev->list_node); |
58397e6c | 1059 | dpdk_mp_put(dev->dpdk_mp); |
8d38823b | 1060 | |
2d24d165 | 1061 | vhost_id = xstrdup(dev->vhost_id); |
c1ff66ac | 1062 | |
8d38823b | 1063 | ovs_mutex_unlock(&dev->mutex); |
58397e6c | 1064 | ovs_mutex_unlock(&dpdk_mutex); |
3f891bbe | 1065 | |
569c26da | 1066 | if (!vhost_id[0]) { |
821b8664 IM |
1067 | goto out; |
1068 | } | |
1069 | ||
c1ff66ac | 1070 | if (dpdk_vhost_driver_unregister(dev, vhost_id)) { |
41964543 IM |
1071 | VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n", |
1072 | netdev->name, vhost_id); | |
c1ff66ac CL |
1073 | } else if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) { |
1074 | /* OVS server mode - remove this socket from list for deletion */ | |
1075 | fatal_signal_remove_file_to_unlink(vhost_id); | |
3f891bbe | 1076 | } |
821b8664 | 1077 | out: |
c1ff66ac | 1078 | free(vhost_id); |
8a9562d2 PS |
1079 | } |
1080 | ||
1081 | static void | |
d46285a2 | 1082 | netdev_dpdk_dealloc(struct netdev *netdev) |
8a9562d2 | 1083 | { |
d46285a2 | 1084 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 | 1085 | |
d46285a2 | 1086 | rte_free(dev); |
8a9562d2 PS |
1087 | } |
1088 | ||
1089 | static int | |
a14b8947 | 1090 | netdev_dpdk_get_config(const struct netdev *netdev, struct smap *args) |
8a9562d2 | 1091 | { |
a14b8947 | 1092 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
1093 | |
1094 | ovs_mutex_lock(&dev->mutex); | |
1095 | ||
050c60bf | 1096 | smap_add_format(args, "requested_rx_queues", "%d", dev->requested_n_rxq); |
a14b8947 | 1097 | smap_add_format(args, "configured_rx_queues", "%d", netdev->n_rxq); |
81acebda IM |
1098 | smap_add_format(args, "requested_tx_queues", "%d", dev->requested_n_txq); |
1099 | smap_add_format(args, "configured_tx_queues", "%d", netdev->n_txq); | |
0072e931 | 1100 | smap_add_format(args, "mtu", "%d", dev->mtu); |
451f26fd IM |
1101 | |
1102 | if (dev->type == DPDK_DEV_ETH) { | |
1103 | smap_add_format(args, "requested_rxq_descriptors", "%d", | |
1104 | dev->requested_rxq_size); | |
1105 | smap_add_format(args, "configured_rxq_descriptors", "%d", | |
1106 | dev->rxq_size); | |
1107 | smap_add_format(args, "requested_txq_descriptors", "%d", | |
1108 | dev->requested_txq_size); | |
1109 | smap_add_format(args, "configured_txq_descriptors", "%d", | |
1110 | dev->txq_size); | |
1a2bb118 SC |
1111 | if (dev->hw_ol_features & NETDEV_RX_CHECKSUM_OFFLOAD) { |
1112 | smap_add(args, "rx_csum_offload", "true"); | |
1113 | } | |
451f26fd | 1114 | } |
8a9562d2 PS |
1115 | ovs_mutex_unlock(&dev->mutex); |
1116 | ||
1117 | return 0; | |
1118 | } | |
1119 | ||
c3d062a7 CL |
1120 | static void |
1121 | dpdk_set_rxq_config(struct netdev_dpdk *dev, const struct smap *args) | |
b614c894 | 1122 | OVS_REQUIRES(dev->mutex) |
a14b8947 | 1123 | { |
050c60bf | 1124 | int new_n_rxq; |
a14b8947 | 1125 | |
2a21e757 | 1126 | new_n_rxq = MAX(smap_get_int(args, "n_rxq", NR_QUEUE), 1); |
050c60bf DDP |
1127 | if (new_n_rxq != dev->requested_n_rxq) { |
1128 | dev->requested_n_rxq = new_n_rxq; | |
c3d062a7 | 1129 | netdev_request_reconfigure(&dev->up); |
050c60bf | 1130 | } |
c3d062a7 CL |
1131 | } |
1132 | ||
b685696b CL |
1133 | static void |
1134 | dpdk_process_queue_size(struct netdev *netdev, const struct smap *args, | |
1135 | const char *flag, int default_size, int *new_size) | |
1136 | { | |
1137 | int queue_size = smap_get_int(args, flag, default_size); | |
1138 | ||
1139 | if (queue_size <= 0 || queue_size > NIC_PORT_MAX_Q_SIZE | |
1140 | || !is_pow2(queue_size)) { | |
1141 | queue_size = default_size; | |
1142 | } | |
1143 | ||
1144 | if (queue_size != *new_size) { | |
1145 | *new_size = queue_size; | |
1146 | netdev_request_reconfigure(netdev); | |
1147 | } | |
1148 | } | |
1149 | ||
c3d062a7 CL |
1150 | static int |
1151 | netdev_dpdk_set_config(struct netdev *netdev, const struct smap *args) | |
1152 | { | |
1153 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
b614c894 IM |
1154 | bool rx_fc_en, tx_fc_en, autoneg; |
1155 | enum rte_eth_fc_mode fc_mode; | |
1156 | static const enum rte_eth_fc_mode fc_mode_set[2][2] = { | |
1157 | {RTE_FC_NONE, RTE_FC_TX_PAUSE}, | |
1158 | {RTE_FC_RX_PAUSE, RTE_FC_FULL } | |
1159 | }; | |
1a2bb118 SC |
1160 | bool rx_chksm_ofld; |
1161 | bool temp_flag; | |
c3d062a7 CL |
1162 | |
1163 | ovs_mutex_lock(&dev->mutex); | |
1164 | ||
1165 | dpdk_set_rxq_config(dev, args); | |
1166 | ||
b685696b CL |
1167 | dpdk_process_queue_size(netdev, args, "n_rxq_desc", |
1168 | NIC_PORT_DEFAULT_RXQ_SIZE, | |
1169 | &dev->requested_rxq_size); | |
1170 | dpdk_process_queue_size(netdev, args, "n_txq_desc", | |
1171 | NIC_PORT_DEFAULT_TXQ_SIZE, | |
1172 | &dev->requested_txq_size); | |
1173 | ||
c3d062a7 CL |
1174 | rx_fc_en = smap_get_bool(args, "rx-flow-ctrl", false); |
1175 | tx_fc_en = smap_get_bool(args, "tx-flow-ctrl", false); | |
b614c894 | 1176 | autoneg = smap_get_bool(args, "flow-ctrl-autoneg", false); |
c3d062a7 | 1177 | |
b614c894 IM |
1178 | fc_mode = fc_mode_set[tx_fc_en][rx_fc_en]; |
1179 | if (dev->fc_conf.mode != fc_mode || autoneg != dev->fc_conf.autoneg) { | |
1180 | dev->fc_conf.mode = fc_mode; | |
1181 | dev->fc_conf.autoneg = autoneg; | |
1182 | dpdk_eth_flow_ctrl_setup(dev); | |
1183 | } | |
9fd39370 | 1184 | |
1a2bb118 SC |
1185 | /* Rx checksum offload configuration */ |
1186 | /* By default the Rx checksum offload is ON */ | |
1187 | rx_chksm_ofld = smap_get_bool(args, "rx-checksum-offload", true); | |
1188 | temp_flag = (dev->hw_ol_features & NETDEV_RX_CHECKSUM_OFFLOAD) | |
1189 | != 0; | |
1190 | if (temp_flag != rx_chksm_ofld) { | |
1191 | dev->hw_ol_features ^= NETDEV_RX_CHECKSUM_OFFLOAD; | |
1192 | dpdk_eth_checksum_offload_configure(dev); | |
1193 | } | |
c3d062a7 CL |
1194 | ovs_mutex_unlock(&dev->mutex); |
1195 | ||
1196 | return 0; | |
1197 | } | |
1198 | ||
1199 | static int | |
1200 | netdev_dpdk_ring_set_config(struct netdev *netdev, const struct smap *args) | |
1201 | { | |
1202 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
1203 | ||
1204 | ovs_mutex_lock(&dev->mutex); | |
1205 | dpdk_set_rxq_config(dev, args); | |
a14b8947 IM |
1206 | ovs_mutex_unlock(&dev->mutex); |
1207 | ||
1208 | return 0; | |
1209 | } | |
1210 | ||
c1ff66ac | 1211 | static int |
2d24d165 CL |
1212 | netdev_dpdk_vhost_client_set_config(struct netdev *netdev, |
1213 | const struct smap *args) | |
c1ff66ac CL |
1214 | { |
1215 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
1216 | const char *path; | |
1217 | ||
6881885a | 1218 | ovs_mutex_lock(&dev->mutex); |
c1ff66ac CL |
1219 | if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) { |
1220 | path = smap_get(args, "vhost-server-path"); | |
2d24d165 CL |
1221 | if (path && strcmp(path, dev->vhost_id)) { |
1222 | strcpy(dev->vhost_id, path); | |
c1ff66ac CL |
1223 | netdev_request_reconfigure(netdev); |
1224 | } | |
1225 | } | |
6881885a | 1226 | ovs_mutex_unlock(&dev->mutex); |
c1ff66ac CL |
1227 | |
1228 | return 0; | |
1229 | } | |
1230 | ||
7dec44fe | 1231 | static int |
d46285a2 | 1232 | netdev_dpdk_get_numa_id(const struct netdev *netdev) |
7dec44fe | 1233 | { |
d46285a2 | 1234 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
7dec44fe | 1235 | |
d46285a2 | 1236 | return dev->socket_id; |
7dec44fe AW |
1237 | } |
1238 | ||
050c60bf | 1239 | /* Sets the number of tx queues for the dpdk interface. */ |
5496878c | 1240 | static int |
050c60bf | 1241 | netdev_dpdk_set_tx_multiq(struct netdev *netdev, unsigned int n_txq) |
5496878c | 1242 | { |
d46285a2 | 1243 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
5496878c | 1244 | |
d46285a2 | 1245 | ovs_mutex_lock(&dev->mutex); |
91968eb0 | 1246 | |
050c60bf DDP |
1247 | if (dev->requested_n_txq == n_txq) { |
1248 | goto out; | |
4573fbd3 FL |
1249 | } |
1250 | ||
050c60bf DDP |
1251 | dev->requested_n_txq = n_txq; |
1252 | netdev_request_reconfigure(netdev); | |
58397e6c | 1253 | |
050c60bf | 1254 | out: |
d46285a2 | 1255 | ovs_mutex_unlock(&dev->mutex); |
050c60bf | 1256 | return 0; |
58397e6c KT |
1257 | } |
1258 | ||
8a9562d2 PS |
1259 | static struct netdev_rxq * |
1260 | netdev_dpdk_rxq_alloc(void) | |
1261 | { | |
1262 | struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx); | |
1263 | ||
eff23640 DDP |
1264 | if (rx) { |
1265 | return &rx->up; | |
1266 | } | |
1267 | ||
1268 | return NULL; | |
8a9562d2 PS |
1269 | } |
1270 | ||
1271 | static struct netdev_rxq_dpdk * | |
d46285a2 | 1272 | netdev_rxq_dpdk_cast(const struct netdev_rxq *rxq) |
8a9562d2 | 1273 | { |
d46285a2 | 1274 | return CONTAINER_OF(rxq, struct netdev_rxq_dpdk, up); |
8a9562d2 PS |
1275 | } |
1276 | ||
1277 | static int | |
d46285a2 | 1278 | netdev_dpdk_rxq_construct(struct netdev_rxq *rxq) |
8a9562d2 | 1279 | { |
d46285a2 DDP |
1280 | struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq); |
1281 | struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev); | |
8a9562d2 | 1282 | |
d46285a2 DDP |
1283 | ovs_mutex_lock(&dev->mutex); |
1284 | rx->port_id = dev->port_id; | |
1285 | ovs_mutex_unlock(&dev->mutex); | |
8a9562d2 PS |
1286 | |
1287 | return 0; | |
1288 | } | |
1289 | ||
1290 | static void | |
d46285a2 | 1291 | netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq OVS_UNUSED) |
8a9562d2 PS |
1292 | { |
1293 | } | |
1294 | ||
1295 | static void | |
d46285a2 | 1296 | netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq) |
8a9562d2 | 1297 | { |
d46285a2 | 1298 | struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq); |
8a9562d2 PS |
1299 | |
1300 | rte_free(rx); | |
1301 | } | |
1302 | ||
819f13bd DDP |
1303 | /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of |
1304 | * 'pkts', even in case of failure. | |
1305 | * | |
1306 | * Returns the number of packets that weren't transmitted. */ | |
1307 | static inline int | |
b59cc14e | 1308 | netdev_dpdk_eth_tx_burst(struct netdev_dpdk *dev, int qid, |
819f13bd | 1309 | struct rte_mbuf **pkts, int cnt) |
8a9562d2 | 1310 | { |
1304f1f8 DDP |
1311 | uint32_t nb_tx = 0; |
1312 | ||
b59cc14e | 1313 | while (nb_tx != cnt) { |
1304f1f8 DDP |
1314 | uint32_t ret; |
1315 | ||
b59cc14e | 1316 | ret = rte_eth_tx_burst(dev->port_id, qid, pkts + nb_tx, cnt - nb_tx); |
1304f1f8 DDP |
1317 | if (!ret) { |
1318 | break; | |
1319 | } | |
1320 | ||
1321 | nb_tx += ret; | |
1322 | } | |
8a9562d2 | 1323 | |
b59cc14e | 1324 | if (OVS_UNLIKELY(nb_tx != cnt)) { |
819f13bd | 1325 | /* Free buffers, which we couldn't transmit, one at a time (each |
db73f716 DDP |
1326 | * packet could come from a different mempool) */ |
1327 | int i; | |
1328 | ||
b59cc14e IM |
1329 | for (i = nb_tx; i < cnt; i++) { |
1330 | rte_pktmbuf_free(pkts[i]); | |
db73f716 | 1331 | } |
8a9562d2 | 1332 | } |
819f13bd DDP |
1333 | |
1334 | return cnt - nb_tx; | |
8a9562d2 PS |
1335 | } |
1336 | ||
f3926f29 IS |
1337 | static inline bool |
1338 | netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm *meter, | |
1339 | struct rte_mbuf *pkt, uint64_t time) | |
1340 | { | |
1341 | uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr); | |
1342 | ||
1343 | return rte_meter_srtcm_color_blind_check(meter, time, pkt_len) == | |
1344 | e_RTE_METER_GREEN; | |
1345 | } | |
1346 | ||
1347 | static int | |
1348 | netdev_dpdk_policer_run(struct rte_meter_srtcm *meter, | |
1349 | struct rte_mbuf **pkts, int pkt_cnt) | |
1350 | { | |
1351 | int i = 0; | |
1352 | int cnt = 0; | |
1353 | struct rte_mbuf *pkt = NULL; | |
1354 | uint64_t current_time = rte_rdtsc(); | |
1355 | ||
1356 | for (i = 0; i < pkt_cnt; i++) { | |
1357 | pkt = pkts[i]; | |
1358 | /* Handle current packet */ | |
1359 | if (netdev_dpdk_policer_pkt_handle(meter, pkt, current_time)) { | |
1360 | if (cnt != i) { | |
1361 | pkts[cnt] = pkt; | |
1362 | } | |
1363 | cnt++; | |
1364 | } else { | |
1365 | rte_pktmbuf_free(pkt); | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | return cnt; | |
1370 | } | |
1371 | ||
9509913a IS |
1372 | static int |
1373 | ingress_policer_run(struct ingress_policer *policer, struct rte_mbuf **pkts, | |
1374 | int pkt_cnt) | |
1375 | { | |
1376 | int cnt = 0; | |
1377 | ||
1378 | rte_spinlock_lock(&policer->policer_lock); | |
1379 | cnt = netdev_dpdk_policer_run(&policer->in_policer, pkts, pkt_cnt); | |
1380 | rte_spinlock_unlock(&policer->policer_lock); | |
1381 | ||
1382 | return cnt; | |
1383 | } | |
1384 | ||
58397e6c | 1385 | static bool |
0a0f39df | 1386 | is_vhost_running(struct netdev_dpdk *dev) |
58397e6c | 1387 | { |
0a0f39df | 1388 | return (netdev_dpdk_get_vid(dev) >= 0 && dev->vhost_reconfigured); |
58397e6c KT |
1389 | } |
1390 | ||
d6e3feb5 | 1391 | static inline void |
1392 | netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats *stats, | |
1393 | unsigned int packet_size) | |
1394 | { | |
1395 | /* Hard-coded search for the size bucket. */ | |
1396 | if (packet_size < 256) { | |
1397 | if (packet_size >= 128) { | |
1398 | stats->rx_128_to_255_packets++; | |
1399 | } else if (packet_size <= 64) { | |
1400 | stats->rx_1_to_64_packets++; | |
1401 | } else { | |
1402 | stats->rx_65_to_127_packets++; | |
1403 | } | |
1404 | } else { | |
1405 | if (packet_size >= 1523) { | |
1406 | stats->rx_1523_to_max_packets++; | |
1407 | } else if (packet_size >= 1024) { | |
1408 | stats->rx_1024_to_1522_packets++; | |
1409 | } else if (packet_size < 512) { | |
1410 | stats->rx_256_to_511_packets++; | |
1411 | } else { | |
1412 | stats->rx_512_to_1023_packets++; | |
1413 | } | |
1414 | } | |
1415 | } | |
1416 | ||
9e3ddd45 TP |
1417 | static inline void |
1418 | netdev_dpdk_vhost_update_rx_counters(struct netdev_stats *stats, | |
9509913a IS |
1419 | struct dp_packet **packets, int count, |
1420 | int dropped) | |
9e3ddd45 TP |
1421 | { |
1422 | int i; | |
d6e3feb5 | 1423 | unsigned int packet_size; |
9e3ddd45 TP |
1424 | struct dp_packet *packet; |
1425 | ||
1426 | stats->rx_packets += count; | |
9509913a | 1427 | stats->rx_dropped += dropped; |
9e3ddd45 TP |
1428 | for (i = 0; i < count; i++) { |
1429 | packet = packets[i]; | |
d6e3feb5 | 1430 | packet_size = dp_packet_size(packet); |
9e3ddd45 | 1431 | |
d6e3feb5 | 1432 | if (OVS_UNLIKELY(packet_size < ETH_HEADER_LEN)) { |
9e3ddd45 TP |
1433 | /* This only protects the following multicast counting from |
1434 | * too short packets, but it does not stop the packet from | |
1435 | * further processing. */ | |
1436 | stats->rx_errors++; | |
1437 | stats->rx_length_errors++; | |
1438 | continue; | |
1439 | } | |
1440 | ||
d6e3feb5 | 1441 | netdev_dpdk_vhost_update_rx_size_counters(stats, packet_size); |
1442 | ||
9e3ddd45 TP |
1443 | struct eth_header *eh = (struct eth_header *) dp_packet_data(packet); |
1444 | if (OVS_UNLIKELY(eth_addr_is_multicast(eh->eth_dst))) { | |
1445 | stats->multicast++; | |
1446 | } | |
1447 | ||
d6e3feb5 | 1448 | stats->rx_bytes += packet_size; |
9e3ddd45 TP |
1449 | } |
1450 | } | |
1451 | ||
58397e6c KT |
1452 | /* |
1453 | * The receive path for the vhost port is the TX path out from guest. | |
1454 | */ | |
1455 | static int | |
d46285a2 | 1456 | netdev_dpdk_vhost_rxq_recv(struct netdev_rxq *rxq, |
64839cf4 | 1457 | struct dp_packet_batch *batch) |
58397e6c | 1458 | { |
d46285a2 | 1459 | struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev); |
d46285a2 | 1460 | int qid = rxq->queue_id; |
9509913a | 1461 | struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev); |
58397e6c | 1462 | uint16_t nb_rx = 0; |
9509913a | 1463 | uint16_t dropped = 0; |
58397e6c | 1464 | |
0a0f39df | 1465 | if (OVS_UNLIKELY(!is_vhost_running(dev) |
e543851d | 1466 | || !(dev->flags & NETDEV_UP))) { |
58397e6c KT |
1467 | return EAGAIN; |
1468 | } | |
1469 | ||
0a0f39df CL |
1470 | nb_rx = rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev), |
1471 | qid * VIRTIO_QNUM + VIRTIO_TXQ, | |
d46285a2 | 1472 | dev->dpdk_mp->mp, |
64839cf4 | 1473 | (struct rte_mbuf **) batch->packets, |
cd159f1a | 1474 | NETDEV_MAX_BURST); |
58397e6c KT |
1475 | if (!nb_rx) { |
1476 | return EAGAIN; | |
1477 | } | |
1478 | ||
9509913a IS |
1479 | if (policer) { |
1480 | dropped = nb_rx; | |
64839cf4 WT |
1481 | nb_rx = ingress_policer_run(policer, |
1482 | (struct rte_mbuf **) batch->packets, | |
1483 | nb_rx); | |
9509913a IS |
1484 | dropped -= nb_rx; |
1485 | } | |
1486 | ||
d46285a2 | 1487 | rte_spinlock_lock(&dev->stats_lock); |
64839cf4 WT |
1488 | netdev_dpdk_vhost_update_rx_counters(&dev->stats, batch->packets, |
1489 | nb_rx, dropped); | |
d46285a2 | 1490 | rte_spinlock_unlock(&dev->stats_lock); |
45d947c4 | 1491 | |
64839cf4 | 1492 | batch->count = (int) nb_rx; |
58397e6c KT |
1493 | return 0; |
1494 | } | |
1495 | ||
8a9562d2 | 1496 | static int |
64839cf4 | 1497 | netdev_dpdk_rxq_recv(struct netdev_rxq *rxq, struct dp_packet_batch *batch) |
8a9562d2 | 1498 | { |
d46285a2 DDP |
1499 | struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq); |
1500 | struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev); | |
9509913a | 1501 | struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev); |
8a9562d2 | 1502 | int nb_rx; |
9509913a | 1503 | int dropped = 0; |
8a9562d2 | 1504 | |
d46285a2 | 1505 | nb_rx = rte_eth_rx_burst(rx->port_id, rxq->queue_id, |
64839cf4 | 1506 | (struct rte_mbuf **) batch->packets, |
cd159f1a | 1507 | NETDEV_MAX_BURST); |
8a9562d2 PS |
1508 | if (!nb_rx) { |
1509 | return EAGAIN; | |
1510 | } | |
1511 | ||
9509913a IS |
1512 | if (policer) { |
1513 | dropped = nb_rx; | |
64839cf4 | 1514 | nb_rx = ingress_policer_run(policer, |
58be5c0e | 1515 | (struct rte_mbuf **) batch->packets, |
64839cf4 | 1516 | nb_rx); |
9509913a IS |
1517 | dropped -= nb_rx; |
1518 | } | |
1519 | ||
1520 | /* Update stats to reflect dropped packets */ | |
1521 | if (OVS_UNLIKELY(dropped)) { | |
1522 | rte_spinlock_lock(&dev->stats_lock); | |
1523 | dev->stats.rx_dropped += dropped; | |
1524 | rte_spinlock_unlock(&dev->stats_lock); | |
1525 | } | |
1526 | ||
64839cf4 | 1527 | batch->count = nb_rx; |
8a9562d2 PS |
1528 | |
1529 | return 0; | |
1530 | } | |
1531 | ||
0bf765f7 | 1532 | static inline int |
78bd47cf DDP |
1533 | netdev_dpdk_qos_run(struct netdev_dpdk *dev, struct rte_mbuf **pkts, |
1534 | int cnt) | |
0bf765f7 | 1535 | { |
78bd47cf | 1536 | struct qos_conf *qos_conf = ovsrcu_get(struct qos_conf *, &dev->qos_conf); |
0bf765f7 | 1537 | |
78bd47cf DDP |
1538 | if (qos_conf) { |
1539 | rte_spinlock_lock(&qos_conf->lock); | |
1540 | cnt = qos_conf->ops->qos_run(qos_conf, pkts, cnt); | |
1541 | rte_spinlock_unlock(&qos_conf->lock); | |
0bf765f7 IS |
1542 | } |
1543 | ||
1544 | return cnt; | |
1545 | } | |
1546 | ||
c6ec9d17 IM |
1547 | static int |
1548 | netdev_dpdk_filter_packet_len(struct netdev_dpdk *dev, struct rte_mbuf **pkts, | |
1549 | int pkt_cnt) | |
1550 | { | |
1551 | int i = 0; | |
1552 | int cnt = 0; | |
1553 | struct rte_mbuf *pkt; | |
1554 | ||
1555 | for (i = 0; i < pkt_cnt; i++) { | |
1556 | pkt = pkts[i]; | |
1557 | if (OVS_UNLIKELY(pkt->pkt_len > dev->max_packet_len)) { | |
1558 | VLOG_WARN_RL(&rl, "%s: Too big size %" PRIu32 " max_packet_len %d", | |
1559 | dev->up.name, pkt->pkt_len, dev->max_packet_len); | |
1560 | rte_pktmbuf_free(pkt); | |
1561 | continue; | |
1562 | } | |
1563 | ||
1564 | if (OVS_UNLIKELY(i != cnt)) { | |
1565 | pkts[cnt] = pkt; | |
1566 | } | |
1567 | cnt++; | |
1568 | } | |
1569 | ||
1570 | return cnt; | |
1571 | } | |
1572 | ||
9e3ddd45 TP |
1573 | static inline void |
1574 | netdev_dpdk_vhost_update_tx_counters(struct netdev_stats *stats, | |
1575 | struct dp_packet **packets, | |
1576 | int attempted, | |
1577 | int dropped) | |
1578 | { | |
1579 | int i; | |
1580 | int sent = attempted - dropped; | |
1581 | ||
1582 | stats->tx_packets += sent; | |
1583 | stats->tx_dropped += dropped; | |
1584 | ||
1585 | for (i = 0; i < sent; i++) { | |
1586 | stats->tx_bytes += dp_packet_size(packets[i]); | |
1587 | } | |
1588 | } | |
1589 | ||
58397e6c | 1590 | static void |
4573fbd3 | 1591 | __netdev_dpdk_vhost_send(struct netdev *netdev, int qid, |
dd52de45 | 1592 | struct dp_packet **pkts, int cnt) |
58397e6c | 1593 | { |
d46285a2 | 1594 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
95e9881f KT |
1595 | struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts; |
1596 | unsigned int total_pkts = cnt; | |
c6ec9d17 | 1597 | unsigned int dropped = 0; |
dd52de45 | 1598 | int i, retries = 0; |
58397e6c | 1599 | |
81acebda | 1600 | qid = dev->tx_q[qid % netdev->n_txq].map; |
585a5bea | 1601 | |
0a0f39df | 1602 | if (OVS_UNLIKELY(!is_vhost_running(dev) || qid < 0 |
e543851d | 1603 | || !(dev->flags & NETDEV_UP))) { |
d46285a2 DDP |
1604 | rte_spinlock_lock(&dev->stats_lock); |
1605 | dev->stats.tx_dropped+= cnt; | |
1606 | rte_spinlock_unlock(&dev->stats_lock); | |
1b99bb05 | 1607 | goto out; |
58397e6c KT |
1608 | } |
1609 | ||
d46285a2 | 1610 | rte_spinlock_lock(&dev->tx_q[qid].tx_lock); |
58397e6c | 1611 | |
c6ec9d17 | 1612 | cnt = netdev_dpdk_filter_packet_len(dev, cur_pkts, cnt); |
0bf765f7 | 1613 | /* Check has QoS has been configured for the netdev */ |
78bd47cf | 1614 | cnt = netdev_dpdk_qos_run(dev, cur_pkts, cnt); |
c6ec9d17 | 1615 | dropped = total_pkts - cnt; |
0bf765f7 | 1616 | |
95e9881f | 1617 | do { |
4573fbd3 | 1618 | int vhost_qid = qid * VIRTIO_QNUM + VIRTIO_RXQ; |
95e9881f KT |
1619 | unsigned int tx_pkts; |
1620 | ||
0a0f39df | 1621 | tx_pkts = rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev), |
c6ec9d17 | 1622 | vhost_qid, cur_pkts, cnt); |
95e9881f KT |
1623 | if (OVS_LIKELY(tx_pkts)) { |
1624 | /* Packets have been sent.*/ | |
1625 | cnt -= tx_pkts; | |
31871ee3 | 1626 | /* Prepare for possible retry.*/ |
95e9881f KT |
1627 | cur_pkts = &cur_pkts[tx_pkts]; |
1628 | } else { | |
31871ee3 KT |
1629 | /* No packets sent - do not retry.*/ |
1630 | break; | |
95e9881f | 1631 | } |
c6ec9d17 | 1632 | } while (cnt && (retries++ <= VHOST_ENQ_RETRY_NUM)); |
4573fbd3 | 1633 | |
d46285a2 | 1634 | rte_spinlock_unlock(&dev->tx_q[qid].tx_lock); |
95e9881f | 1635 | |
d46285a2 | 1636 | rte_spinlock_lock(&dev->stats_lock); |
0072e931 | 1637 | netdev_dpdk_vhost_update_tx_counters(&dev->stats, pkts, total_pkts, |
c6ec9d17 | 1638 | cnt + dropped); |
d46285a2 | 1639 | rte_spinlock_unlock(&dev->stats_lock); |
58397e6c KT |
1640 | |
1641 | out: | |
c6ec9d17 | 1642 | for (i = 0; i < total_pkts - dropped; i++) { |
dd52de45 | 1643 | dp_packet_delete(pkts[i]); |
58397e6c KT |
1644 | } |
1645 | } | |
1646 | ||
8a9562d2 PS |
1647 | /* Tx function. Transmit packets indefinitely */ |
1648 | static void | |
64839cf4 | 1649 | dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch) |
db73f716 | 1650 | OVS_NO_THREAD_SAFETY_ANALYSIS |
8a9562d2 | 1651 | { |
bce01e3a | 1652 | #if !defined(__CHECKER__) && !defined(_WIN32) |
64839cf4 | 1653 | const size_t PKT_ARRAY_SIZE = batch->count; |
bce01e3a EJ |
1654 | #else |
1655 | /* Sparse or MSVC doesn't like variable length array. */ | |
cd159f1a | 1656 | enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST }; |
bce01e3a | 1657 | #endif |
8a9562d2 | 1658 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
2391135c | 1659 | struct rte_mbuf *pkts[PKT_ARRAY_SIZE]; |
175cf4de RW |
1660 | int dropped = 0; |
1661 | int newcnt = 0; | |
1662 | int i; | |
8a9562d2 | 1663 | |
7d6d1a40 WT |
1664 | dp_packet_batch_apply_cutlen(batch); |
1665 | ||
64839cf4 WT |
1666 | for (i = 0; i < batch->count; i++) { |
1667 | int size = dp_packet_size(batch->packets[i]); | |
95fb793a | 1668 | |
f98d7864 | 1669 | if (OVS_UNLIKELY(size > dev->max_packet_len)) { |
f4fd623c | 1670 | VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d", |
58be5c0e | 1671 | (int) size, dev->max_packet_len); |
f4fd623c | 1672 | |
175cf4de | 1673 | dropped++; |
f4fd623c DDP |
1674 | continue; |
1675 | } | |
8a9562d2 | 1676 | |
2391135c | 1677 | pkts[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp); |
8a9562d2 | 1678 | |
2391135c | 1679 | if (!pkts[newcnt]) { |
64839cf4 | 1680 | dropped += batch->count - i; |
175cf4de | 1681 | break; |
f4fd623c DDP |
1682 | } |
1683 | ||
1684 | /* We have to do a copy for now */ | |
2391135c | 1685 | memcpy(rte_pktmbuf_mtod(pkts[newcnt], void *), |
64839cf4 | 1686 | dp_packet_data(batch->packets[i]), size); |
f4fd623c | 1687 | |
2391135c MK |
1688 | rte_pktmbuf_data_len(pkts[newcnt]) = size; |
1689 | rte_pktmbuf_pkt_len(pkts[newcnt]) = size; | |
f4fd623c DDP |
1690 | |
1691 | newcnt++; | |
1692 | } | |
8a9562d2 | 1693 | |
58397e6c | 1694 | if (dev->type == DPDK_DEV_VHOST) { |
2391135c | 1695 | __netdev_dpdk_vhost_send(netdev, qid, (struct dp_packet **) pkts, |
dd52de45 | 1696 | newcnt); |
58397e6c | 1697 | } else { |
0bf765f7 IS |
1698 | unsigned int qos_pkts = newcnt; |
1699 | ||
1700 | /* Check if QoS has been configured for this netdev. */ | |
78bd47cf | 1701 | newcnt = netdev_dpdk_qos_run(dev, pkts, newcnt); |
0bf765f7 IS |
1702 | |
1703 | dropped += qos_pkts - newcnt; | |
819f13bd | 1704 | dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, newcnt); |
58397e6c | 1705 | } |
db73f716 | 1706 | |
0bf765f7 IS |
1707 | if (OVS_UNLIKELY(dropped)) { |
1708 | rte_spinlock_lock(&dev->stats_lock); | |
1709 | dev->stats.tx_dropped += dropped; | |
1710 | rte_spinlock_unlock(&dev->stats_lock); | |
1711 | } | |
8a9562d2 PS |
1712 | } |
1713 | ||
58397e6c | 1714 | static int |
64839cf4 WT |
1715 | netdev_dpdk_vhost_send(struct netdev *netdev, int qid, |
1716 | struct dp_packet_batch *batch, | |
324c8374 | 1717 | bool may_steal, bool concurrent_txq OVS_UNUSED) |
58397e6c | 1718 | { |
58397e6c | 1719 | |
dd52de45 | 1720 | if (OVS_UNLIKELY(!may_steal || batch->packets[0]->source != DPBUF_DPDK)) { |
64839cf4 WT |
1721 | dpdk_do_tx_copy(netdev, qid, batch); |
1722 | dp_packet_delete_batch(batch, may_steal); | |
58397e6c | 1723 | } else { |
64839cf4 | 1724 | dp_packet_batch_apply_cutlen(batch); |
dd52de45 | 1725 | __netdev_dpdk_vhost_send(netdev, qid, batch->packets, batch->count); |
58397e6c KT |
1726 | } |
1727 | return 0; | |
1728 | } | |
1729 | ||
7251515e DV |
1730 | static inline void |
1731 | netdev_dpdk_send__(struct netdev_dpdk *dev, int qid, | |
324c8374 IM |
1732 | struct dp_packet_batch *batch, bool may_steal, |
1733 | bool concurrent_txq) | |
8a9562d2 | 1734 | { |
324c8374 | 1735 | if (OVS_UNLIKELY(concurrent_txq)) { |
81acebda | 1736 | qid = qid % dev->up.n_txq; |
a0cb2d66 DDP |
1737 | rte_spinlock_lock(&dev->tx_q[qid].tx_lock); |
1738 | } | |
1739 | ||
7251515e | 1740 | if (OVS_UNLIKELY(!may_steal || |
64839cf4 | 1741 | batch->packets[0]->source != DPBUF_DPDK)) { |
7251515e DV |
1742 | struct netdev *netdev = &dev->up; |
1743 | ||
64839cf4 WT |
1744 | dpdk_do_tx_copy(netdev, qid, batch); |
1745 | dp_packet_delete_batch(batch, may_steal); | |
8a9562d2 | 1746 | } else { |
6b094bf4 | 1747 | int dropped; |
64839cf4 | 1748 | int cnt = batch->count; |
2391135c | 1749 | struct rte_mbuf **pkts = (struct rte_mbuf **) batch->packets; |
8a9562d2 | 1750 | |
7d6d1a40 WT |
1751 | dp_packet_batch_apply_cutlen(batch); |
1752 | ||
2391135c | 1753 | cnt = netdev_dpdk_filter_packet_len(dev, pkts, cnt); |
78bd47cf | 1754 | cnt = netdev_dpdk_qos_run(dev, pkts, cnt); |
6b094bf4 | 1755 | dropped = batch->count - cnt; |
1b99bb05 | 1756 | |
819f13bd | 1757 | dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, cnt); |
8a9562d2 | 1758 | |
f4fd623c | 1759 | if (OVS_UNLIKELY(dropped)) { |
45d947c4 | 1760 | rte_spinlock_lock(&dev->stats_lock); |
f4fd623c | 1761 | dev->stats.tx_dropped += dropped; |
45d947c4 | 1762 | rte_spinlock_unlock(&dev->stats_lock); |
f4fd623c | 1763 | } |
8a9562d2 | 1764 | } |
a0cb2d66 | 1765 | |
324c8374 | 1766 | if (OVS_UNLIKELY(concurrent_txq)) { |
a0cb2d66 DDP |
1767 | rte_spinlock_unlock(&dev->tx_q[qid].tx_lock); |
1768 | } | |
7251515e DV |
1769 | } |
1770 | ||
1771 | static int | |
1772 | netdev_dpdk_eth_send(struct netdev *netdev, int qid, | |
324c8374 IM |
1773 | struct dp_packet_batch *batch, bool may_steal, |
1774 | bool concurrent_txq) | |
7251515e DV |
1775 | { |
1776 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
8a9562d2 | 1777 | |
324c8374 | 1778 | netdev_dpdk_send__(dev, qid, batch, may_steal, concurrent_txq); |
7251515e | 1779 | return 0; |
8a9562d2 PS |
1780 | } |
1781 | ||
1782 | static int | |
74ff3298 | 1783 | netdev_dpdk_set_etheraddr(struct netdev *netdev, const struct eth_addr mac) |
8a9562d2 PS |
1784 | { |
1785 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
1786 | ||
1787 | ovs_mutex_lock(&dev->mutex); | |
1788 | if (!eth_addr_equals(dev->hwaddr, mac)) { | |
74ff3298 | 1789 | dev->hwaddr = mac; |
045c0d1a | 1790 | netdev_change_seq_changed(netdev); |
8a9562d2 PS |
1791 | } |
1792 | ovs_mutex_unlock(&dev->mutex); | |
1793 | ||
1794 | return 0; | |
1795 | } | |
1796 | ||
1797 | static int | |
74ff3298 | 1798 | netdev_dpdk_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac) |
8a9562d2 PS |
1799 | { |
1800 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
1801 | ||
1802 | ovs_mutex_lock(&dev->mutex); | |
74ff3298 | 1803 | *mac = dev->hwaddr; |
8a9562d2 PS |
1804 | ovs_mutex_unlock(&dev->mutex); |
1805 | ||
1806 | return 0; | |
1807 | } | |
1808 | ||
1809 | static int | |
1810 | netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup) | |
1811 | { | |
1812 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
1813 | ||
1814 | ovs_mutex_lock(&dev->mutex); | |
1815 | *mtup = dev->mtu; | |
1816 | ovs_mutex_unlock(&dev->mutex); | |
1817 | ||
1818 | return 0; | |
1819 | } | |
1820 | ||
0072e931 MK |
1821 | static int |
1822 | netdev_dpdk_set_mtu(struct netdev *netdev, int mtu) | |
1823 | { | |
1824 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
1825 | ||
1826 | if (MTU_TO_FRAME_LEN(mtu) > NETDEV_DPDK_MAX_PKT_LEN | |
1827 | || mtu < ETHER_MIN_MTU) { | |
1828 | VLOG_WARN("%s: unsupported MTU %d\n", dev->up.name, mtu); | |
1829 | return EINVAL; | |
1830 | } | |
1831 | ||
1832 | ovs_mutex_lock(&dev->mutex); | |
1833 | if (dev->requested_mtu != mtu) { | |
1834 | dev->requested_mtu = mtu; | |
1835 | netdev_request_reconfigure(netdev); | |
1836 | } | |
1837 | ovs_mutex_unlock(&dev->mutex); | |
1838 | ||
1839 | return 0; | |
1840 | } | |
1841 | ||
8a9562d2 | 1842 | static int |
d46285a2 | 1843 | netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier); |
8a9562d2 | 1844 | |
58397e6c KT |
1845 | static int |
1846 | netdev_dpdk_vhost_get_stats(const struct netdev *netdev, | |
1847 | struct netdev_stats *stats) | |
1848 | { | |
1849 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
1850 | ||
1851 | ovs_mutex_lock(&dev->mutex); | |
58397e6c | 1852 | |
45d947c4 | 1853 | rte_spinlock_lock(&dev->stats_lock); |
58397e6c KT |
1854 | /* Supported Stats */ |
1855 | stats->rx_packets += dev->stats.rx_packets; | |
1856 | stats->tx_packets += dev->stats.tx_packets; | |
9509913a | 1857 | stats->rx_dropped = dev->stats.rx_dropped; |
58397e6c | 1858 | stats->tx_dropped += dev->stats.tx_dropped; |
9e3ddd45 TP |
1859 | stats->multicast = dev->stats.multicast; |
1860 | stats->rx_bytes = dev->stats.rx_bytes; | |
1861 | stats->tx_bytes = dev->stats.tx_bytes; | |
1862 | stats->rx_errors = dev->stats.rx_errors; | |
1863 | stats->rx_length_errors = dev->stats.rx_length_errors; | |
d6e3feb5 | 1864 | |
1865 | stats->rx_1_to_64_packets = dev->stats.rx_1_to_64_packets; | |
1866 | stats->rx_65_to_127_packets = dev->stats.rx_65_to_127_packets; | |
1867 | stats->rx_128_to_255_packets = dev->stats.rx_128_to_255_packets; | |
1868 | stats->rx_256_to_511_packets = dev->stats.rx_256_to_511_packets; | |
1869 | stats->rx_512_to_1023_packets = dev->stats.rx_512_to_1023_packets; | |
1870 | stats->rx_1024_to_1522_packets = dev->stats.rx_1024_to_1522_packets; | |
1871 | stats->rx_1523_to_max_packets = dev->stats.rx_1523_to_max_packets; | |
1872 | ||
45d947c4 | 1873 | rte_spinlock_unlock(&dev->stats_lock); |
9e3ddd45 | 1874 | |
58397e6c KT |
1875 | ovs_mutex_unlock(&dev->mutex); |
1876 | ||
1877 | return 0; | |
1878 | } | |
1879 | ||
d6e3feb5 | 1880 | static void |
1881 | netdev_dpdk_convert_xstats(struct netdev_stats *stats, | |
0a0f39df CL |
1882 | const struct rte_eth_xstat *xstats, |
1883 | const struct rte_eth_xstat_name *names, | |
d6e3feb5 | 1884 | const unsigned int size) |
1885 | { | |
d6e3feb5 | 1886 | for (unsigned int i = 0; i < size; i++) { |
0a0f39df | 1887 | if (strcmp(XSTAT_RX_64_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1888 | stats->rx_1_to_64_packets = xstats[i].value; |
0a0f39df | 1889 | } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1890 | stats->rx_65_to_127_packets = xstats[i].value; |
0a0f39df | 1891 | } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1892 | stats->rx_128_to_255_packets = xstats[i].value; |
0a0f39df | 1893 | } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1894 | stats->rx_256_to_511_packets = xstats[i].value; |
0a0f39df | 1895 | } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1896 | stats->rx_512_to_1023_packets = xstats[i].value; |
0a0f39df | 1897 | } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1898 | stats->rx_1024_to_1522_packets = xstats[i].value; |
0a0f39df | 1899 | } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1900 | stats->rx_1523_to_max_packets = xstats[i].value; |
0a0f39df | 1901 | } else if (strcmp(XSTAT_TX_64_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1902 | stats->tx_1_to_64_packets = xstats[i].value; |
0a0f39df | 1903 | } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1904 | stats->tx_65_to_127_packets = xstats[i].value; |
0a0f39df | 1905 | } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1906 | stats->tx_128_to_255_packets = xstats[i].value; |
0a0f39df | 1907 | } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1908 | stats->tx_256_to_511_packets = xstats[i].value; |
0a0f39df | 1909 | } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1910 | stats->tx_512_to_1023_packets = xstats[i].value; |
0a0f39df | 1911 | } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1912 | stats->tx_1024_to_1522_packets = xstats[i].value; |
0a0f39df | 1913 | } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1914 | stats->tx_1523_to_max_packets = xstats[i].value; |
0a0f39df | 1915 | } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1916 | stats->tx_multicast_packets = xstats[i].value; |
0a0f39df | 1917 | } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1918 | stats->rx_broadcast_packets = xstats[i].value; |
0a0f39df | 1919 | } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS, names[i].name) == 0) { |
d6e3feb5 | 1920 | stats->tx_broadcast_packets = xstats[i].value; |
0a0f39df | 1921 | } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS, names[i].name) == 0) { |
d6e3feb5 | 1922 | stats->rx_undersized_errors = xstats[i].value; |
0a0f39df | 1923 | } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS, names[i].name) == 0) { |
d6e3feb5 | 1924 | stats->rx_fragmented_errors = xstats[i].value; |
0a0f39df | 1925 | } else if (strcmp(XSTAT_RX_JABBER_ERRORS, names[i].name) == 0) { |
d6e3feb5 | 1926 | stats->rx_jabber_errors = xstats[i].value; |
1927 | } | |
1928 | } | |
1929 | } | |
1930 | ||
8a9562d2 PS |
1931 | static int |
1932 | netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats) | |
1933 | { | |
1934 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
1935 | struct rte_eth_stats rte_stats; | |
1936 | bool gg; | |
1937 | ||
1938 | netdev_dpdk_get_carrier(netdev, &gg); | |
1939 | ovs_mutex_lock(&dev->mutex); | |
8a9562d2 | 1940 | |
0a0f39df CL |
1941 | struct rte_eth_xstat *rte_xstats = NULL; |
1942 | struct rte_eth_xstat_name *rte_xstats_names = NULL; | |
1943 | int rte_xstats_len, rte_xstats_new_len, rte_xstats_ret; | |
d6e3feb5 | 1944 | |
1945 | if (rte_eth_stats_get(dev->port_id, &rte_stats)) { | |
1946 | VLOG_ERR("Can't get ETH statistics for port: %i.", dev->port_id); | |
f9256822 | 1947 | ovs_mutex_unlock(&dev->mutex); |
d6e3feb5 | 1948 | return EPROTO; |
1949 | } | |
1950 | ||
0a0f39df CL |
1951 | /* Get length of statistics */ |
1952 | rte_xstats_len = rte_eth_xstats_get_names(dev->port_id, NULL, 0); | |
1953 | if (rte_xstats_len < 0) { | |
1954 | VLOG_WARN("Cannot get XSTATS values for port: %i", dev->port_id); | |
1955 | goto out; | |
1956 | } | |
1957 | /* Reserve memory for xstats names and values */ | |
1958 | rte_xstats_names = xcalloc(rte_xstats_len, sizeof *rte_xstats_names); | |
1959 | rte_xstats = xcalloc(rte_xstats_len, sizeof *rte_xstats); | |
1960 | ||
1961 | /* Retreive xstats names */ | |
1962 | rte_xstats_new_len = rte_eth_xstats_get_names(dev->port_id, | |
1963 | rte_xstats_names, | |
1964 | rte_xstats_len); | |
1965 | if (rte_xstats_new_len != rte_xstats_len) { | |
1966 | VLOG_WARN("Cannot get XSTATS names for port: %i.", dev->port_id); | |
1967 | goto out; | |
1968 | } | |
1969 | /* Retreive xstats values */ | |
1970 | memset(rte_xstats, 0xff, sizeof *rte_xstats * rte_xstats_len); | |
1971 | rte_xstats_ret = rte_eth_xstats_get(dev->port_id, rte_xstats, | |
1972 | rte_xstats_len); | |
1973 | if (rte_xstats_ret > 0 && rte_xstats_ret <= rte_xstats_len) { | |
1974 | netdev_dpdk_convert_xstats(stats, rte_xstats, rte_xstats_names, | |
1975 | rte_xstats_len); | |
d6e3feb5 | 1976 | } else { |
0a0f39df | 1977 | VLOG_WARN("Cannot get XSTATS values for port: %i.", dev->port_id); |
d6e3feb5 | 1978 | } |
8a9562d2 | 1979 | |
0a0f39df CL |
1980 | out: |
1981 | free(rte_xstats); | |
1982 | free(rte_xstats_names); | |
1983 | ||
2f9dd77f PS |
1984 | stats->rx_packets = rte_stats.ipackets; |
1985 | stats->tx_packets = rte_stats.opackets; | |
1986 | stats->rx_bytes = rte_stats.ibytes; | |
1987 | stats->tx_bytes = rte_stats.obytes; | |
9e3ddd45 TP |
1988 | /* DPDK counts imissed as errors, but count them here as dropped instead */ |
1989 | stats->rx_errors = rte_stats.ierrors - rte_stats.imissed; | |
2f9dd77f | 1990 | stats->tx_errors = rte_stats.oerrors; |
8a9562d2 | 1991 | |
45d947c4 | 1992 | rte_spinlock_lock(&dev->stats_lock); |
2f9dd77f | 1993 | stats->tx_dropped = dev->stats.tx_dropped; |
9509913a | 1994 | stats->rx_dropped = dev->stats.rx_dropped; |
45d947c4 | 1995 | rte_spinlock_unlock(&dev->stats_lock); |
9e3ddd45 TP |
1996 | |
1997 | /* These are the available DPDK counters for packets not received due to | |
1998 | * local resource constraints in DPDK and NIC respectively. */ | |
9509913a | 1999 | stats->rx_dropped += rte_stats.rx_nombuf + rte_stats.imissed; |
9e3ddd45 TP |
2000 | stats->rx_missed_errors = rte_stats.imissed; |
2001 | ||
8a9562d2 PS |
2002 | ovs_mutex_unlock(&dev->mutex); |
2003 | ||
2004 | return 0; | |
2005 | } | |
2006 | ||
2007 | static int | |
d46285a2 | 2008 | netdev_dpdk_get_features(const struct netdev *netdev, |
8a9562d2 | 2009 | enum netdev_features *current, |
ca3d4f55 BX |
2010 | enum netdev_features *advertised, |
2011 | enum netdev_features *supported, | |
2012 | enum netdev_features *peer) | |
8a9562d2 | 2013 | { |
d46285a2 | 2014 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
2015 | struct rte_eth_link link; |
2016 | ||
2017 | ovs_mutex_lock(&dev->mutex); | |
2018 | link = dev->link; | |
2019 | ovs_mutex_unlock(&dev->mutex); | |
2020 | ||
362ca396 | 2021 | if (link.link_duplex == ETH_LINK_HALF_DUPLEX) { |
2022 | if (link.link_speed == ETH_SPEED_NUM_10M) { | |
8a9562d2 PS |
2023 | *current = NETDEV_F_10MB_HD; |
2024 | } | |
362ca396 | 2025 | if (link.link_speed == ETH_SPEED_NUM_100M) { |
8a9562d2 PS |
2026 | *current = NETDEV_F_100MB_HD; |
2027 | } | |
362ca396 | 2028 | if (link.link_speed == ETH_SPEED_NUM_1G) { |
8a9562d2 PS |
2029 | *current = NETDEV_F_1GB_HD; |
2030 | } | |
2031 | } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) { | |
362ca396 | 2032 | if (link.link_speed == ETH_SPEED_NUM_10M) { |
8a9562d2 PS |
2033 | *current = NETDEV_F_10MB_FD; |
2034 | } | |
362ca396 | 2035 | if (link.link_speed == ETH_SPEED_NUM_100M) { |
8a9562d2 PS |
2036 | *current = NETDEV_F_100MB_FD; |
2037 | } | |
362ca396 | 2038 | if (link.link_speed == ETH_SPEED_NUM_1G) { |
8a9562d2 PS |
2039 | *current = NETDEV_F_1GB_FD; |
2040 | } | |
362ca396 | 2041 | if (link.link_speed == ETH_SPEED_NUM_10G) { |
8a9562d2 PS |
2042 | *current = NETDEV_F_10GB_FD; |
2043 | } | |
2044 | } | |
2045 | ||
362ca396 | 2046 | if (link.link_autoneg) { |
2047 | *current |= NETDEV_F_AUTONEG; | |
2048 | } | |
2049 | ||
ca3d4f55 BX |
2050 | *advertised = *supported = *peer = 0; |
2051 | ||
8a9562d2 PS |
2052 | return 0; |
2053 | } | |
2054 | ||
9509913a IS |
2055 | static struct ingress_policer * |
2056 | netdev_dpdk_policer_construct(uint32_t rate, uint32_t burst) | |
2057 | { | |
2058 | struct ingress_policer *policer = NULL; | |
2059 | uint64_t rate_bytes; | |
2060 | uint64_t burst_bytes; | |
2061 | int err = 0; | |
2062 | ||
2063 | policer = xmalloc(sizeof *policer); | |
2064 | rte_spinlock_init(&policer->policer_lock); | |
2065 | ||
2066 | /* rte_meter requires bytes so convert kbits rate and burst to bytes. */ | |
2067 | rate_bytes = rate * 1000/8; | |
2068 | burst_bytes = burst * 1000/8; | |
2069 | ||
2070 | policer->app_srtcm_params.cir = rate_bytes; | |
2071 | policer->app_srtcm_params.cbs = burst_bytes; | |
2072 | policer->app_srtcm_params.ebs = 0; | |
2073 | err = rte_meter_srtcm_config(&policer->in_policer, | |
2074 | &policer->app_srtcm_params); | |
58be5c0e | 2075 | if (err) { |
9509913a IS |
2076 | VLOG_ERR("Could not create rte meter for ingress policer"); |
2077 | return NULL; | |
2078 | } | |
2079 | ||
2080 | return policer; | |
2081 | } | |
2082 | ||
2083 | static int | |
2084 | netdev_dpdk_set_policing(struct netdev* netdev, uint32_t policer_rate, | |
2085 | uint32_t policer_burst) | |
2086 | { | |
2087 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
2088 | struct ingress_policer *policer; | |
2089 | ||
2090 | /* Force to 0 if no rate specified, | |
2091 | * default to 8000 kbits if burst is 0, | |
2092 | * else stick with user-specified value. | |
2093 | */ | |
2094 | policer_burst = (!policer_rate ? 0 | |
2095 | : !policer_burst ? 8000 | |
2096 | : policer_burst); | |
2097 | ||
2098 | ovs_mutex_lock(&dev->mutex); | |
2099 | ||
2100 | policer = ovsrcu_get_protected(struct ingress_policer *, | |
2101 | &dev->ingress_policer); | |
2102 | ||
2103 | if (dev->policer_rate == policer_rate && | |
2104 | dev->policer_burst == policer_burst) { | |
2105 | /* Assume that settings haven't changed since we last set them. */ | |
2106 | ovs_mutex_unlock(&dev->mutex); | |
2107 | return 0; | |
2108 | } | |
2109 | ||
2110 | /* Destroy any existing ingress policer for the device if one exists */ | |
2111 | if (policer) { | |
2112 | ovsrcu_postpone(free, policer); | |
2113 | } | |
2114 | ||
2115 | if (policer_rate != 0) { | |
2116 | policer = netdev_dpdk_policer_construct(policer_rate, policer_burst); | |
2117 | } else { | |
2118 | policer = NULL; | |
2119 | } | |
2120 | ovsrcu_set(&dev->ingress_policer, policer); | |
2121 | dev->policer_rate = policer_rate; | |
2122 | dev->policer_burst = policer_burst; | |
2123 | ovs_mutex_unlock(&dev->mutex); | |
2124 | ||
2125 | return 0; | |
2126 | } | |
2127 | ||
8a9562d2 PS |
2128 | static int |
2129 | netdev_dpdk_get_ifindex(const struct netdev *netdev) | |
2130 | { | |
2131 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
2132 | int ifindex; | |
2133 | ||
2134 | ovs_mutex_lock(&dev->mutex); | |
2135 | ifindex = dev->port_id; | |
2136 | ovs_mutex_unlock(&dev->mutex); | |
2137 | ||
2138 | return ifindex; | |
2139 | } | |
2140 | ||
2141 | static int | |
d46285a2 | 2142 | netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier) |
8a9562d2 | 2143 | { |
d46285a2 | 2144 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
2145 | |
2146 | ovs_mutex_lock(&dev->mutex); | |
2147 | check_link_status(dev); | |
2148 | *carrier = dev->link.link_status; | |
58397e6c KT |
2149 | |
2150 | ovs_mutex_unlock(&dev->mutex); | |
2151 | ||
2152 | return 0; | |
2153 | } | |
2154 | ||
2155 | static int | |
d46285a2 | 2156 | netdev_dpdk_vhost_get_carrier(const struct netdev *netdev, bool *carrier) |
58397e6c | 2157 | { |
d46285a2 | 2158 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
58397e6c KT |
2159 | |
2160 | ovs_mutex_lock(&dev->mutex); | |
2161 | ||
0a0f39df | 2162 | if (is_vhost_running(dev)) { |
58397e6c KT |
2163 | *carrier = 1; |
2164 | } else { | |
2165 | *carrier = 0; | |
2166 | } | |
2167 | ||
8a9562d2 PS |
2168 | ovs_mutex_unlock(&dev->mutex); |
2169 | ||
2170 | return 0; | |
2171 | } | |
2172 | ||
2173 | static long long int | |
d46285a2 | 2174 | netdev_dpdk_get_carrier_resets(const struct netdev *netdev) |
8a9562d2 | 2175 | { |
d46285a2 | 2176 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
2177 | long long int carrier_resets; |
2178 | ||
2179 | ovs_mutex_lock(&dev->mutex); | |
2180 | carrier_resets = dev->link_reset_cnt; | |
2181 | ovs_mutex_unlock(&dev->mutex); | |
2182 | ||
2183 | return carrier_resets; | |
2184 | } | |
2185 | ||
2186 | static int | |
d46285a2 | 2187 | netdev_dpdk_set_miimon(struct netdev *netdev OVS_UNUSED, |
8a9562d2 PS |
2188 | long long int interval OVS_UNUSED) |
2189 | { | |
ee32150e | 2190 | return EOPNOTSUPP; |
8a9562d2 PS |
2191 | } |
2192 | ||
2193 | static int | |
2194 | netdev_dpdk_update_flags__(struct netdev_dpdk *dev, | |
2195 | enum netdev_flags off, enum netdev_flags on, | |
64839cf4 WT |
2196 | enum netdev_flags *old_flagsp) |
2197 | OVS_REQUIRES(dev->mutex) | |
8a9562d2 PS |
2198 | { |
2199 | int err; | |
2200 | ||
2201 | if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) { | |
2202 | return EINVAL; | |
2203 | } | |
2204 | ||
2205 | *old_flagsp = dev->flags; | |
2206 | dev->flags |= on; | |
2207 | dev->flags &= ~off; | |
2208 | ||
2209 | if (dev->flags == *old_flagsp) { | |
2210 | return 0; | |
2211 | } | |
2212 | ||
58397e6c KT |
2213 | if (dev->type == DPDK_DEV_ETH) { |
2214 | if (dev->flags & NETDEV_UP) { | |
2215 | err = rte_eth_dev_start(dev->port_id); | |
2216 | if (err) | |
2217 | return -err; | |
2218 | } | |
8a9562d2 | 2219 | |
58397e6c KT |
2220 | if (dev->flags & NETDEV_PROMISC) { |
2221 | rte_eth_promiscuous_enable(dev->port_id); | |
2222 | } | |
8a9562d2 | 2223 | |
58397e6c KT |
2224 | if (!(dev->flags & NETDEV_UP)) { |
2225 | rte_eth_dev_stop(dev->port_id); | |
2226 | } | |
314fb5ad | 2227 | |
2228 | netdev_change_seq_changed(&dev->up); | |
e543851d ZB |
2229 | } else { |
2230 | /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is | |
2231 | * running then change netdev's change_seq to trigger link state | |
2232 | * update. */ | |
e543851d ZB |
2233 | |
2234 | if ((NETDEV_UP & ((*old_flagsp ^ on) | (*old_flagsp ^ off))) | |
0a0f39df | 2235 | && is_vhost_running(dev)) { |
e543851d ZB |
2236 | netdev_change_seq_changed(&dev->up); |
2237 | ||
2238 | /* Clear statistics if device is getting up. */ | |
2239 | if (NETDEV_UP & on) { | |
2240 | rte_spinlock_lock(&dev->stats_lock); | |
58be5c0e | 2241 | memset(&dev->stats, 0, sizeof dev->stats); |
e543851d ZB |
2242 | rte_spinlock_unlock(&dev->stats_lock); |
2243 | } | |
2244 | } | |
8a9562d2 PS |
2245 | } |
2246 | ||
2247 | return 0; | |
2248 | } | |
2249 | ||
2250 | static int | |
d46285a2 | 2251 | netdev_dpdk_update_flags(struct netdev *netdev, |
8a9562d2 PS |
2252 | enum netdev_flags off, enum netdev_flags on, |
2253 | enum netdev_flags *old_flagsp) | |
2254 | { | |
d46285a2 | 2255 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
2256 | int error; |
2257 | ||
d46285a2 DDP |
2258 | ovs_mutex_lock(&dev->mutex); |
2259 | error = netdev_dpdk_update_flags__(dev, off, on, old_flagsp); | |
2260 | ovs_mutex_unlock(&dev->mutex); | |
8a9562d2 PS |
2261 | |
2262 | return error; | |
2263 | } | |
2264 | ||
2265 | static int | |
d46285a2 | 2266 | netdev_dpdk_get_status(const struct netdev *netdev, struct smap *args) |
8a9562d2 | 2267 | { |
d46285a2 | 2268 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
8a9562d2 PS |
2269 | struct rte_eth_dev_info dev_info; |
2270 | ||
7cd1261d | 2271 | if (!rte_eth_dev_is_valid_port(dev->port_id)) { |
8a9562d2 | 2272 | return ENODEV; |
7cd1261d | 2273 | } |
8a9562d2 PS |
2274 | |
2275 | ovs_mutex_lock(&dev->mutex); | |
2276 | rte_eth_dev_info_get(dev->port_id, &dev_info); | |
2277 | ovs_mutex_unlock(&dev->mutex); | |
2278 | ||
95fb793a | 2279 | smap_add_format(args, "port_no", "%d", dev->port_id); |
58be5c0e MK |
2280 | smap_add_format(args, "numa_id", "%d", |
2281 | rte_eth_dev_socket_id(dev->port_id)); | |
8a9562d2 PS |
2282 | smap_add_format(args, "driver_name", "%s", dev_info.driver_name); |
2283 | smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize); | |
4be4d22c | 2284 | smap_add_format(args, "max_rx_pktlen", "%u", dev->max_packet_len); |
8a9562d2 PS |
2285 | smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues); |
2286 | smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues); | |
2287 | smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs); | |
58be5c0e MK |
2288 | smap_add_format(args, "max_hash_mac_addrs", "%u", |
2289 | dev_info.max_hash_mac_addrs); | |
8a9562d2 PS |
2290 | smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs); |
2291 | smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools); | |
2292 | ||
39c2baa9 | 2293 | if (dev_info.pci_dev) { |
2294 | smap_add_format(args, "pci-vendor_id", "0x%u", | |
2295 | dev_info.pci_dev->id.vendor_id); | |
2296 | smap_add_format(args, "pci-device_id", "0x%x", | |
2297 | dev_info.pci_dev->id.device_id); | |
2298 | } | |
8a9562d2 PS |
2299 | |
2300 | return 0; | |
2301 | } | |
2302 | ||
2303 | static void | |
2304 | netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state) | |
2305 | OVS_REQUIRES(dev->mutex) | |
2306 | { | |
2307 | enum netdev_flags old_flags; | |
2308 | ||
2309 | if (admin_state) { | |
2310 | netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags); | |
2311 | } else { | |
2312 | netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags); | |
2313 | } | |
2314 | } | |
2315 | ||
2316 | static void | |
2317 | netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc, | |
2318 | const char *argv[], void *aux OVS_UNUSED) | |
2319 | { | |
2320 | bool up; | |
2321 | ||
2322 | if (!strcasecmp(argv[argc - 1], "up")) { | |
2323 | up = true; | |
2324 | } else if ( !strcasecmp(argv[argc - 1], "down")) { | |
2325 | up = false; | |
2326 | } else { | |
2327 | unixctl_command_reply_error(conn, "Invalid Admin State"); | |
2328 | return; | |
2329 | } | |
2330 | ||
2331 | if (argc > 2) { | |
2332 | struct netdev *netdev = netdev_from_name(argv[1]); | |
2333 | if (netdev && is_dpdk_class(netdev->netdev_class)) { | |
2334 | struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev); | |
2335 | ||
2336 | ovs_mutex_lock(&dpdk_dev->mutex); | |
2337 | netdev_dpdk_set_admin_state__(dpdk_dev, up); | |
2338 | ovs_mutex_unlock(&dpdk_dev->mutex); | |
2339 | ||
2340 | netdev_close(netdev); | |
2341 | } else { | |
2342 | unixctl_command_reply_error(conn, "Not a DPDK Interface"); | |
2343 | netdev_close(netdev); | |
2344 | return; | |
2345 | } | |
2346 | } else { | |
2347 | struct netdev_dpdk *netdev; | |
2348 | ||
2349 | ovs_mutex_lock(&dpdk_mutex); | |
2350 | LIST_FOR_EACH (netdev, list_node, &dpdk_list) { | |
2351 | ovs_mutex_lock(&netdev->mutex); | |
2352 | netdev_dpdk_set_admin_state__(netdev, up); | |
2353 | ovs_mutex_unlock(&netdev->mutex); | |
2354 | } | |
2355 | ovs_mutex_unlock(&dpdk_mutex); | |
2356 | } | |
2357 | unixctl_command_reply(conn, "OK"); | |
2358 | } | |
2359 | ||
b8374d0d MV |
2360 | static void |
2361 | netdev_dpdk_attach(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
2362 | const char *argv[], void *aux OVS_UNUSED) | |
2363 | { | |
2364 | int ret; | |
2365 | char *response; | |
2366 | uint8_t port_id; | |
2367 | ||
2368 | ovs_mutex_lock(&dpdk_mutex); | |
2369 | ||
2370 | ret = rte_eth_dev_attach(argv[1], &port_id); | |
2371 | if (ret < 0) { | |
2372 | response = xasprintf("Error attaching device '%s'", argv[1]); | |
2373 | ovs_mutex_unlock(&dpdk_mutex); | |
2374 | unixctl_command_reply_error(conn, response); | |
2375 | free(response); | |
2376 | return; | |
2377 | } | |
2378 | ||
2379 | response = xasprintf("Device '%s' has been attached as 'dpdk%d'", | |
2380 | argv[1], port_id); | |
2381 | ||
2382 | ovs_mutex_unlock(&dpdk_mutex); | |
2383 | unixctl_command_reply(conn, response); | |
2384 | free(response); | |
2385 | } | |
2386 | ||
2387 | static void | |
2388 | netdev_dpdk_detach(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
2389 | const char *argv[], void *aux OVS_UNUSED) | |
2390 | { | |
2391 | int ret; | |
2392 | char *response; | |
2393 | unsigned int parsed_port; | |
2394 | uint8_t port_id; | |
2395 | char devname[RTE_ETH_NAME_MAX_LEN]; | |
2396 | ||
2397 | ovs_mutex_lock(&dpdk_mutex); | |
2398 | ||
2399 | ret = dpdk_dev_parse_name(argv[1], "dpdk", &parsed_port); | |
2400 | if (ret) { | |
2401 | response = xasprintf("'%s' is not a valid port", argv[1]); | |
2402 | goto error; | |
2403 | } | |
2404 | ||
2405 | port_id = parsed_port; | |
2406 | ||
2407 | struct netdev *netdev = netdev_from_name(argv[1]); | |
2408 | if (netdev) { | |
2409 | netdev_close(netdev); | |
2410 | response = xasprintf("Port '%s' is being used. Remove it before" | |
2411 | "detaching", argv[1]); | |
2412 | goto error; | |
2413 | } | |
2414 | ||
2415 | rte_eth_dev_close(port_id); | |
2416 | ||
2417 | ret = rte_eth_dev_detach(port_id, devname); | |
2418 | if (ret < 0) { | |
2419 | response = xasprintf("Port '%s' can not be detached", argv[1]); | |
2420 | goto error; | |
2421 | } | |
2422 | ||
2423 | response = xasprintf("Port '%s' has been detached", argv[1]); | |
2424 | ||
2425 | ovs_mutex_unlock(&dpdk_mutex); | |
2426 | unixctl_command_reply(conn, response); | |
2427 | free(response); | |
2428 | return; | |
2429 | ||
2430 | error: | |
2431 | ovs_mutex_unlock(&dpdk_mutex); | |
2432 | unixctl_command_reply_error(conn, response); | |
2433 | free(response); | |
2434 | } | |
2435 | ||
58397e6c KT |
2436 | /* |
2437 | * Set virtqueue flags so that we do not receive interrupts. | |
2438 | */ | |
2439 | static void | |
0a0f39df | 2440 | set_irq_status(int vid) |
58397e6c | 2441 | { |
4573fbd3 FL |
2442 | uint32_t i; |
2443 | uint64_t idx; | |
2444 | ||
0a0f39df | 2445 | for (i = 0; i < rte_vhost_get_queue_num(vid); i++) { |
4573fbd3 | 2446 | idx = i * VIRTIO_QNUM; |
0a0f39df CL |
2447 | rte_vhost_enable_guest_notification(vid, idx + VIRTIO_RXQ, 0); |
2448 | rte_vhost_enable_guest_notification(vid, idx + VIRTIO_TXQ, 0); | |
4573fbd3 FL |
2449 | } |
2450 | } | |
2451 | ||
585a5bea IM |
2452 | /* |
2453 | * Fixes mapping for vhost-user tx queues. Must be called after each | |
81acebda | 2454 | * enabling/disabling of queues and n_txq modifications. |
585a5bea IM |
2455 | */ |
2456 | static void | |
d46285a2 DDP |
2457 | netdev_dpdk_remap_txqs(struct netdev_dpdk *dev) |
2458 | OVS_REQUIRES(dev->mutex) | |
585a5bea IM |
2459 | { |
2460 | int *enabled_queues, n_enabled = 0; | |
81acebda | 2461 | int i, k, total_txqs = dev->up.n_txq; |
585a5bea | 2462 | |
eff23640 | 2463 | enabled_queues = xcalloc(total_txqs, sizeof *enabled_queues); |
585a5bea IM |
2464 | |
2465 | for (i = 0; i < total_txqs; i++) { | |
2466 | /* Enabled queues always mapped to themselves. */ | |
d46285a2 | 2467 | if (dev->tx_q[i].map == i) { |
585a5bea IM |
2468 | enabled_queues[n_enabled++] = i; |
2469 | } | |
2470 | } | |
2471 | ||
2472 | if (n_enabled == 0 && total_txqs != 0) { | |
f3ea2ad2 | 2473 | enabled_queues[0] = OVS_VHOST_QUEUE_DISABLED; |
585a5bea IM |
2474 | n_enabled = 1; |
2475 | } | |
2476 | ||
2477 | k = 0; | |
2478 | for (i = 0; i < total_txqs; i++) { | |
d46285a2 DDP |
2479 | if (dev->tx_q[i].map != i) { |
2480 | dev->tx_q[i].map = enabled_queues[k]; | |
585a5bea IM |
2481 | k = (k + 1) % n_enabled; |
2482 | } | |
2483 | } | |
2484 | ||
2d24d165 | 2485 | VLOG_DBG("TX queue mapping for %s\n", dev->vhost_id); |
585a5bea | 2486 | for (i = 0; i < total_txqs; i++) { |
d46285a2 | 2487 | VLOG_DBG("%2d --> %2d", i, dev->tx_q[i].map); |
585a5bea IM |
2488 | } |
2489 | ||
eff23640 | 2490 | free(enabled_queues); |
585a5bea | 2491 | } |
4573fbd3 | 2492 | |
58397e6c KT |
2493 | /* |
2494 | * A new virtio-net device is added to a vhost port. | |
2495 | */ | |
2496 | static int | |
0a0f39df | 2497 | new_device(int vid) |
58397e6c | 2498 | { |
d46285a2 | 2499 | struct netdev_dpdk *dev; |
58397e6c | 2500 | bool exists = false; |
db8f13b0 | 2501 | int newnode = 0; |
0a0f39df CL |
2502 | char ifname[IF_NAME_SZ]; |
2503 | ||
58be5c0e | 2504 | rte_vhost_get_ifname(vid, ifname, sizeof ifname); |
58397e6c KT |
2505 | |
2506 | ovs_mutex_lock(&dpdk_mutex); | |
2507 | /* Add device to the vhost port with the same name as that passed down. */ | |
d46285a2 | 2508 | LIST_FOR_EACH(dev, list_node, &dpdk_list) { |
c1ff66ac | 2509 | ovs_mutex_lock(&dev->mutex); |
2d24d165 | 2510 | if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) { |
0a0f39df | 2511 | uint32_t qp_num = rte_vhost_get_queue_num(vid); |
db8f13b0 CL |
2512 | |
2513 | /* Get NUMA information */ | |
0a0f39df CL |
2514 | newnode = rte_vhost_get_numa_node(vid); |
2515 | if (newnode == -1) { | |
5b9bf9e0 | 2516 | #ifdef VHOST_NUMA |
db8f13b0 | 2517 | VLOG_INFO("Error getting NUMA info for vHost Device '%s'", |
0a0f39df | 2518 | ifname); |
5b9bf9e0 | 2519 | #endif |
db8f13b0 | 2520 | newnode = dev->socket_id; |
db8f13b0 CL |
2521 | } |
2522 | ||
7f5f2bd0 IM |
2523 | if (dev->requested_n_txq != qp_num |
2524 | || dev->requested_n_rxq != qp_num | |
2525 | || dev->requested_socket_id != newnode) { | |
2526 | dev->requested_socket_id = newnode; | |
2527 | dev->requested_n_rxq = qp_num; | |
2528 | dev->requested_n_txq = qp_num; | |
2529 | netdev_request_reconfigure(&dev->up); | |
2530 | } else { | |
2531 | /* Reconfiguration not required. */ | |
2532 | dev->vhost_reconfigured = true; | |
2533 | } | |
81acebda | 2534 | |
0a0f39df | 2535 | ovsrcu_index_set(&dev->vid, vid); |
81acebda IM |
2536 | exists = true; |
2537 | ||
58397e6c | 2538 | /* Disable notifications. */ |
0a0f39df | 2539 | set_irq_status(vid); |
e543851d | 2540 | netdev_change_seq_changed(&dev->up); |
d46285a2 | 2541 | ovs_mutex_unlock(&dev->mutex); |
58397e6c KT |
2542 | break; |
2543 | } | |
c1ff66ac | 2544 | ovs_mutex_unlock(&dev->mutex); |
58397e6c KT |
2545 | } |
2546 | ovs_mutex_unlock(&dpdk_mutex); | |
2547 | ||
2548 | if (!exists) { | |
0a0f39df | 2549 | VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname); |
58397e6c KT |
2550 | |
2551 | return -1; | |
2552 | } | |
2553 | ||
0a0f39df CL |
2554 | VLOG_INFO("vHost Device '%s' has been added on numa node %i", |
2555 | ifname, newnode); | |
2556 | ||
58397e6c KT |
2557 | return 0; |
2558 | } | |
2559 | ||
f3ea2ad2 IM |
2560 | /* Clears mapping for all available queues of vhost interface. */ |
2561 | static void | |
2562 | netdev_dpdk_txq_map_clear(struct netdev_dpdk *dev) | |
2563 | OVS_REQUIRES(dev->mutex) | |
2564 | { | |
2565 | int i; | |
2566 | ||
81acebda | 2567 | for (i = 0; i < dev->up.n_txq; i++) { |
f3ea2ad2 IM |
2568 | dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN; |
2569 | } | |
2570 | } | |
2571 | ||
58397e6c KT |
2572 | /* |
2573 | * Remove a virtio-net device from the specific vhost port. Use dev->remove | |
2574 | * flag to stop any more packets from being sent or received to/from a VM and | |
2575 | * ensure all currently queued packets have been sent/received before removing | |
2576 | * the device. | |
2577 | */ | |
2578 | static void | |
0a0f39df | 2579 | destroy_device(int vid) |
58397e6c | 2580 | { |
d46285a2 | 2581 | struct netdev_dpdk *dev; |
afee281f | 2582 | bool exists = false; |
0a0f39df CL |
2583 | char ifname[IF_NAME_SZ]; |
2584 | ||
58be5c0e | 2585 | rte_vhost_get_ifname(vid, ifname, sizeof ifname); |
58397e6c KT |
2586 | |
2587 | ovs_mutex_lock(&dpdk_mutex); | |
d46285a2 | 2588 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { |
0a0f39df | 2589 | if (netdev_dpdk_get_vid(dev) == vid) { |
58397e6c | 2590 | |
d46285a2 | 2591 | ovs_mutex_lock(&dev->mutex); |
0a0f39df CL |
2592 | dev->vhost_reconfigured = false; |
2593 | ovsrcu_index_set(&dev->vid, -1); | |
d46285a2 | 2594 | netdev_dpdk_txq_map_clear(dev); |
81acebda | 2595 | |
e543851d | 2596 | netdev_change_seq_changed(&dev->up); |
d46285a2 | 2597 | ovs_mutex_unlock(&dev->mutex); |
81acebda | 2598 | exists = true; |
afee281f | 2599 | break; |
58397e6c KT |
2600 | } |
2601 | } | |
afee281f | 2602 | |
58397e6c KT |
2603 | ovs_mutex_unlock(&dpdk_mutex); |
2604 | ||
0a0f39df | 2605 | if (exists) { |
afee281f KT |
2606 | /* |
2607 | * Wait for other threads to quiesce after setting the 'virtio_dev' | |
2608 | * to NULL, before returning. | |
2609 | */ | |
2610 | ovsrcu_synchronize(); | |
2611 | /* | |
2612 | * As call to ovsrcu_synchronize() will end the quiescent state, | |
2613 | * put thread back into quiescent state before returning. | |
2614 | */ | |
2615 | ovsrcu_quiesce_start(); | |
0a0f39df | 2616 | VLOG_INFO("vHost Device '%s' has been removed", ifname); |
afee281f | 2617 | } else { |
0a0f39df | 2618 | VLOG_INFO("vHost Device '%s' not found", ifname); |
afee281f | 2619 | } |
58397e6c KT |
2620 | } |
2621 | ||
585a5bea | 2622 | static int |
0a0f39df | 2623 | vring_state_changed(int vid, uint16_t queue_id, int enable) |
585a5bea | 2624 | { |
d46285a2 | 2625 | struct netdev_dpdk *dev; |
585a5bea IM |
2626 | bool exists = false; |
2627 | int qid = queue_id / VIRTIO_QNUM; | |
0a0f39df CL |
2628 | char ifname[IF_NAME_SZ]; |
2629 | ||
58be5c0e | 2630 | rte_vhost_get_ifname(vid, ifname, sizeof ifname); |
585a5bea IM |
2631 | |
2632 | if (queue_id % VIRTIO_QNUM == VIRTIO_TXQ) { | |
2633 | return 0; | |
2634 | } | |
2635 | ||
2636 | ovs_mutex_lock(&dpdk_mutex); | |
d46285a2 | 2637 | LIST_FOR_EACH (dev, list_node, &dpdk_list) { |
c1ff66ac | 2638 | ovs_mutex_lock(&dev->mutex); |
2d24d165 | 2639 | if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) { |
585a5bea | 2640 | if (enable) { |
d46285a2 | 2641 | dev->tx_q[qid].map = qid; |
585a5bea | 2642 | } else { |
d46285a2 | 2643 | dev->tx_q[qid].map = OVS_VHOST_QUEUE_DISABLED; |
585a5bea | 2644 | } |
d46285a2 | 2645 | netdev_dpdk_remap_txqs(dev); |
585a5bea | 2646 | exists = true; |
d46285a2 | 2647 | ovs_mutex_unlock(&dev->mutex); |
585a5bea IM |
2648 | break; |
2649 | } | |
c1ff66ac | 2650 | ovs_mutex_unlock(&dev->mutex); |
585a5bea IM |
2651 | } |
2652 | ovs_mutex_unlock(&dpdk_mutex); | |
2653 | ||
2654 | if (exists) { | |
0a0f39df CL |
2655 | VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'" |
2656 | "changed to \'%s\'", queue_id, qid, ifname, | |
d46285a2 | 2657 | (enable == 1) ? "enabled" : "disabled"); |
585a5bea | 2658 | } else { |
0a0f39df | 2659 | VLOG_INFO("vHost Device '%s' not found", ifname); |
585a5bea IM |
2660 | return -1; |
2661 | } | |
2662 | ||
2663 | return 0; | |
2664 | } | |
2665 | ||
0a0f39df CL |
2666 | int |
2667 | netdev_dpdk_get_vid(const struct netdev_dpdk *dev) | |
58397e6c | 2668 | { |
0a0f39df | 2669 | return ovsrcu_index_get(&dev->vid); |
58397e6c KT |
2670 | } |
2671 | ||
9509913a IS |
2672 | struct ingress_policer * |
2673 | netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev) | |
2674 | { | |
2675 | return ovsrcu_get(struct ingress_policer *, &dev->ingress_policer); | |
2676 | } | |
2677 | ||
58397e6c KT |
2678 | /* |
2679 | * These callbacks allow virtio-net devices to be added to vhost ports when | |
2680 | * configuration has been fully complete. | |
2681 | */ | |
bce01e3a | 2682 | static const struct virtio_net_device_ops virtio_net_device_ops = |
58397e6c KT |
2683 | { |
2684 | .new_device = new_device, | |
2685 | .destroy_device = destroy_device, | |
585a5bea | 2686 | .vring_state_changed = vring_state_changed |
58397e6c KT |
2687 | }; |
2688 | ||
2689 | static void * | |
7d1ced01 | 2690 | start_vhost_loop(void *dummy OVS_UNUSED) |
58397e6c KT |
2691 | { |
2692 | pthread_detach(pthread_self()); | |
41987644 | 2693 | /* Put the vhost thread into quiescent state. */ |
618f44f7 | 2694 | ovsrcu_quiesce_start(); |
58397e6c KT |
2695 | rte_vhost_driver_session_start(); |
2696 | return NULL; | |
2697 | } | |
2698 | ||
2699 | static int | |
ecc1a34e | 2700 | netdev_dpdk_class_init(void) |
7d1ced01 | 2701 | { |
ecc1a34e DDP |
2702 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
2703 | ||
2704 | /* This function can be called for different classes. The initialization | |
2705 | * needs to be done only once */ | |
2706 | if (ovsthread_once_start(&once)) { | |
2707 | ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL); | |
2708 | unixctl_command_register("netdev-dpdk/set-admin-state", | |
2709 | "[netdev] up|down", 1, 2, | |
2710 | netdev_dpdk_set_admin_state, NULL); | |
b8374d0d MV |
2711 | unixctl_command_register("netdev-dpdk/attach", |
2712 | "pci address of device", 1, 1, | |
2713 | netdev_dpdk_attach, NULL); | |
2714 | unixctl_command_register("netdev-dpdk/detach", | |
2715 | "port", 1, 1, | |
2716 | netdev_dpdk_detach, NULL); | |
ecc1a34e DDP |
2717 | |
2718 | ovsthread_once_done(&once); | |
2719 | } | |
362ca396 | 2720 | |
7d1ced01 CL |
2721 | return 0; |
2722 | } | |
2723 | ||
ecc1a34e DDP |
2724 | static int |
2725 | netdev_dpdk_vhost_class_init(void) | |
033e9df2 | 2726 | { |
ecc1a34e | 2727 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
033e9df2 | 2728 | |
ecc1a34e DDP |
2729 | /* This function can be called for different classes. The initialization |
2730 | * needs to be done only once */ | |
2731 | if (ovsthread_once_start(&once)) { | |
2732 | rte_vhost_driver_callback_register(&virtio_net_device_ops); | |
2733 | rte_vhost_feature_disable(1ULL << VIRTIO_NET_F_HOST_TSO4 | |
2734 | | 1ULL << VIRTIO_NET_F_HOST_TSO6 | |
04de404e CL |
2735 | | 1ULL << VIRTIO_NET_F_CSUM |
2736 | | 1ULL << VIRTIO_RING_F_INDIRECT_DESC); | |
ecc1a34e DDP |
2737 | ovs_thread_create("vhost_thread", start_vhost_loop, NULL); |
2738 | ||
2739 | ovsthread_once_done(&once); | |
2740 | } | |
2741 | ||
2742 | return 0; | |
033e9df2 DDP |
2743 | } |
2744 | ||
95fb793a | 2745 | /* Client Rings */ |
2746 | ||
95fb793a | 2747 | static int |
2748 | dpdk_ring_create(const char dev_name[], unsigned int port_no, | |
2749 | unsigned int *eth_port_id) | |
2750 | { | |
48fffdee | 2751 | struct dpdk_ring *ring_pair; |
0c6f39e5 | 2752 | char *ring_name; |
b8374d0d | 2753 | int port_id; |
95fb793a | 2754 | |
48fffdee KT |
2755 | ring_pair = dpdk_rte_mzalloc(sizeof *ring_pair); |
2756 | if (!ring_pair) { | |
95fb793a | 2757 | return ENOMEM; |
2758 | } | |
2759 | ||
7251515e | 2760 | /* XXX: Add support for multiquque ring. */ |
0c6f39e5 | 2761 | ring_name = xasprintf("%s_tx", dev_name); |
95fb793a | 2762 | |
8f0a76c9 | 2763 | /* Create single producer tx ring, netdev does explicit locking. */ |
48fffdee | 2764 | ring_pair->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0, |
8f0a76c9 | 2765 | RING_F_SP_ENQ); |
0c6f39e5 | 2766 | free(ring_name); |
48fffdee KT |
2767 | if (ring_pair->cring_tx == NULL) { |
2768 | rte_free(ring_pair); | |
95fb793a | 2769 | return ENOMEM; |
2770 | } | |
2771 | ||
0c6f39e5 | 2772 | ring_name = xasprintf("%s_rx", dev_name); |
95fb793a | 2773 | |
8f0a76c9 | 2774 | /* Create single consumer rx ring, netdev does explicit locking. */ |
48fffdee | 2775 | ring_pair->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0, |
8f0a76c9 | 2776 | RING_F_SC_DEQ); |
0c6f39e5 | 2777 | free(ring_name); |
48fffdee KT |
2778 | if (ring_pair->cring_rx == NULL) { |
2779 | rte_free(ring_pair); | |
95fb793a | 2780 | return ENOMEM; |
2781 | } | |
2782 | ||
b8374d0d MV |
2783 | port_id = rte_eth_from_rings(dev_name, &ring_pair->cring_rx, 1, |
2784 | &ring_pair->cring_tx, 1, SOCKET0); | |
d7310583 | 2785 | |
b8374d0d | 2786 | if (port_id < 0) { |
48fffdee | 2787 | rte_free(ring_pair); |
95fb793a | 2788 | return ENODEV; |
2789 | } | |
2790 | ||
48fffdee | 2791 | ring_pair->user_port_id = port_no; |
b8374d0d MV |
2792 | ring_pair->eth_port_id = port_id; |
2793 | *eth_port_id = port_id; | |
2794 | ||
48fffdee | 2795 | ovs_list_push_back(&dpdk_ring_list, &ring_pair->list_node); |
95fb793a | 2796 | |
95fb793a | 2797 | return 0; |
2798 | } | |
2799 | ||
2800 | static int | |
64839cf4 WT |
2801 | dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) |
2802 | OVS_REQUIRES(dpdk_mutex) | |
95fb793a | 2803 | { |
48fffdee | 2804 | struct dpdk_ring *ring_pair; |
95fb793a | 2805 | unsigned int port_no; |
2806 | int err = 0; | |
2807 | ||
2808 | /* Names always start with "dpdkr" */ | |
2809 | err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no); | |
2810 | if (err) { | |
2811 | return err; | |
2812 | } | |
2813 | ||
58be5c0e | 2814 | /* Look through our list to find the device */ |
48fffdee KT |
2815 | LIST_FOR_EACH (ring_pair, list_node, &dpdk_ring_list) { |
2816 | if (ring_pair->user_port_id == port_no) { | |
58397e6c | 2817 | VLOG_INFO("Found dpdk ring device %s:", dev_name); |
58be5c0e | 2818 | /* Really all that is needed */ |
48fffdee | 2819 | *eth_port_id = ring_pair->eth_port_id; |
95fb793a | 2820 | return 0; |
2821 | } | |
2822 | } | |
2823 | /* Need to create the device rings */ | |
2824 | return dpdk_ring_create(dev_name, port_no, eth_port_id); | |
2825 | } | |
2826 | ||
7251515e | 2827 | static int |
d46285a2 | 2828 | netdev_dpdk_ring_send(struct netdev *netdev, int qid, |
324c8374 IM |
2829 | struct dp_packet_batch *batch, bool may_steal, |
2830 | bool concurrent_txq) | |
7251515e | 2831 | { |
d46285a2 | 2832 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
1b99bb05 MG |
2833 | unsigned i; |
2834 | ||
58be5c0e MK |
2835 | /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that |
2836 | * the rss hash field is clear. This is because the same mbuf may be | |
2837 | * modified by the consumer of the ring and return into the datapath | |
2838 | * without recalculating the RSS hash. */ | |
64839cf4 WT |
2839 | for (i = 0; i < batch->count; i++) { |
2840 | dp_packet_rss_invalidate(batch->packets[i]); | |
1b99bb05 | 2841 | } |
7251515e | 2842 | |
324c8374 | 2843 | netdev_dpdk_send__(dev, qid, batch, may_steal, concurrent_txq); |
7251515e DV |
2844 | return 0; |
2845 | } | |
2846 | ||
95fb793a | 2847 | static int |
2848 | netdev_dpdk_ring_construct(struct netdev *netdev) | |
2849 | { | |
2850 | unsigned int port_no = 0; | |
2851 | int err = 0; | |
2852 | ||
95fb793a | 2853 | ovs_mutex_lock(&dpdk_mutex); |
2854 | ||
2855 | err = dpdk_ring_open(netdev->name, &port_no); | |
2856 | if (err) { | |
2857 | goto unlock_dpdk; | |
2858 | } | |
2859 | ||
58397e6c | 2860 | err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH); |
95fb793a | 2861 | |
2862 | unlock_dpdk: | |
2863 | ovs_mutex_unlock(&dpdk_mutex); | |
2864 | return err; | |
2865 | } | |
2866 | ||
0bf765f7 IS |
2867 | /* QoS Functions */ |
2868 | ||
2869 | /* | |
2870 | * Initialize QoS configuration operations. | |
2871 | */ | |
2872 | static void | |
2873 | qos_conf_init(struct qos_conf *conf, const struct dpdk_qos_ops *ops) | |
2874 | { | |
2875 | conf->ops = ops; | |
78bd47cf | 2876 | rte_spinlock_init(&conf->lock); |
0bf765f7 IS |
2877 | } |
2878 | ||
2879 | /* | |
2880 | * Search existing QoS operations in qos_ops and compare each set of | |
2881 | * operations qos_name to name. Return a dpdk_qos_ops pointer to a match, | |
2882 | * else return NULL | |
2883 | */ | |
2884 | static const struct dpdk_qos_ops * | |
2885 | qos_lookup_name(const char *name) | |
2886 | { | |
2887 | const struct dpdk_qos_ops *const *opsp; | |
2888 | ||
2889 | for (opsp = qos_confs; *opsp != NULL; opsp++) { | |
2890 | const struct dpdk_qos_ops *ops = *opsp; | |
2891 | if (!strcmp(name, ops->qos_name)) { | |
2892 | return ops; | |
2893 | } | |
2894 | } | |
2895 | return NULL; | |
2896 | } | |
2897 | ||
0bf765f7 IS |
2898 | static int |
2899 | netdev_dpdk_get_qos_types(const struct netdev *netdev OVS_UNUSED, | |
2900 | struct sset *types) | |
2901 | { | |
2902 | const struct dpdk_qos_ops *const *opsp; | |
2903 | ||
2904 | for (opsp = qos_confs; *opsp != NULL; opsp++) { | |
2905 | const struct dpdk_qos_ops *ops = *opsp; | |
2906 | if (ops->qos_construct && ops->qos_name[0] != '\0') { | |
2907 | sset_add(types, ops->qos_name); | |
2908 | } | |
2909 | } | |
2910 | return 0; | |
2911 | } | |
2912 | ||
2913 | static int | |
d46285a2 | 2914 | netdev_dpdk_get_qos(const struct netdev *netdev, |
0bf765f7 IS |
2915 | const char **typep, struct smap *details) |
2916 | { | |
d46285a2 | 2917 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
78bd47cf | 2918 | struct qos_conf *qos_conf; |
0bf765f7 IS |
2919 | int error = 0; |
2920 | ||
d46285a2 | 2921 | ovs_mutex_lock(&dev->mutex); |
78bd47cf DDP |
2922 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); |
2923 | if (qos_conf) { | |
2924 | *typep = qos_conf->ops->qos_name; | |
2925 | error = (qos_conf->ops->qos_get | |
2926 | ? qos_conf->ops->qos_get(qos_conf, details): 0); | |
d03603c4 MC |
2927 | } else { |
2928 | /* No QoS configuration set, return an empty string */ | |
2929 | *typep = ""; | |
0bf765f7 | 2930 | } |
d46285a2 | 2931 | ovs_mutex_unlock(&dev->mutex); |
0bf765f7 IS |
2932 | |
2933 | return error; | |
2934 | } | |
2935 | ||
2936 | static int | |
78bd47cf DDP |
2937 | netdev_dpdk_set_qos(struct netdev *netdev, const char *type, |
2938 | const struct smap *details) | |
0bf765f7 | 2939 | { |
d46285a2 | 2940 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); |
0bf765f7 | 2941 | const struct dpdk_qos_ops *new_ops = NULL; |
78bd47cf | 2942 | struct qos_conf *qos_conf, *new_qos_conf = NULL; |
0bf765f7 IS |
2943 | int error = 0; |
2944 | ||
d46285a2 | 2945 | ovs_mutex_lock(&dev->mutex); |
0bf765f7 | 2946 | |
78bd47cf | 2947 | qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf); |
0bf765f7 | 2948 | |
78bd47cf DDP |
2949 | new_ops = qos_lookup_name(type); |
2950 | ||
2951 | if (!new_ops || !new_ops->qos_construct) { | |
2952 | new_qos_conf = NULL; | |
2953 | if (type && type[0]) { | |
2954 | error = EOPNOTSUPP; | |
0bf765f7 | 2955 | } |
44975bb0 | 2956 | } else if (qos_conf && qos_conf->ops == new_ops |
78bd47cf DDP |
2957 | && qos_conf->ops->qos_is_equal(qos_conf, details)) { |
2958 | new_qos_conf = qos_conf; | |
0bf765f7 | 2959 | } else { |
78bd47cf | 2960 | error = new_ops->qos_construct(details, &new_qos_conf); |
7ea266e9 IS |
2961 | } |
2962 | ||
7ea266e9 | 2963 | if (error) { |
78bd47cf DDP |
2964 | VLOG_ERR("Failed to set QoS type %s on port %s: %s", |
2965 | type, netdev->name, rte_strerror(error)); | |
2966 | } | |
2967 | ||
2968 | if (new_qos_conf != qos_conf) { | |
2969 | ovsrcu_set(&dev->qos_conf, new_qos_conf); | |
2970 | if (qos_conf) { | |
2971 | ovsrcu_postpone(qos_conf->ops->qos_destruct, qos_conf); | |
2972 | } | |
0bf765f7 IS |
2973 | } |
2974 | ||
d46285a2 | 2975 | ovs_mutex_unlock(&dev->mutex); |
78bd47cf | 2976 | |
0bf765f7 IS |
2977 | return error; |
2978 | } | |
2979 | ||
2980 | /* egress-policer details */ | |
2981 | ||
2982 | struct egress_policer { | |
2983 | struct qos_conf qos_conf; | |
2984 | struct rte_meter_srtcm_params app_srtcm_params; | |
2985 | struct rte_meter_srtcm egress_meter; | |
2986 | }; | |
2987 | ||
78bd47cf DDP |
2988 | static void |
2989 | egress_policer_details_to_param(const struct smap *details, | |
2990 | struct rte_meter_srtcm_params *params) | |
0bf765f7 | 2991 | { |
78bd47cf DDP |
2992 | memset(params, 0, sizeof *params); |
2993 | params->cir = smap_get_ullong(details, "cir", 0); | |
2994 | params->cbs = smap_get_ullong(details, "cbs", 0); | |
2995 | params->ebs = 0; | |
0bf765f7 IS |
2996 | } |
2997 | ||
2998 | static int | |
78bd47cf DDP |
2999 | egress_policer_qos_construct(const struct smap *details, |
3000 | struct qos_conf **conf) | |
0bf765f7 | 3001 | { |
0bf765f7 | 3002 | struct egress_policer *policer; |
0bf765f7 IS |
3003 | int err = 0; |
3004 | ||
0bf765f7 IS |
3005 | policer = xmalloc(sizeof *policer); |
3006 | qos_conf_init(&policer->qos_conf, &egress_policer_ops); | |
78bd47cf | 3007 | egress_policer_details_to_param(details, &policer->app_srtcm_params); |
0bf765f7 | 3008 | err = rte_meter_srtcm_config(&policer->egress_meter, |
78bd47cf DDP |
3009 | &policer->app_srtcm_params); |
3010 | if (!err) { | |
3011 | *conf = &policer->qos_conf; | |
3012 | } else { | |
7ea266e9 | 3013 | free(policer); |
78bd47cf | 3014 | *conf = NULL; |
7ea266e9 IS |
3015 | err = -err; |
3016 | } | |
0bf765f7 IS |
3017 | |
3018 | return err; | |
3019 | } | |
3020 | ||
3021 | static void | |
78bd47cf | 3022 | egress_policer_qos_destruct(struct qos_conf *conf) |
0bf765f7 IS |
3023 | { |
3024 | struct egress_policer *policer = CONTAINER_OF(conf, struct egress_policer, | |
78bd47cf | 3025 | qos_conf); |
0bf765f7 IS |
3026 | free(policer); |
3027 | } | |
3028 | ||
3029 | static int | |
78bd47cf | 3030 | egress_policer_qos_get(const struct qos_conf *conf, struct smap *details) |
0bf765f7 | 3031 | { |
78bd47cf DDP |
3032 | struct egress_policer *policer = |
3033 | CONTAINER_OF(conf, struct egress_policer, qos_conf); | |
3034 | ||
3035 | smap_add_format(details, "cir", "%"PRIu64, policer->app_srtcm_params.cir); | |
3036 | smap_add_format(details, "cbs", "%"PRIu64, policer->app_srtcm_params.cbs); | |
050c60bf | 3037 | |
0bf765f7 IS |
3038 | return 0; |
3039 | } | |
3040 | ||
78bd47cf DDP |
3041 | static bool |
3042 | egress_policer_qos_is_equal(const struct qos_conf *conf, const struct smap *details) | |
0bf765f7 | 3043 | { |
78bd47cf DDP |
3044 | struct egress_policer *policer = |
3045 | CONTAINER_OF(conf, struct egress_policer, qos_conf); | |
3046 | struct rte_meter_srtcm_params params; | |
0bf765f7 | 3047 | |
78bd47cf | 3048 | egress_policer_details_to_param(details, ¶ms); |
7ea266e9 | 3049 | |
78bd47cf | 3050 | return !memcmp(¶ms, &policer->app_srtcm_params, sizeof params); |
0bf765f7 IS |
3051 | } |
3052 | ||
0bf765f7 | 3053 | static int |
78bd47cf | 3054 | egress_policer_run(struct qos_conf *conf, struct rte_mbuf **pkts, int pkt_cnt) |
0bf765f7 | 3055 | { |
0bf765f7 | 3056 | int cnt = 0; |
78bd47cf DDP |
3057 | struct egress_policer *policer = |
3058 | CONTAINER_OF(conf, struct egress_policer, qos_conf); | |
0bf765f7 | 3059 | |
f3926f29 | 3060 | cnt = netdev_dpdk_policer_run(&policer->egress_meter, pkts, pkt_cnt); |
0bf765f7 IS |
3061 | |
3062 | return cnt; | |
3063 | } | |
3064 | ||
3065 | static const struct dpdk_qos_ops egress_policer_ops = { | |
3066 | "egress-policer", /* qos_name */ | |
3067 | egress_policer_qos_construct, | |
3068 | egress_policer_qos_destruct, | |
3069 | egress_policer_qos_get, | |
78bd47cf | 3070 | egress_policer_qos_is_equal, |
0bf765f7 IS |
3071 | egress_policer_run |
3072 | }; | |
3073 | ||
050c60bf DDP |
3074 | static int |
3075 | netdev_dpdk_reconfigure(struct netdev *netdev) | |
3076 | { | |
3077 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3078 | int err = 0; | |
3079 | ||
050c60bf DDP |
3080 | ovs_mutex_lock(&dev->mutex); |
3081 | ||
3082 | if (netdev->n_txq == dev->requested_n_txq | |
0072e931 | 3083 | && netdev->n_rxq == dev->requested_n_rxq |
b685696b CL |
3084 | && dev->mtu == dev->requested_mtu |
3085 | && dev->rxq_size == dev->requested_rxq_size | |
3086 | && dev->txq_size == dev->requested_txq_size) { | |
050c60bf DDP |
3087 | /* Reconfiguration is unnecessary */ |
3088 | ||
3089 | goto out; | |
3090 | } | |
3091 | ||
3092 | rte_eth_dev_stop(dev->port_id); | |
3093 | ||
0072e931 MK |
3094 | if (dev->mtu != dev->requested_mtu) { |
3095 | netdev_dpdk_mempool_configure(dev); | |
3096 | } | |
3097 | ||
050c60bf DDP |
3098 | netdev->n_txq = dev->requested_n_txq; |
3099 | netdev->n_rxq = dev->requested_n_rxq; | |
3100 | ||
b685696b CL |
3101 | dev->rxq_size = dev->requested_rxq_size; |
3102 | dev->txq_size = dev->requested_txq_size; | |
3103 | ||
050c60bf DDP |
3104 | rte_free(dev->tx_q); |
3105 | err = dpdk_eth_dev_init(dev); | |
eff23640 DDP |
3106 | dev->tx_q = netdev_dpdk_alloc_txq(netdev->n_txq); |
3107 | if (!dev->tx_q) { | |
3108 | err = ENOMEM; | |
3109 | } | |
050c60bf | 3110 | |
0072e931 MK |
3111 | netdev_change_seq_changed(netdev); |
3112 | ||
050c60bf | 3113 | out: |
050c60bf | 3114 | ovs_mutex_unlock(&dev->mutex); |
050c60bf DDP |
3115 | return err; |
3116 | } | |
3117 | ||
2d24d165 CL |
3118 | static void |
3119 | dpdk_vhost_reconfigure_helper(struct netdev_dpdk *dev) | |
2d24d165 | 3120 | OVS_REQUIRES(dev->mutex) |
050c60bf | 3121 | { |
2d24d165 CL |
3122 | dev->up.n_txq = dev->requested_n_txq; |
3123 | dev->up.n_rxq = dev->requested_n_rxq; | |
050c60bf | 3124 | |
81acebda IM |
3125 | /* Enable TX queue 0 by default if it wasn't disabled. */ |
3126 | if (dev->tx_q[0].map == OVS_VHOST_QUEUE_MAP_UNKNOWN) { | |
3127 | dev->tx_q[0].map = 0; | |
3128 | } | |
3129 | ||
3130 | netdev_dpdk_remap_txqs(dev); | |
3131 | ||
0072e931 MK |
3132 | if (dev->requested_socket_id != dev->socket_id |
3133 | || dev->requested_mtu != dev->mtu) { | |
3134 | if (!netdev_dpdk_mempool_configure(dev)) { | |
2d24d165 | 3135 | netdev_change_seq_changed(&dev->up); |
db8f13b0 CL |
3136 | } |
3137 | } | |
3138 | ||
0a0f39df CL |
3139 | if (netdev_dpdk_get_vid(dev) >= 0) { |
3140 | dev->vhost_reconfigured = true; | |
81acebda | 3141 | } |
2d24d165 CL |
3142 | } |
3143 | ||
3144 | static int | |
3145 | netdev_dpdk_vhost_reconfigure(struct netdev *netdev) | |
3146 | { | |
3147 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3148 | ||
2d24d165 | 3149 | ovs_mutex_lock(&dev->mutex); |
2d24d165 | 3150 | dpdk_vhost_reconfigure_helper(dev); |
2d24d165 | 3151 | ovs_mutex_unlock(&dev->mutex); |
2d24d165 CL |
3152 | return 0; |
3153 | } | |
3154 | ||
3155 | static int | |
3156 | netdev_dpdk_vhost_client_reconfigure(struct netdev *netdev) | |
3157 | { | |
3158 | struct netdev_dpdk *dev = netdev_dpdk_cast(netdev); | |
3159 | int err = 0; | |
3160 | ||
2d24d165 CL |
3161 | ovs_mutex_lock(&dev->mutex); |
3162 | ||
3163 | dpdk_vhost_reconfigure_helper(dev); | |
81acebda | 3164 | |
c1ff66ac CL |
3165 | /* Configure vHost client mode if requested and if the following criteria |
3166 | * are met: | |
2d24d165 CL |
3167 | * 1. Device hasn't been registered yet. |
3168 | * 2. A path has been specified. | |
c1ff66ac CL |
3169 | */ |
3170 | if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT) | |
2d24d165 CL |
3171 | && strlen(dev->vhost_id)) { |
3172 | /* Register client-mode device */ | |
3173 | err = rte_vhost_driver_register(dev->vhost_id, | |
3174 | RTE_VHOST_USER_CLIENT); | |
c1ff66ac | 3175 | if (err) { |
2d24d165 CL |
3176 | VLOG_ERR("vhost-user device setup failure for device %s\n", |
3177 | dev->vhost_id); | |
c1ff66ac | 3178 | } else { |
2d24d165 CL |
3179 | /* Configuration successful */ |
3180 | dev->vhost_driver_flags |= RTE_VHOST_USER_CLIENT; | |
3181 | VLOG_INFO("vHost User device '%s' created in 'client' mode, " | |
3182 | "using client socket '%s'", | |
3183 | dev->up.name, dev->vhost_id); | |
c1ff66ac CL |
3184 | } |
3185 | } | |
3186 | ||
050c60bf | 3187 | ovs_mutex_unlock(&dev->mutex); |
050c60bf | 3188 | |
0072e931 | 3189 | return 0; |
050c60bf DDP |
3190 | } |
3191 | ||
ecc1a34e | 3192 | #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \ |
81acebda IM |
3193 | SET_CONFIG, SET_TX_MULTIQ, SEND, \ |
3194 | GET_CARRIER, GET_STATS, \ | |
3195 | GET_FEATURES, GET_STATUS, \ | |
3196 | RECONFIGURE, RXQ_RECV) \ | |
95fb793a | 3197 | { \ |
3198 | NAME, \ | |
118c77b1 | 3199 | true, /* is_pmd */ \ |
ecc1a34e | 3200 | INIT, /* init */ \ |
95fb793a | 3201 | NULL, /* netdev_dpdk_run */ \ |
3202 | NULL, /* netdev_dpdk_wait */ \ | |
3203 | \ | |
3204 | netdev_dpdk_alloc, \ | |
3205 | CONSTRUCT, \ | |
58397e6c | 3206 | DESTRUCT, \ |
95fb793a | 3207 | netdev_dpdk_dealloc, \ |
3208 | netdev_dpdk_get_config, \ | |
81acebda | 3209 | SET_CONFIG, \ |
95fb793a | 3210 | NULL, /* get_tunnel_config */ \ |
58397e6c KT |
3211 | NULL, /* build header */ \ |
3212 | NULL, /* push header */ \ | |
3213 | NULL, /* pop header */ \ | |
7dec44fe | 3214 | netdev_dpdk_get_numa_id, /* get_numa_id */ \ |
81acebda | 3215 | SET_TX_MULTIQ, \ |
95fb793a | 3216 | \ |
7251515e | 3217 | SEND, /* send */ \ |
95fb793a | 3218 | NULL, /* send_wait */ \ |
3219 | \ | |
3220 | netdev_dpdk_set_etheraddr, \ | |
3221 | netdev_dpdk_get_etheraddr, \ | |
3222 | netdev_dpdk_get_mtu, \ | |
0072e931 | 3223 | netdev_dpdk_set_mtu, \ |
95fb793a | 3224 | netdev_dpdk_get_ifindex, \ |
58397e6c | 3225 | GET_CARRIER, \ |
95fb793a | 3226 | netdev_dpdk_get_carrier_resets, \ |
3227 | netdev_dpdk_set_miimon, \ | |
58397e6c KT |
3228 | GET_STATS, \ |
3229 | GET_FEATURES, \ | |
95fb793a | 3230 | NULL, /* set_advertisements */ \ |
3231 | \ | |
9509913a | 3232 | netdev_dpdk_set_policing, \ |
0bf765f7 | 3233 | netdev_dpdk_get_qos_types, \ |
95fb793a | 3234 | NULL, /* get_qos_capabilities */ \ |
0bf765f7 IS |
3235 | netdev_dpdk_get_qos, \ |
3236 | netdev_dpdk_set_qos, \ | |
95fb793a | 3237 | NULL, /* get_queue */ \ |
3238 | NULL, /* set_queue */ \ | |
3239 | NULL, /* delete_queue */ \ | |
3240 | NULL, /* get_queue_stats */ \ | |
3241 | NULL, /* queue_dump_start */ \ | |
3242 | NULL, /* queue_dump_next */ \ | |
3243 | NULL, /* queue_dump_done */ \ | |
3244 | NULL, /* dump_queue_stats */ \ | |
3245 | \ | |
95fb793a | 3246 | NULL, /* set_in4 */ \ |
a8704b50 | 3247 | NULL, /* get_addr_list */ \ |
95fb793a | 3248 | NULL, /* add_router */ \ |
3249 | NULL, /* get_next_hop */ \ | |
58397e6c | 3250 | GET_STATUS, \ |
95fb793a | 3251 | NULL, /* arp_lookup */ \ |
3252 | \ | |
3253 | netdev_dpdk_update_flags, \ | |
050c60bf | 3254 | RECONFIGURE, \ |
95fb793a | 3255 | \ |
3256 | netdev_dpdk_rxq_alloc, \ | |
3257 | netdev_dpdk_rxq_construct, \ | |
3258 | netdev_dpdk_rxq_destruct, \ | |
3259 | netdev_dpdk_rxq_dealloc, \ | |
58397e6c | 3260 | RXQ_RECV, \ |
95fb793a | 3261 | NULL, /* rx_wait */ \ |
3262 | NULL, /* rxq_drain */ \ | |
3263 | } | |
8a9562d2 | 3264 | |
bce01e3a | 3265 | static const struct netdev_class dpdk_class = |
95fb793a | 3266 | NETDEV_DPDK_CLASS( |
3267 | "dpdk", | |
ecc1a34e | 3268 | netdev_dpdk_class_init, |
5496878c | 3269 | netdev_dpdk_construct, |
58397e6c | 3270 | netdev_dpdk_destruct, |
81acebda IM |
3271 | netdev_dpdk_set_config, |
3272 | netdev_dpdk_set_tx_multiq, | |
58397e6c KT |
3273 | netdev_dpdk_eth_send, |
3274 | netdev_dpdk_get_carrier, | |
3275 | netdev_dpdk_get_stats, | |
3276 | netdev_dpdk_get_features, | |
3277 | netdev_dpdk_get_status, | |
050c60bf | 3278 | netdev_dpdk_reconfigure, |
58397e6c | 3279 | netdev_dpdk_rxq_recv); |
95fb793a | 3280 | |
bce01e3a | 3281 | static const struct netdev_class dpdk_ring_class = |
95fb793a | 3282 | NETDEV_DPDK_CLASS( |
3283 | "dpdkr", | |
ecc1a34e | 3284 | netdev_dpdk_class_init, |
5496878c | 3285 | netdev_dpdk_ring_construct, |
58397e6c | 3286 | netdev_dpdk_destruct, |
c3d062a7 | 3287 | netdev_dpdk_ring_set_config, |
81acebda | 3288 | netdev_dpdk_set_tx_multiq, |
58397e6c KT |
3289 | netdev_dpdk_ring_send, |
3290 | netdev_dpdk_get_carrier, | |
3291 | netdev_dpdk_get_stats, | |
3292 | netdev_dpdk_get_features, | |
3293 | netdev_dpdk_get_status, | |
050c60bf | 3294 | netdev_dpdk_reconfigure, |
58397e6c KT |
3295 | netdev_dpdk_rxq_recv); |
3296 | ||
53f50d24 | 3297 | static const struct netdev_class dpdk_vhost_class = |
7d1ced01 CL |
3298 | NETDEV_DPDK_CLASS( |
3299 | "dpdkvhostuser", | |
ecc1a34e | 3300 | netdev_dpdk_vhost_class_init, |
53f50d24 | 3301 | netdev_dpdk_vhost_construct, |
58397e6c | 3302 | netdev_dpdk_vhost_destruct, |
2d24d165 | 3303 | NULL, |
81acebda | 3304 | NULL, |
58397e6c KT |
3305 | netdev_dpdk_vhost_send, |
3306 | netdev_dpdk_vhost_get_carrier, | |
3307 | netdev_dpdk_vhost_get_stats, | |
3308 | NULL, | |
7251515e | 3309 | NULL, |
53f50d24 | 3310 | netdev_dpdk_vhost_reconfigure, |
58397e6c | 3311 | netdev_dpdk_vhost_rxq_recv); |
2d24d165 CL |
3312 | static const struct netdev_class dpdk_vhost_client_class = |
3313 | NETDEV_DPDK_CLASS( | |
3314 | "dpdkvhostuserclient", | |
ecc1a34e | 3315 | netdev_dpdk_vhost_class_init, |
2d24d165 CL |
3316 | netdev_dpdk_vhost_client_construct, |
3317 | netdev_dpdk_vhost_destruct, | |
3318 | netdev_dpdk_vhost_client_set_config, | |
3319 | NULL, | |
3320 | netdev_dpdk_vhost_send, | |
3321 | netdev_dpdk_vhost_get_carrier, | |
3322 | netdev_dpdk_vhost_get_stats, | |
3323 | NULL, | |
3324 | NULL, | |
3325 | netdev_dpdk_vhost_client_reconfigure, | |
3326 | netdev_dpdk_vhost_rxq_recv); | |
95fb793a | 3327 | |
8a9562d2 PS |
3328 | void |
3329 | netdev_dpdk_register(void) | |
3330 | { | |
bab69409 AC |
3331 | netdev_register_provider(&dpdk_class); |
3332 | netdev_register_provider(&dpdk_ring_class); | |
53f50d24 | 3333 | netdev_register_provider(&dpdk_vhost_class); |
2d24d165 | 3334 | netdev_register_provider(&dpdk_vhost_client_class); |
8a9562d2 | 3335 | } |