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