acc0f79d53ac7f899a9914a41df61f747952b605
[dpdk.git] / drivers / net / softnic / rte_eth_softnic_action.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <rte_string_fns.h>
10
11 #include "hash_func.h"
12 #include "rte_eth_softnic_internals.h"
13
14 /**
15  * Input port
16  */
17 int
18 softnic_port_in_action_profile_init(struct pmd_internals *p)
19 {
20         TAILQ_INIT(&p->port_in_action_profile_list);
21
22         return 0;
23 }
24
25 void
26 softnic_port_in_action_profile_free(struct pmd_internals *p)
27 {
28         for ( ; ; ) {
29                 struct softnic_port_in_action_profile *profile;
30
31                 profile = TAILQ_FIRST(&p->port_in_action_profile_list);
32                 if (profile == NULL)
33                         break;
34
35                 TAILQ_REMOVE(&p->port_in_action_profile_list, profile, node);
36                 free(profile);
37         }
38 }
39
40 struct softnic_port_in_action_profile *
41 softnic_port_in_action_profile_find(struct pmd_internals *p,
42         const char *name)
43 {
44         struct softnic_port_in_action_profile *profile;
45
46         if (name == NULL)
47                 return NULL;
48
49         TAILQ_FOREACH(profile, &p->port_in_action_profile_list, node)
50                 if (strcmp(profile->name, name) == 0)
51                         return profile;
52
53         return NULL;
54 }
55
56 struct softnic_port_in_action_profile *
57 softnic_port_in_action_profile_create(struct pmd_internals *p,
58         const char *name,
59         struct softnic_port_in_action_profile_params *params)
60 {
61         struct softnic_port_in_action_profile *profile;
62         struct rte_port_in_action_profile *ap;
63         int status;
64
65         /* Check input params */
66         if (name == NULL ||
67                 softnic_port_in_action_profile_find(p, name) ||
68                 params == NULL)
69                 return NULL;
70
71         if ((params->action_mask & (1LLU << RTE_PORT_IN_ACTION_LB)) &&
72                 params->lb.f_hash == NULL) {
73                 switch (params->lb.key_size) {
74                 case  8:
75                         params->lb.f_hash = hash_default_key8;
76                         break;
77
78                 case 16:
79                         params->lb.f_hash = hash_default_key16;
80                         break;
81
82                 case 24:
83                         params->lb.f_hash = hash_default_key24;
84                         break;
85
86                 case 32:
87                         params->lb.f_hash = hash_default_key32;
88                         break;
89
90                 case 40:
91                         params->lb.f_hash = hash_default_key40;
92                         break;
93
94                 case 48:
95                         params->lb.f_hash = hash_default_key48;
96                         break;
97
98                 case 56:
99                         params->lb.f_hash = hash_default_key56;
100                         break;
101
102                 case 64:
103                         params->lb.f_hash = hash_default_key64;
104                         break;
105
106                 default:
107                         return NULL;
108                 }
109
110                 params->lb.seed = 0;
111         }
112
113         /* Resource */
114         ap = rte_port_in_action_profile_create(0);
115         if (ap == NULL)
116                 return NULL;
117
118         if (params->action_mask & (1LLU << RTE_PORT_IN_ACTION_FLTR)) {
119                 status = rte_port_in_action_profile_action_register(ap,
120                         RTE_PORT_IN_ACTION_FLTR,
121                         &params->fltr);
122
123                 if (status) {
124                         rte_port_in_action_profile_free(ap);
125                         return NULL;
126                 }
127         }
128
129         if (params->action_mask & (1LLU << RTE_PORT_IN_ACTION_LB)) {
130                 status = rte_port_in_action_profile_action_register(ap,
131                         RTE_PORT_IN_ACTION_LB,
132                         &params->lb);
133
134                 if (status) {
135                         rte_port_in_action_profile_free(ap);
136                         return NULL;
137                 }
138         }
139
140         status = rte_port_in_action_profile_freeze(ap);
141         if (status) {
142                 rte_port_in_action_profile_free(ap);
143                 return NULL;
144         }
145
146         /* Node allocation */
147         profile = calloc(1, sizeof(struct softnic_port_in_action_profile));
148         if (profile == NULL) {
149                 rte_port_in_action_profile_free(ap);
150                 return NULL;
151         }
152
153         /* Node fill in */
154         strlcpy(profile->name, name, sizeof(profile->name));
155         memcpy(&profile->params, params, sizeof(*params));
156         profile->ap = ap;
157
158         /* Node add to list */
159         TAILQ_INSERT_TAIL(&p->port_in_action_profile_list, profile, node);
160
161         return profile;
162 }