1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
3 */
4
5 #include <stdlib.h>
6
7 #include <rte_common.h>
8 #include <rte_string_fns.h>
9
10 #include "tmgr.h"
11
12 static struct rte_sched_subport_profile_params
13 subport_profile[TMGR_SUBPORT_PROFILE_MAX];
14
15 static uint32_t n_subport_profiles;
16
17 static struct rte_sched_pipe_params
18 pipe_profile[TMGR_PIPE_PROFILE_MAX];
19
20 static uint32_t n_pipe_profiles;
21
22 static const struct rte_sched_subport_params subport_params_default = {
23 .n_pipes_per_subport_enabled = 0, /* filled at runtime */
24 .qsize = {64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
25 .pipe_profiles = pipe_profile,
26 .n_pipe_profiles = 0, /* filled at run time */
27 .n_max_pipe_profiles = RTE_DIM(pipe_profile),
28 .cman_params = NULL,
29 };
30
31 static struct tmgr_port_list tmgr_port_list;
32
33 int
tmgr_init(void)34 tmgr_init(void)
35 {
36 TAILQ_INIT(&tmgr_port_list);
37
38 return 0;
39 }
40
41 struct tmgr_port *
tmgr_port_find(const char * name)42 tmgr_port_find(const char *name)
43 {
44 struct tmgr_port *tmgr_port;
45
46 if (name == NULL)
47 return NULL;
48
49 TAILQ_FOREACH(tmgr_port, &tmgr_port_list, node)
50 if (strcmp(tmgr_port->name, name) == 0)
51 return tmgr_port;
52
53 return NULL;
54 }
55
56 int
tmgr_subport_profile_add(struct rte_sched_subport_profile_params * params)57 tmgr_subport_profile_add(struct rte_sched_subport_profile_params *params)
58 {
59 /* Check input params */
60 if (params == NULL)
61 return -1;
62
63 /* Save profile */
64 memcpy(&subport_profile[n_subport_profiles],
65 params,
66 sizeof(*params));
67
68 n_subport_profiles++;
69
70 return 0;
71 }
72
73 int
tmgr_pipe_profile_add(struct rte_sched_pipe_params * p)74 tmgr_pipe_profile_add(struct rte_sched_pipe_params *p)
75 {
76 /* Check input params */
77 if (p == NULL)
78 return -1;
79
80 /* Save profile */
81 memcpy(&pipe_profile[n_pipe_profiles],
82 p,
83 sizeof(*p));
84
85 n_pipe_profiles++;
86
87 return 0;
88 }
89
90 struct tmgr_port *
tmgr_port_create(const char * name,struct tmgr_port_params * params)91 tmgr_port_create(const char *name, struct tmgr_port_params *params)
92 {
93 struct rte_sched_subport_params subport_params;
94 struct rte_sched_port_params p;
95 struct tmgr_port *tmgr_port;
96 struct rte_sched_port *s;
97 uint32_t i, j;
98
99 /* Check input params */
100 if ((name == NULL) ||
101 tmgr_port_find(name) ||
102 (params == NULL) ||
103 (params->n_subports_per_port == 0) ||
104 (params->n_pipes_per_subport == 0) ||
105 (params->cpu_id >= RTE_MAX_NUMA_NODES) ||
106 (n_subport_profiles == 0) ||
107 (n_pipe_profiles == 0))
108 return NULL;
109
110 /* Resource create */
111 p.name = name;
112 p.socket = (int) params->cpu_id;
113 p.rate = params->rate;
114 p.mtu = params->mtu;
115 p.frame_overhead = params->frame_overhead;
116 p.n_subports_per_port = params->n_subports_per_port;
117 p.n_subport_profiles = n_subport_profiles;
118 p.subport_profiles = subport_profile;
119 p.n_max_subport_profiles = TMGR_SUBPORT_PROFILE_MAX;
120 p.n_pipes_per_subport = params->n_pipes_per_subport;
121
122
123 s = rte_sched_port_config(&p);
124 if (s == NULL)
125 return NULL;
126
127 memcpy(&subport_params, &subport_params_default,
128 sizeof(subport_params_default));
129
130 subport_params.n_pipe_profiles = n_pipe_profiles;
131 subport_params.n_pipes_per_subport_enabled =
132 params->n_pipes_per_subport;
133
134 for (i = 0; i < params->n_subports_per_port; i++) {
135 int status;
136
137 status = rte_sched_subport_config(
138 s,
139 i,
140 &subport_params,
141 0);
142
143 if (status) {
144 rte_sched_port_free(s);
145 return NULL;
146 }
147
148 for (j = 0; j < params->n_pipes_per_subport; j++) {
149
150 status = rte_sched_pipe_config(
151 s,
152 i,
153 j,
154 0);
155
156 if (status) {
157 rte_sched_port_free(s);
158 return NULL;
159 }
160 }
161 }
162
163 /* Node allocation */
164 tmgr_port = calloc(1, sizeof(struct tmgr_port));
165 if (tmgr_port == NULL) {
166 rte_sched_port_free(s);
167 return NULL;
168 }
169
170 /* Node fill in */
171 strlcpy(tmgr_port->name, name, sizeof(tmgr_port->name));
172 tmgr_port->s = s;
173 tmgr_port->n_subports_per_port = params->n_subports_per_port;
174 tmgr_port->n_pipes_per_subport = params->n_pipes_per_subport;
175
176 /* Node add to list */
177 TAILQ_INSERT_TAIL(&tmgr_port_list, tmgr_port, node);
178
179 return tmgr_port;
180 }
181
182 int
tmgr_subport_config(const char * port_name,uint32_t subport_id,uint32_t subport_profile_id)183 tmgr_subport_config(const char *port_name,
184 uint32_t subport_id,
185 uint32_t subport_profile_id)
186 {
187 struct tmgr_port *port;
188 int status;
189
190 /* Check input params */
191 if (port_name == NULL)
192 return -1;
193
194 port = tmgr_port_find(port_name);
195 if ((port == NULL) ||
196 (subport_id >= port->n_subports_per_port) ||
197 (subport_profile_id >= n_subport_profiles))
198 return -1;
199
200 /* Resource config */
201 status = rte_sched_subport_config(
202 port->s,
203 subport_id,
204 NULL,
205 subport_profile_id);
206
207 return status;
208 }
209
210 int
tmgr_pipe_config(const char * port_name,uint32_t subport_id,uint32_t pipe_id_first,uint32_t pipe_id_last,uint32_t pipe_profile_id)211 tmgr_pipe_config(const char *port_name,
212 uint32_t subport_id,
213 uint32_t pipe_id_first,
214 uint32_t pipe_id_last,
215 uint32_t pipe_profile_id)
216 {
217 struct tmgr_port *port;
218 uint32_t i;
219
220 /* Check input params */
221 if (port_name == NULL)
222 return -1;
223
224 port = tmgr_port_find(port_name);
225 if ((port == NULL) ||
226 (subport_id >= port->n_subports_per_port) ||
227 (pipe_id_first >= port->n_pipes_per_subport) ||
228 (pipe_id_last >= port->n_pipes_per_subport) ||
229 (pipe_id_first > pipe_id_last) ||
230 (pipe_profile_id >= n_pipe_profiles))
231 return -1;
232
233 /* Resource config */
234 for (i = pipe_id_first; i <= pipe_id_last; i++) {
235 int status;
236
237 status = rte_sched_pipe_config(
238 port->s,
239 subport_id,
240 i,
241 (int) pipe_profile_id);
242
243 if (status)
244 return status;
245 }
246
247 return 0;
248 }
249