1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #include <stdlib.h>
6 #include <string.h>
7 
8 #include <rte_ethdev.h>
9 #include <rte_string_fns.h>
10 
11 #include "rte_eth_softnic_internals.h"
12 
13 int
softnic_link_init(struct pmd_internals * p)14 softnic_link_init(struct pmd_internals *p)
15 {
16 	TAILQ_INIT(&p->link_list);
17 
18 	return 0;
19 }
20 
21 void
softnic_link_free(struct pmd_internals * p)22 softnic_link_free(struct pmd_internals *p)
23 {
24 	for ( ; ; ) {
25 		struct softnic_link *link;
26 
27 		link = TAILQ_FIRST(&p->link_list);
28 		if (link == NULL)
29 			break;
30 
31 		TAILQ_REMOVE(&p->link_list, link, node);
32 		free(link);
33 	}
34 }
35 
36 struct softnic_link *
softnic_link_find(struct pmd_internals * p,const char * name)37 softnic_link_find(struct pmd_internals *p,
38 	const char *name)
39 {
40 	struct softnic_link *link;
41 
42 	if (name == NULL)
43 		return NULL;
44 
45 	TAILQ_FOREACH(link, &p->link_list, node)
46 		if (strcmp(link->name, name) == 0)
47 			return link;
48 
49 	return NULL;
50 }
51 
52 struct softnic_link *
softnic_link_create(struct pmd_internals * p,const char * name,struct softnic_link_params * params)53 softnic_link_create(struct pmd_internals *p,
54 	const char *name,
55 	struct softnic_link_params *params)
56 {
57 	struct rte_eth_dev_info port_info;
58 	struct softnic_link *link;
59 	uint16_t port_id;
60 	int ret;
61 
62 	/* Check input params */
63 	if (name == NULL ||
64 		softnic_link_find(p, name) ||
65 		params == NULL)
66 		return NULL;
67 
68 	port_id = params->port_id;
69 	if (params->dev_name) {
70 		int status;
71 
72 		status = rte_eth_dev_get_port_by_name(params->dev_name,
73 			&port_id);
74 
75 		if (status)
76 			return NULL;
77 	} else {
78 		if (!rte_eth_dev_is_valid_port(port_id))
79 			return NULL;
80 	}
81 
82 	ret = rte_eth_dev_info_get(port_id, &port_info);
83 	if (ret != 0)
84 		return NULL;
85 
86 	/* Node allocation */
87 	link = calloc(1, sizeof(struct softnic_link));
88 	if (link == NULL)
89 		return NULL;
90 
91 	/* Node fill in */
92 	strlcpy(link->name, name, sizeof(link->name));
93 	link->port_id = port_id;
94 	link->n_rxq = port_info.nb_rx_queues;
95 	link->n_txq = port_info.nb_tx_queues;
96 
97 	/* Node add to list */
98 	TAILQ_INSERT_TAIL(&p->link_list, link, node);
99 
100 	return link;
101 }
102