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