]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/drivers/net/kni/rte_eth_kni.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / dpdk / drivers / net / kni / rte_eth_kni.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
3 */
4
5 #include <fcntl.h>
6 #include <pthread.h>
7 #include <unistd.h>
8
9 #include <rte_ethdev_driver.h>
10 #include <rte_ethdev_vdev.h>
11 #include <rte_kni.h>
12 #include <rte_kvargs.h>
13 #include <rte_malloc.h>
14 #include <rte_bus_vdev.h>
15
16 /* Only single queue supported */
17 #define KNI_MAX_QUEUE_PER_PORT 1
18
19 #define MAX_PACKET_SZ 2048
20 #define MAX_KNI_PORTS 8
21
22 #define ETH_KNI_NO_REQUEST_THREAD_ARG "no_request_thread"
23 static const char * const valid_arguments[] = {
24 ETH_KNI_NO_REQUEST_THREAD_ARG,
25 NULL
26 };
27
28 struct eth_kni_args {
29 int no_request_thread;
30 };
31
32 struct pmd_queue_stats {
33 uint64_t pkts;
34 uint64_t bytes;
35 uint64_t err_pkts;
36 };
37
38 struct pmd_queue {
39 struct pmd_internals *internals;
40 struct rte_mempool *mb_pool;
41
42 struct pmd_queue_stats rx;
43 struct pmd_queue_stats tx;
44 };
45
46 struct pmd_internals {
47 struct rte_kni *kni;
48 int is_kni_started;
49
50 pthread_t thread;
51 int stop_thread;
52 int no_request_thread;
53
54 struct ether_addr eth_addr;
55
56 struct pmd_queue rx_queues[KNI_MAX_QUEUE_PER_PORT];
57 struct pmd_queue tx_queues[KNI_MAX_QUEUE_PER_PORT];
58 };
59
60 static const struct rte_eth_link pmd_link = {
61 .link_speed = ETH_SPEED_NUM_10G,
62 .link_duplex = ETH_LINK_FULL_DUPLEX,
63 .link_status = ETH_LINK_DOWN,
64 .link_autoneg = ETH_LINK_FIXED,
65 };
66 static int is_kni_initialized;
67
68 static int eth_kni_logtype;
69
70 #define PMD_LOG(level, fmt, args...) \
71 rte_log(RTE_LOG_ ## level, eth_kni_logtype, \
72 "%s(): " fmt "\n", __func__, ##args)
73 static uint16_t
74 eth_kni_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
75 {
76 struct pmd_queue *kni_q = q;
77 struct rte_kni *kni = kni_q->internals->kni;
78 uint16_t nb_pkts;
79
80 nb_pkts = rte_kni_rx_burst(kni, bufs, nb_bufs);
81
82 kni_q->rx.pkts += nb_pkts;
83 kni_q->rx.err_pkts += nb_bufs - nb_pkts;
84
85 return nb_pkts;
86 }
87
88 static uint16_t
89 eth_kni_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
90 {
91 struct pmd_queue *kni_q = q;
92 struct rte_kni *kni = kni_q->internals->kni;
93 uint16_t nb_pkts;
94
95 nb_pkts = rte_kni_tx_burst(kni, bufs, nb_bufs);
96
97 kni_q->tx.pkts += nb_pkts;
98 kni_q->tx.err_pkts += nb_bufs - nb_pkts;
99
100 return nb_pkts;
101 }
102
103 static void *
104 kni_handle_request(void *param)
105 {
106 struct pmd_internals *internals = param;
107 #define MS 1000
108
109 while (!internals->stop_thread) {
110 rte_kni_handle_request(internals->kni);
111 usleep(500 * MS);
112 }
113
114 return param;
115 }
116
117 static int
118 eth_kni_start(struct rte_eth_dev *dev)
119 {
120 struct pmd_internals *internals = dev->data->dev_private;
121 uint16_t port_id = dev->data->port_id;
122 struct rte_mempool *mb_pool;
123 struct rte_kni_conf conf;
124 const char *name = dev->device->name + 4; /* remove net_ */
125
126 snprintf(conf.name, RTE_KNI_NAMESIZE, "%s", name);
127 conf.force_bind = 0;
128 conf.group_id = port_id;
129 conf.mbuf_size = MAX_PACKET_SZ;
130 mb_pool = internals->rx_queues[0].mb_pool;
131
132 internals->kni = rte_kni_alloc(mb_pool, &conf, NULL);
133 if (internals->kni == NULL) {
134 PMD_LOG(ERR,
135 "Fail to create kni interface for port: %d",
136 port_id);
137 return -1;
138 }
139
140 return 0;
141 }
142
143 static int
144 eth_kni_dev_start(struct rte_eth_dev *dev)
145 {
146 struct pmd_internals *internals = dev->data->dev_private;
147 int ret;
148
149 if (internals->is_kni_started == 0) {
150 ret = eth_kni_start(dev);
151 if (ret)
152 return -1;
153 internals->is_kni_started = 1;
154 }
155
156 if (internals->no_request_thread == 0) {
157 ret = rte_ctrl_thread_create(&internals->thread,
158 "kni_handle_req", NULL,
159 kni_handle_request, internals);
160 if (ret) {
161 PMD_LOG(ERR,
162 "Fail to create kni request thread");
163 return -1;
164 }
165 }
166
167 dev->data->dev_link.link_status = 1;
168
169 return 0;
170 }
171
172 static void
173 eth_kni_dev_stop(struct rte_eth_dev *dev)
174 {
175 struct pmd_internals *internals = dev->data->dev_private;
176 int ret;
177
178 if (internals->no_request_thread == 0) {
179 internals->stop_thread = 1;
180
181 ret = pthread_cancel(internals->thread);
182 if (ret)
183 PMD_LOG(ERR, "Can't cancel the thread");
184
185 ret = pthread_join(internals->thread, NULL);
186 if (ret)
187 PMD_LOG(ERR, "Can't join the thread");
188
189 internals->stop_thread = 0;
190 }
191
192 dev->data->dev_link.link_status = 0;
193 }
194
195 static int
196 eth_kni_dev_configure(struct rte_eth_dev *dev __rte_unused)
197 {
198 return 0;
199 }
200
201 static void
202 eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
203 struct rte_eth_dev_info *dev_info)
204 {
205 dev_info->max_mac_addrs = 1;
206 dev_info->max_rx_pktlen = UINT32_MAX;
207 dev_info->max_rx_queues = KNI_MAX_QUEUE_PER_PORT;
208 dev_info->max_tx_queues = KNI_MAX_QUEUE_PER_PORT;
209 dev_info->min_rx_bufsize = 0;
210 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_CRC_STRIP;
211 }
212
213 static int
214 eth_kni_rx_queue_setup(struct rte_eth_dev *dev,
215 uint16_t rx_queue_id,
216 uint16_t nb_rx_desc __rte_unused,
217 unsigned int socket_id __rte_unused,
218 const struct rte_eth_rxconf *rx_conf __rte_unused,
219 struct rte_mempool *mb_pool)
220 {
221 struct pmd_internals *internals = dev->data->dev_private;
222 struct pmd_queue *q;
223
224 q = &internals->rx_queues[rx_queue_id];
225 q->internals = internals;
226 q->mb_pool = mb_pool;
227
228 dev->data->rx_queues[rx_queue_id] = q;
229
230 return 0;
231 }
232
233 static int
234 eth_kni_tx_queue_setup(struct rte_eth_dev *dev,
235 uint16_t tx_queue_id,
236 uint16_t nb_tx_desc __rte_unused,
237 unsigned int socket_id __rte_unused,
238 const struct rte_eth_txconf *tx_conf __rte_unused)
239 {
240 struct pmd_internals *internals = dev->data->dev_private;
241 struct pmd_queue *q;
242
243 q = &internals->tx_queues[tx_queue_id];
244 q->internals = internals;
245
246 dev->data->tx_queues[tx_queue_id] = q;
247
248 return 0;
249 }
250
251 static void
252 eth_kni_queue_release(void *q __rte_unused)
253 {
254 }
255
256 static int
257 eth_kni_link_update(struct rte_eth_dev *dev __rte_unused,
258 int wait_to_complete __rte_unused)
259 {
260 return 0;
261 }
262
263 static int
264 eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
265 {
266 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
267 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
268 struct rte_eth_dev_data *data = dev->data;
269 unsigned long tx_packets_err_total = 0;
270 unsigned int i, num_stats;
271 struct pmd_queue *q;
272
273 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
274 data->nb_rx_queues);
275 for (i = 0; i < num_stats; i++) {
276 q = data->rx_queues[i];
277 stats->q_ipackets[i] = q->rx.pkts;
278 stats->q_ibytes[i] = q->rx.bytes;
279 rx_packets_total += stats->q_ipackets[i];
280 rx_bytes_total += stats->q_ibytes[i];
281 }
282
283 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
284 data->nb_tx_queues);
285 for (i = 0; i < num_stats; i++) {
286 q = data->tx_queues[i];
287 stats->q_opackets[i] = q->tx.pkts;
288 stats->q_obytes[i] = q->tx.bytes;
289 stats->q_errors[i] = q->tx.err_pkts;
290 tx_packets_total += stats->q_opackets[i];
291 tx_bytes_total += stats->q_obytes[i];
292 tx_packets_err_total += stats->q_errors[i];
293 }
294
295 stats->ipackets = rx_packets_total;
296 stats->ibytes = rx_bytes_total;
297 stats->opackets = tx_packets_total;
298 stats->obytes = tx_bytes_total;
299 stats->oerrors = tx_packets_err_total;
300
301 return 0;
302 }
303
304 static void
305 eth_kni_stats_reset(struct rte_eth_dev *dev)
306 {
307 struct rte_eth_dev_data *data = dev->data;
308 struct pmd_queue *q;
309 unsigned int i;
310
311 for (i = 0; i < data->nb_rx_queues; i++) {
312 q = data->rx_queues[i];
313 q->rx.pkts = 0;
314 q->rx.bytes = 0;
315 }
316 for (i = 0; i < data->nb_tx_queues; i++) {
317 q = data->tx_queues[i];
318 q->tx.pkts = 0;
319 q->tx.bytes = 0;
320 q->tx.err_pkts = 0;
321 }
322 }
323
324 static const struct eth_dev_ops eth_kni_ops = {
325 .dev_start = eth_kni_dev_start,
326 .dev_stop = eth_kni_dev_stop,
327 .dev_configure = eth_kni_dev_configure,
328 .dev_infos_get = eth_kni_dev_info,
329 .rx_queue_setup = eth_kni_rx_queue_setup,
330 .tx_queue_setup = eth_kni_tx_queue_setup,
331 .rx_queue_release = eth_kni_queue_release,
332 .tx_queue_release = eth_kni_queue_release,
333 .link_update = eth_kni_link_update,
334 .stats_get = eth_kni_stats_get,
335 .stats_reset = eth_kni_stats_reset,
336 };
337
338 static struct rte_eth_dev *
339 eth_kni_create(struct rte_vdev_device *vdev,
340 struct eth_kni_args *args,
341 unsigned int numa_node)
342 {
343 struct pmd_internals *internals;
344 struct rte_eth_dev_data *data;
345 struct rte_eth_dev *eth_dev;
346
347 PMD_LOG(INFO, "Creating kni ethdev on numa socket %u",
348 numa_node);
349
350 /* reserve an ethdev entry */
351 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
352 if (!eth_dev)
353 return NULL;
354
355 internals = eth_dev->data->dev_private;
356 data = eth_dev->data;
357 data->nb_rx_queues = 1;
358 data->nb_tx_queues = 1;
359 data->dev_link = pmd_link;
360 data->mac_addrs = &internals->eth_addr;
361
362 eth_random_addr(internals->eth_addr.addr_bytes);
363
364 eth_dev->dev_ops = &eth_kni_ops;
365
366 internals->no_request_thread = args->no_request_thread;
367
368 return eth_dev;
369 }
370
371 static int
372 kni_init(void)
373 {
374 if (is_kni_initialized == 0)
375 rte_kni_init(MAX_KNI_PORTS);
376
377 is_kni_initialized++;
378
379 return 0;
380 }
381
382 static int
383 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
384 {
385 struct rte_kvargs *kvlist;
386
387 kvlist = rte_kvargs_parse(params, valid_arguments);
388 if (kvlist == NULL)
389 return -1;
390
391 memset(args, 0, sizeof(struct eth_kni_args));
392
393 if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
394 args->no_request_thread = 1;
395
396 rte_kvargs_free(kvlist);
397
398 return 0;
399 }
400
401 static int
402 eth_kni_probe(struct rte_vdev_device *vdev)
403 {
404 struct rte_eth_dev *eth_dev;
405 struct eth_kni_args args;
406 const char *name;
407 const char *params;
408 int ret;
409
410 name = rte_vdev_device_name(vdev);
411 params = rte_vdev_device_args(vdev);
412 PMD_LOG(INFO, "Initializing eth_kni for %s", name);
413
414 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
415 strlen(params) == 0) {
416 eth_dev = rte_eth_dev_attach_secondary(name);
417 if (!eth_dev) {
418 PMD_LOG(ERR, "Failed to probe %s", name);
419 return -1;
420 }
421 /* TODO: request info from primary to set up Rx and Tx */
422 eth_dev->dev_ops = &eth_kni_ops;
423 eth_dev->device = &vdev->device;
424 rte_eth_dev_probing_finish(eth_dev);
425 return 0;
426 }
427
428 ret = eth_kni_kvargs_process(&args, params);
429 if (ret < 0)
430 return ret;
431
432 ret = kni_init();
433 if (ret < 0)
434 return ret;
435
436 eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
437 if (eth_dev == NULL)
438 goto kni_uninit;
439
440 eth_dev->rx_pkt_burst = eth_kni_rx;
441 eth_dev->tx_pkt_burst = eth_kni_tx;
442
443 rte_eth_dev_probing_finish(eth_dev);
444 return 0;
445
446 kni_uninit:
447 is_kni_initialized--;
448 if (is_kni_initialized == 0)
449 rte_kni_close();
450 return -1;
451 }
452
453 static int
454 eth_kni_remove(struct rte_vdev_device *vdev)
455 {
456 struct rte_eth_dev *eth_dev;
457 struct pmd_internals *internals;
458 const char *name;
459
460 name = rte_vdev_device_name(vdev);
461 PMD_LOG(INFO, "Un-Initializing eth_kni for %s", name);
462
463 /* find the ethdev entry */
464 eth_dev = rte_eth_dev_allocated(name);
465 if (eth_dev == NULL)
466 return -1;
467
468 eth_kni_dev_stop(eth_dev);
469
470 internals = eth_dev->data->dev_private;
471 rte_kni_release(internals->kni);
472
473 rte_free(internals);
474
475 rte_eth_dev_release_port(eth_dev);
476
477 is_kni_initialized--;
478 if (is_kni_initialized == 0)
479 rte_kni_close();
480
481 return 0;
482 }
483
484 static struct rte_vdev_driver eth_kni_drv = {
485 .probe = eth_kni_probe,
486 .remove = eth_kni_remove,
487 };
488
489 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
490 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");
491
492 RTE_INIT(eth_kni_init_log)
493 {
494 eth_kni_logtype = rte_log_register("pmd.net.kni");
495 if (eth_kni_logtype >= 0)
496 rte_log_set_level(eth_kni_logtype, RTE_LOG_NOTICE);
497 }