New upstream version 18.08
[deb_dpdk.git] / drivers / net / softnic / rte_eth_softnic_link.c
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
14 softnic_link_init(struct pmd_internals *p)
15 {
16         TAILQ_INIT(&p->link_list);
17
18         return 0;
19 }
20
21 void
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 *
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 *
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
61         /* Check input params */
62         if (name == NULL ||
63                 softnic_link_find(p, name) ||
64                 params == NULL)
65                 return NULL;
66
67         port_id = params->port_id;
68         if (params->dev_name) {
69                 int status;
70
71                 status = rte_eth_dev_get_port_by_name(params->dev_name,
72                         &port_id);
73
74                 if (status)
75                         return NULL;
76         } else {
77                 if (!rte_eth_dev_is_valid_port(port_id))
78                         return NULL;
79         }
80
81         rte_eth_dev_info_get(port_id, &port_info);
82
83         /* Node allocation */
84         link = calloc(1, sizeof(struct softnic_link));
85         if (link == NULL)
86                 return NULL;
87
88         /* Node fill in */
89         strlcpy(link->name, name, sizeof(link->name));
90         link->port_id = port_id;
91         link->n_rxq = port_info.nb_rx_queues;
92         link->n_txq = port_info.nb_tx_queues;
93
94         /* Node add to list */
95         TAILQ_INSERT_TAIL(&p->link_list, link, node);
96
97         return link;
98 }