net/softnic: add thread
[dpdk.git] / drivers / net / softnic / rte_eth_softnic_thread.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <stdlib.h>
6
7 #include <rte_cycles.h>
8 #include <rte_lcore.h>
9 #include <rte_ring.h>
10
11 #include "rte_eth_softnic_internals.h"
12
13 /**
14  * Master thread: data plane thread init
15  */
16 void
17 softnic_thread_free(struct pmd_internals *softnic)
18 {
19         uint32_t i;
20
21         RTE_LCORE_FOREACH_SLAVE(i) {
22                 struct softnic_thread *t = &softnic->thread[i];
23
24                 /* MSGQs */
25                 if (t->msgq_req)
26                         rte_ring_free(t->msgq_req);
27
28                 if (t->msgq_rsp)
29                         rte_ring_free(t->msgq_rsp);
30         }
31 }
32
33 int
34 softnic_thread_init(struct pmd_internals *softnic)
35 {
36         uint32_t i;
37
38         RTE_LCORE_FOREACH_SLAVE(i) {
39                 char ring_name[NAME_MAX];
40                 struct rte_ring *msgq_req, *msgq_rsp;
41                 struct softnic_thread *t = &softnic->thread[i];
42                 struct softnic_thread_data *t_data = &softnic->thread_data[i];
43                 uint32_t cpu_id = rte_lcore_to_socket_id(i);
44
45                 /* MSGQs */
46                 snprintf(ring_name, sizeof(ring_name), "%s-TH%u-REQ",
47                         softnic->params.name,
48                         i);
49
50                 msgq_req = rte_ring_create(ring_name,
51                         THREAD_MSGQ_SIZE,
52                         cpu_id,
53                         RING_F_SP_ENQ | RING_F_SC_DEQ);
54
55                 if (msgq_req == NULL) {
56                         softnic_thread_free(softnic);
57                         return -1;
58                 }
59
60                 snprintf(ring_name, sizeof(ring_name), "%s-TH%u-RSP",
61                         softnic->params.name,
62                         i);
63
64                 msgq_rsp = rte_ring_create(ring_name,
65                         THREAD_MSGQ_SIZE,
66                         cpu_id,
67                         RING_F_SP_ENQ | RING_F_SC_DEQ);
68
69                 if (msgq_rsp == NULL) {
70                         softnic_thread_free(softnic);
71                         return -1;
72                 }
73
74                 /* Master thread records */
75                 t->msgq_req = msgq_req;
76                 t->msgq_rsp = msgq_rsp;
77                 t->enabled = 1;
78
79                 /* Data plane thread records */
80                 t_data->n_pipelines = 0;
81                 t_data->msgq_req = msgq_req;
82                 t_data->msgq_rsp = msgq_rsp;
83                 t_data->timer_period =
84                         (rte_get_tsc_hz() * THREAD_TIMER_PERIOD_MS) / 1000;
85                 t_data->time_next = rte_get_tsc_cycles() + t_data->timer_period;
86                 t_data->time_next_min = t_data->time_next;
87         }
88
89         return 0;
90 }