165abfed5e63658b305a84934e34a8b31ef9e674
[dpdk.git] / drivers / net / softnic / rte_eth_softnic_tm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <rte_malloc.h>
39
40 #include "rte_eth_softnic_internals.h"
41 #include "rte_eth_softnic.h"
42
43 #define BYTES_IN_MBPS (1000 * 1000 / 8)
44
45 int
46 tm_params_check(struct pmd_params *params, uint32_t hard_rate)
47 {
48         uint64_t hard_rate_bytes_per_sec = hard_rate * BYTES_IN_MBPS;
49         uint32_t i;
50
51         /* rate */
52         if (params->soft.tm.rate) {
53                 if (params->soft.tm.rate > hard_rate_bytes_per_sec)
54                         return -EINVAL;
55         } else {
56                 params->soft.tm.rate =
57                         (hard_rate_bytes_per_sec > UINT32_MAX) ?
58                                 UINT32_MAX : hard_rate_bytes_per_sec;
59         }
60
61         /* nb_queues */
62         if (params->soft.tm.nb_queues == 0)
63                 return -EINVAL;
64
65         if (params->soft.tm.nb_queues < RTE_SCHED_QUEUES_PER_PIPE)
66                 params->soft.tm.nb_queues = RTE_SCHED_QUEUES_PER_PIPE;
67
68         params->soft.tm.nb_queues =
69                 rte_align32pow2(params->soft.tm.nb_queues);
70
71         /* qsize */
72         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
73                 if (params->soft.tm.qsize[i] == 0)
74                         return -EINVAL;
75
76                 params->soft.tm.qsize[i] =
77                         rte_align32pow2(params->soft.tm.qsize[i]);
78         }
79
80         /* enq_bsz, deq_bsz */
81         if (params->soft.tm.enq_bsz == 0 ||
82                 params->soft.tm.deq_bsz == 0 ||
83                 params->soft.tm.deq_bsz >= params->soft.tm.enq_bsz)
84                 return -EINVAL;
85
86         return 0;
87 }
88
89 int
90 tm_init(struct pmd_internals *p,
91         struct pmd_params *params,
92         int numa_node)
93 {
94         uint32_t enq_bsz = params->soft.tm.enq_bsz;
95         uint32_t deq_bsz = params->soft.tm.deq_bsz;
96
97         p->soft.tm.pkts_enq = rte_zmalloc_socket(params->soft.name,
98                 2 * enq_bsz * sizeof(struct rte_mbuf *),
99                 0,
100                 numa_node);
101
102         if (p->soft.tm.pkts_enq == NULL)
103                 return -ENOMEM;
104
105         p->soft.tm.pkts_deq = rte_zmalloc_socket(params->soft.name,
106                 deq_bsz * sizeof(struct rte_mbuf *),
107                 0,
108                 numa_node);
109
110         if (p->soft.tm.pkts_deq == NULL) {
111                 rte_free(p->soft.tm.pkts_enq);
112                 return -ENOMEM;
113         }
114
115         return 0;
116 }
117
118 void
119 tm_free(struct pmd_internals *p)
120 {
121         rte_free(p->soft.tm.pkts_enq);
122         rte_free(p->soft.tm.pkts_deq);
123 }
124
125 int
126 tm_start(struct pmd_internals *p)
127 {
128         struct tm_params *t = &p->soft.tm.params;
129         uint32_t n_subports, subport_id;
130         int status;
131
132         /* Port */
133         p->soft.tm.sched = rte_sched_port_config(&t->port_params);
134         if (p->soft.tm.sched == NULL)
135                 return -1;
136
137         /* Subport */
138         n_subports = t->port_params.n_subports_per_port;
139         for (subport_id = 0; subport_id < n_subports; subport_id++) {
140                 uint32_t n_pipes_per_subport =
141                         t->port_params.n_pipes_per_subport;
142                 uint32_t pipe_id;
143
144                 status = rte_sched_subport_config(p->soft.tm.sched,
145                         subport_id,
146                         &t->subport_params[subport_id]);
147                 if (status) {
148                         rte_sched_port_free(p->soft.tm.sched);
149                         return -1;
150                 }
151
152                 /* Pipe */
153                 n_pipes_per_subport = t->port_params.n_pipes_per_subport;
154                 for (pipe_id = 0; pipe_id < n_pipes_per_subport; pipe_id++) {
155                         int pos = subport_id * TM_MAX_PIPES_PER_SUBPORT +
156                                 pipe_id;
157                         int profile_id = t->pipe_to_profile[pos];
158
159                         if (profile_id < 0)
160                                 continue;
161
162                         status = rte_sched_pipe_config(p->soft.tm.sched,
163                                 subport_id,
164                                 pipe_id,
165                                 profile_id);
166                         if (status) {
167                                 rte_sched_port_free(p->soft.tm.sched);
168                                 return -1;
169                         }
170                 }
171         }
172
173         return 0;
174 }
175
176 void
177 tm_stop(struct pmd_internals *p)
178 {
179         if (p->soft.tm.sched)
180                 rte_sched_port_free(p->soft.tm.sched);
181 }