4 * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include <rte_malloc.h>
36 #include "base/i40e_prototype.h"
37 #include "i40e_ethdev.h"
39 static int i40e_tm_capabilities_get(struct rte_eth_dev *dev,
40 struct rte_tm_capabilities *cap,
41 struct rte_tm_error *error);
42 static int i40e_shaper_profile_add(struct rte_eth_dev *dev,
43 uint32_t shaper_profile_id,
44 struct rte_tm_shaper_params *profile,
45 struct rte_tm_error *error);
46 static int i40e_shaper_profile_del(struct rte_eth_dev *dev,
47 uint32_t shaper_profile_id,
48 struct rte_tm_error *error);
49 static int i40e_node_add(struct rte_eth_dev *dev, uint32_t node_id,
50 uint32_t parent_node_id, uint32_t priority,
51 uint32_t weight, uint32_t level_id,
52 struct rte_tm_node_params *params,
53 struct rte_tm_error *error);
54 static int i40e_node_delete(struct rte_eth_dev *dev, uint32_t node_id,
55 struct rte_tm_error *error);
56 static int i40e_node_type_get(struct rte_eth_dev *dev, uint32_t node_id,
57 int *is_leaf, struct rte_tm_error *error);
59 const struct rte_tm_ops i40e_tm_ops = {
60 .capabilities_get = i40e_tm_capabilities_get,
61 .shaper_profile_add = i40e_shaper_profile_add,
62 .shaper_profile_delete = i40e_shaper_profile_del,
63 .node_add = i40e_node_add,
64 .node_delete = i40e_node_delete,
65 .node_type_get = i40e_node_type_get,
69 i40e_tm_ops_get(struct rte_eth_dev *dev __rte_unused,
75 *(const void **)arg = &i40e_tm_ops;
81 i40e_tm_conf_init(struct rte_eth_dev *dev)
83 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
85 /* initialize shaper profile list */
86 TAILQ_INIT(&pf->tm_conf.shaper_profile_list);
88 /* initialize node configuration */
89 pf->tm_conf.root = NULL;
90 TAILQ_INIT(&pf->tm_conf.tc_list);
91 TAILQ_INIT(&pf->tm_conf.queue_list);
92 pf->tm_conf.nb_tc_node = 0;
93 pf->tm_conf.nb_queue_node = 0;
94 pf->tm_conf.committed = false;
98 i40e_tm_conf_uninit(struct rte_eth_dev *dev)
100 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
101 struct i40e_tm_shaper_profile *shaper_profile;
102 struct i40e_tm_node *tm_node;
104 /* clear node configuration */
105 while ((tm_node = TAILQ_FIRST(&pf->tm_conf.queue_list))) {
106 TAILQ_REMOVE(&pf->tm_conf.queue_list, tm_node, node);
109 pf->tm_conf.nb_queue_node = 0;
110 while ((tm_node = TAILQ_FIRST(&pf->tm_conf.tc_list))) {
111 TAILQ_REMOVE(&pf->tm_conf.tc_list, tm_node, node);
114 pf->tm_conf.nb_tc_node = 0;
115 if (pf->tm_conf.root) {
116 rte_free(pf->tm_conf.root);
117 pf->tm_conf.root = NULL;
120 /* Remove all shaper profiles */
121 while ((shaper_profile =
122 TAILQ_FIRST(&pf->tm_conf.shaper_profile_list))) {
123 TAILQ_REMOVE(&pf->tm_conf.shaper_profile_list,
124 shaper_profile, node);
125 rte_free(shaper_profile);
129 static inline uint16_t
130 i40e_tc_nb_get(struct rte_eth_dev *dev)
132 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
133 struct i40e_vsi *main_vsi = pf->main_vsi;
137 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
138 if (main_vsi->enabled_tc & BIT_ULL(i))
146 i40e_tm_capabilities_get(struct rte_eth_dev *dev,
147 struct rte_tm_capabilities *cap,
148 struct rte_tm_error *error)
150 struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
151 uint16_t tc_nb = i40e_tc_nb_get(dev);
156 if (tc_nb > hw->func_caps.num_tx_qp)
159 error->type = RTE_TM_ERROR_TYPE_NONE;
161 /* set all the parameters to 0 first. */
162 memset(cap, 0, sizeof(struct rte_tm_capabilities));
165 * support port + TCs + queues
166 * here shows the max capability not the current configuration.
168 cap->n_nodes_max = 1 + I40E_MAX_TRAFFIC_CLASS + hw->func_caps.num_tx_qp;
169 cap->n_levels_max = 3; /* port, TC, queue */
170 cap->non_leaf_nodes_identical = 1;
171 cap->leaf_nodes_identical = 1;
172 cap->shaper_n_max = cap->n_nodes_max;
173 cap->shaper_private_n_max = cap->n_nodes_max;
174 cap->shaper_private_dual_rate_n_max = 0;
175 cap->shaper_private_rate_min = 0;
176 /* 40Gbps -> 5GBps */
177 cap->shaper_private_rate_max = 5000000000ull;
178 cap->shaper_shared_n_max = 0;
179 cap->shaper_shared_n_nodes_per_shaper_max = 0;
180 cap->shaper_shared_n_shapers_per_node_max = 0;
181 cap->shaper_shared_dual_rate_n_max = 0;
182 cap->shaper_shared_rate_min = 0;
183 cap->shaper_shared_rate_max = 0;
184 cap->sched_n_children_max = hw->func_caps.num_tx_qp;
186 * HW supports SP. But no plan to support it now.
187 * So, all the nodes should have the same priority.
189 cap->sched_sp_n_priorities_max = 1;
190 cap->sched_wfq_n_children_per_group_max = 0;
191 cap->sched_wfq_n_groups_max = 0;
193 * SW only supports fair round robin now.
194 * So, all the nodes should have the same weight.
196 cap->sched_wfq_weight_max = 1;
197 cap->cman_head_drop_supported = 0;
198 cap->dynamic_update_mask = 0;
199 cap->shaper_pkt_length_adjust_min = RTE_TM_ETH_FRAMING_OVERHEAD;
200 cap->shaper_pkt_length_adjust_max = RTE_TM_ETH_FRAMING_OVERHEAD_FCS;
201 cap->cman_wred_context_n_max = 0;
202 cap->cman_wred_context_private_n_max = 0;
203 cap->cman_wred_context_shared_n_max = 0;
204 cap->cman_wred_context_shared_n_nodes_per_context_max = 0;
205 cap->cman_wred_context_shared_n_contexts_per_node_max = 0;
211 static inline struct i40e_tm_shaper_profile *
212 i40e_shaper_profile_search(struct rte_eth_dev *dev,
213 uint32_t shaper_profile_id)
215 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
216 struct i40e_shaper_profile_list *shaper_profile_list =
217 &pf->tm_conf.shaper_profile_list;
218 struct i40e_tm_shaper_profile *shaper_profile;
220 TAILQ_FOREACH(shaper_profile, shaper_profile_list, node) {
221 if (shaper_profile_id == shaper_profile->shaper_profile_id)
222 return shaper_profile;
229 i40e_shaper_profile_param_check(struct rte_tm_shaper_params *profile,
230 struct rte_tm_error *error)
232 /* min rate not supported */
233 if (profile->committed.rate) {
234 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_COMMITTED_RATE;
235 error->message = "committed rate not supported";
238 /* min bucket size not supported */
239 if (profile->committed.size) {
240 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_COMMITTED_SIZE;
241 error->message = "committed bucket size not supported";
244 /* max bucket size not supported */
245 if (profile->peak.size) {
246 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_PEAK_SIZE;
247 error->message = "peak bucket size not supported";
250 /* length adjustment not supported */
251 if (profile->pkt_length_adjust) {
252 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_PKT_ADJUST_LEN;
253 error->message = "packet length adjustment not supported";
261 i40e_shaper_profile_add(struct rte_eth_dev *dev,
262 uint32_t shaper_profile_id,
263 struct rte_tm_shaper_params *profile,
264 struct rte_tm_error *error)
266 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
267 struct i40e_tm_shaper_profile *shaper_profile;
270 if (!profile || !error)
273 ret = i40e_shaper_profile_param_check(profile, error);
277 shaper_profile = i40e_shaper_profile_search(dev, shaper_profile_id);
279 if (shaper_profile) {
280 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_ID;
281 error->message = "profile ID exist";
285 shaper_profile = rte_zmalloc("i40e_tm_shaper_profile",
286 sizeof(struct i40e_tm_shaper_profile),
290 shaper_profile->shaper_profile_id = shaper_profile_id;
291 (void)rte_memcpy(&shaper_profile->profile, profile,
292 sizeof(struct rte_tm_shaper_params));
293 TAILQ_INSERT_TAIL(&pf->tm_conf.shaper_profile_list,
294 shaper_profile, node);
300 i40e_shaper_profile_del(struct rte_eth_dev *dev,
301 uint32_t shaper_profile_id,
302 struct rte_tm_error *error)
304 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
305 struct i40e_tm_shaper_profile *shaper_profile;
310 shaper_profile = i40e_shaper_profile_search(dev, shaper_profile_id);
312 if (!shaper_profile) {
313 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_ID;
314 error->message = "profile ID not exist";
318 /* don't delete a profile if it's used by one or several nodes */
319 if (shaper_profile->reference_count) {
320 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE;
321 error->message = "profile in use";
325 TAILQ_REMOVE(&pf->tm_conf.shaper_profile_list, shaper_profile, node);
326 rte_free(shaper_profile);
331 static inline struct i40e_tm_node *
332 i40e_tm_node_search(struct rte_eth_dev *dev,
333 uint32_t node_id, enum i40e_tm_node_type *node_type)
335 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
336 struct i40e_tm_node_list *queue_list = &pf->tm_conf.queue_list;
337 struct i40e_tm_node_list *tc_list = &pf->tm_conf.tc_list;
338 struct i40e_tm_node *tm_node;
340 if (pf->tm_conf.root && pf->tm_conf.root->id == node_id) {
341 *node_type = I40E_TM_NODE_TYPE_PORT;
342 return pf->tm_conf.root;
345 TAILQ_FOREACH(tm_node, tc_list, node) {
346 if (tm_node->id == node_id) {
347 *node_type = I40E_TM_NODE_TYPE_TC;
352 TAILQ_FOREACH(tm_node, queue_list, node) {
353 if (tm_node->id == node_id) {
354 *node_type = I40E_TM_NODE_TYPE_QUEUE;
363 i40e_node_param_check(uint32_t node_id, uint32_t parent_node_id,
364 uint32_t priority, uint32_t weight,
365 struct rte_tm_node_params *params,
366 struct rte_tm_error *error)
368 if (node_id == RTE_TM_NODE_ID_NULL) {
369 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
370 error->message = "invalid node id";
375 error->type = RTE_TM_ERROR_TYPE_NODE_PRIORITY;
376 error->message = "priority should be 0";
381 error->type = RTE_TM_ERROR_TYPE_NODE_WEIGHT;
382 error->message = "weight must be 1";
386 /* not support shared shaper */
387 if (params->shared_shaper_id) {
388 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_SHARED_SHAPER_ID;
389 error->message = "shared shaper not supported";
392 if (params->n_shared_shapers) {
393 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SHARED_SHAPERS;
394 error->message = "shared shaper not supported";
399 if (parent_node_id == RTE_TM_NODE_ID_NULL) {
400 if (params->nonleaf.wfq_weight_mode) {
402 RTE_TM_ERROR_TYPE_NODE_PARAMS_WFQ_WEIGHT_MODE;
403 error->message = "WFQ not supported";
406 if (params->nonleaf.n_sp_priorities != 1) {
408 RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SP_PRIORITIES;
409 error->message = "SP priority not supported";
411 } else if (params->nonleaf.wfq_weight_mode &&
412 !(*params->nonleaf.wfq_weight_mode)) {
414 RTE_TM_ERROR_TYPE_NODE_PARAMS_WFQ_WEIGHT_MODE;
415 error->message = "WFP should be byte mode";
422 /* for TC or queue node */
423 if (params->leaf.cman) {
424 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_CMAN;
425 error->message = "Congestion management not supported";
428 if (params->leaf.wred.wred_profile_id !=
429 RTE_TM_WRED_PROFILE_ID_NONE) {
431 RTE_TM_ERROR_TYPE_NODE_PARAMS_WRED_PROFILE_ID;
432 error->message = "WRED not supported";
435 if (params->leaf.wred.shared_wred_context_id) {
437 RTE_TM_ERROR_TYPE_NODE_PARAMS_SHARED_WRED_CONTEXT_ID;
438 error->message = "WRED not supported";
441 if (params->leaf.wred.n_shared_wred_contexts) {
443 RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SHARED_WRED_CONTEXTS;
444 error->message = "WRED not supported";
452 * Now the TC and queue configuration is controlled by DCB.
453 * We need check if the node configuration follows the DCB configuration.
454 * In the future, we may use TM to cover DCB.
457 i40e_node_add(struct rte_eth_dev *dev, uint32_t node_id,
458 uint32_t parent_node_id, uint32_t priority,
459 uint32_t weight, uint32_t level_id,
460 struct rte_tm_node_params *params,
461 struct rte_tm_error *error)
463 struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
464 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
465 enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
466 enum i40e_tm_node_type parent_node_type = I40E_TM_NODE_TYPE_MAX;
467 struct i40e_tm_shaper_profile *shaper_profile;
468 struct i40e_tm_node *tm_node;
469 struct i40e_tm_node *parent_node;
473 if (!params || !error)
476 /* if already committed */
477 if (pf->tm_conf.committed) {
478 error->type = RTE_TM_ERROR_TYPE_UNSPECIFIED;
479 error->message = "already committed";
483 ret = i40e_node_param_check(node_id, parent_node_id, priority, weight,
488 /* check if the node ID is already used */
489 if (i40e_tm_node_search(dev, node_id, &node_type)) {
490 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
491 error->message = "node id already used";
495 /* check the shaper profile id */
496 shaper_profile = i40e_shaper_profile_search(dev,
497 params->shaper_profile_id);
498 if (!shaper_profile) {
499 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_SHAPER_PROFILE_ID;
500 error->message = "shaper profile not exist";
504 /* root node if not have a parent */
505 if (parent_node_id == RTE_TM_NODE_ID_NULL) {
507 if (level_id != RTE_TM_NODE_LEVEL_ID_ANY &&
508 level_id > I40E_TM_NODE_TYPE_PORT) {
509 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
510 error->message = "Wrong level";
514 /* obviously no more than one root */
515 if (pf->tm_conf.root) {
516 error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
517 error->message = "already have a root";
521 /* add the root node */
522 tm_node = rte_zmalloc("i40e_tm_node",
523 sizeof(struct i40e_tm_node),
527 tm_node->id = node_id;
528 tm_node->priority = priority;
529 tm_node->weight = weight;
530 tm_node->reference_count = 0;
531 tm_node->parent = NULL;
532 tm_node->shaper_profile = shaper_profile;
533 (void)rte_memcpy(&tm_node->params, params,
534 sizeof(struct rte_tm_node_params));
535 pf->tm_conf.root = tm_node;
537 /* increase the reference counter of the shaper profile */
538 shaper_profile->reference_count++;
543 /* TC or queue node */
544 /* check the parent node */
545 parent_node = i40e_tm_node_search(dev, parent_node_id,
548 error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
549 error->message = "parent not exist";
552 if (parent_node_type != I40E_TM_NODE_TYPE_PORT &&
553 parent_node_type != I40E_TM_NODE_TYPE_TC) {
554 error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
555 error->message = "parent is not port or TC";
559 if (level_id != RTE_TM_NODE_LEVEL_ID_ANY &&
560 level_id != parent_node_type + 1) {
561 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
562 error->message = "Wrong level";
566 /* check the node number */
567 if (parent_node_type == I40E_TM_NODE_TYPE_PORT) {
568 /* check the TC number */
569 tc_nb = i40e_tc_nb_get(dev);
570 if (pf->tm_conf.nb_tc_node >= tc_nb) {
571 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
572 error->message = "too many TCs";
576 /* check the queue number */
577 if (pf->tm_conf.nb_queue_node >= hw->func_caps.num_tx_qp) {
578 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
579 error->message = "too many queues";
585 * For queue, the node id means queue id.
587 if (node_id >= hw->func_caps.num_tx_qp) {
588 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
589 error->message = "too large queue id";
594 /* add the TC or queue node */
595 tm_node = rte_zmalloc("i40e_tm_node",
596 sizeof(struct i40e_tm_node),
600 tm_node->id = node_id;
601 tm_node->priority = priority;
602 tm_node->weight = weight;
603 tm_node->reference_count = 0;
604 tm_node->parent = pf->tm_conf.root;
605 tm_node->shaper_profile = shaper_profile;
606 (void)rte_memcpy(&tm_node->params, params,
607 sizeof(struct rte_tm_node_params));
608 if (parent_node_type == I40E_TM_NODE_TYPE_PORT) {
609 TAILQ_INSERT_TAIL(&pf->tm_conf.tc_list,
611 pf->tm_conf.nb_tc_node++;
613 TAILQ_INSERT_TAIL(&pf->tm_conf.queue_list,
615 pf->tm_conf.nb_queue_node++;
617 tm_node->parent->reference_count++;
619 /* increase the reference counter of the shaper profile */
620 shaper_profile->reference_count++;
626 i40e_node_delete(struct rte_eth_dev *dev, uint32_t node_id,
627 struct rte_tm_error *error)
629 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
630 enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
631 struct i40e_tm_node *tm_node;
636 /* if already committed */
637 if (pf->tm_conf.committed) {
638 error->type = RTE_TM_ERROR_TYPE_UNSPECIFIED;
639 error->message = "already committed";
643 if (node_id == RTE_TM_NODE_ID_NULL) {
644 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
645 error->message = "invalid node id";
649 /* check if the node id exists */
650 tm_node = i40e_tm_node_search(dev, node_id, &node_type);
652 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
653 error->message = "no such node";
657 /* the node should have no child */
658 if (tm_node->reference_count) {
659 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
661 "cannot delete a node which has children";
666 if (node_type == I40E_TM_NODE_TYPE_PORT) {
667 tm_node->shaper_profile->reference_count--;
669 pf->tm_conf.root = NULL;
673 /* TC or queue node */
674 tm_node->shaper_profile->reference_count--;
675 tm_node->parent->reference_count--;
676 if (node_type == I40E_TM_NODE_TYPE_TC) {
677 TAILQ_REMOVE(&pf->tm_conf.tc_list, tm_node, node);
678 pf->tm_conf.nb_tc_node--;
680 TAILQ_REMOVE(&pf->tm_conf.queue_list, tm_node, node);
681 pf->tm_conf.nb_queue_node--;
689 i40e_node_type_get(struct rte_eth_dev *dev, uint32_t node_id,
690 int *is_leaf, struct rte_tm_error *error)
692 enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
693 struct i40e_tm_node *tm_node;
695 if (!is_leaf || !error)
698 if (node_id == RTE_TM_NODE_ID_NULL) {
699 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
700 error->message = "invalid node id";
704 /* check if the node id exists */
705 tm_node = i40e_tm_node_search(dev, node_id, &node_type);
707 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
708 error->message = "no such node";
712 if (node_type == I40E_TM_NODE_TYPE_QUEUE)