eal/bsd: add interrupt thread
[dpdk.git] / drivers / net / bonding / rte_eth_bond_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 Mellanox Technologies, Ltd
3  */
4
5 #include <sys/queue.h>
6
7 #include <rte_malloc.h>
8 #include <rte_tailq.h>
9 #include <rte_flow.h>
10
11 #include "rte_eth_bond_private.h"
12
13 static struct rte_flow *
14 bond_flow_alloc(int numa_node, const struct rte_flow_attr *attr,
15                    const struct rte_flow_item *items,
16                    const struct rte_flow_action *actions)
17 {
18         struct rte_flow *flow;
19         size_t fdsz;
20
21         fdsz = rte_flow_copy(NULL, 0, attr, items, actions);
22         flow = rte_zmalloc_socket(NULL, sizeof(struct rte_flow) + fdsz,
23                                   RTE_CACHE_LINE_SIZE, numa_node);
24         if (unlikely(flow == NULL)) {
25                 RTE_BOND_LOG(ERR, "Could not allocate new flow");
26                 return NULL;
27         }
28         flow->fd = (void *)((uintptr_t)flow + sizeof(*flow));
29         if (unlikely(rte_flow_copy(flow->fd, fdsz, attr, items, actions) !=
30                      fdsz)) {
31                 RTE_BOND_LOG(ERR, "Failed to copy flow description");
32                 rte_free(flow);
33                 return NULL;
34         }
35         return flow;
36 }
37
38 static void
39 bond_flow_release(struct rte_flow **flow)
40 {
41         rte_free(*flow);
42         *flow = NULL;
43 }
44
45 static int
46 bond_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
47                    const struct rte_flow_item patterns[],
48                    const struct rte_flow_action actions[],
49                    struct rte_flow_error *err)
50 {
51         struct bond_dev_private *internals = dev->data->dev_private;
52         int i;
53         int ret;
54
55         for (i = 0; i < internals->slave_count; i++) {
56                 ret = rte_flow_validate(internals->slaves[i].port_id, attr,
57                                         patterns, actions, err);
58                 if (ret) {
59                         RTE_BOND_LOG(ERR, "Operation rte_flow_validate failed"
60                                      " for slave %d with error %d", i, ret);
61                         return ret;
62                 }
63         }
64         return 0;
65 }
66
67 static struct rte_flow *
68 bond_flow_create(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
69                  const struct rte_flow_item patterns[],
70                  const struct rte_flow_action actions[],
71                  struct rte_flow_error *err)
72 {
73         struct bond_dev_private *internals = dev->data->dev_private;
74         struct rte_flow *flow;
75         int i;
76
77         flow = bond_flow_alloc(dev->data->numa_node, attr, patterns, actions);
78         if (unlikely(flow == NULL)) {
79                 rte_flow_error_set(err, ENOMEM, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
80                                    NULL, rte_strerror(ENOMEM));
81                 return NULL;
82         }
83         for (i = 0; i < internals->slave_count; i++) {
84                 flow->flows[i] = rte_flow_create(internals->slaves[i].port_id,
85                                                  attr, patterns, actions, err);
86                 if (unlikely(flow->flows[i] == NULL)) {
87                         RTE_BOND_LOG(ERR, "Failed to create flow on slave %d",
88                                      i);
89                         goto err;
90                 }
91         }
92         TAILQ_INSERT_TAIL(&internals->flow_list, flow, next);
93         return flow;
94 err:
95         /* Destroy all slaves flows. */
96         for (i = 0; i < internals->slave_count; i++) {
97                 if (flow->flows[i] != NULL)
98                         rte_flow_destroy(internals->slaves[i].port_id,
99                                          flow->flows[i], err);
100         }
101         bond_flow_release(&flow);
102         return NULL;
103 }
104
105 static int
106 bond_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
107                   struct rte_flow_error *err)
108 {
109         struct bond_dev_private *internals = dev->data->dev_private;
110         int i;
111         int ret = 0;
112
113         for (i = 0; i < internals->slave_count; i++) {
114                 int lret;
115
116                 if (unlikely(flow->flows[i] == NULL))
117                         continue;
118                 lret = rte_flow_destroy(internals->slaves[i].port_id,
119                                         flow->flows[i], err);
120                 if (unlikely(lret != 0)) {
121                         RTE_BOND_LOG(ERR, "Failed to destroy flow on slave %d:"
122                                      " %d", i, lret);
123                         ret = lret;
124                 }
125         }
126         TAILQ_REMOVE(&internals->flow_list, flow, next);
127         bond_flow_release(&flow);
128         return ret;
129 }
130
131 static int
132 bond_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *err)
133 {
134         struct bond_dev_private *internals = dev->data->dev_private;
135         struct rte_flow *flow;
136         void *tmp;
137         int ret = 0;
138         int lret;
139
140         /* Destroy all bond flows from its slaves instead of flushing them to
141          * keep the LACP flow or any other external flows.
142          */
143         TAILQ_FOREACH_SAFE(flow, &internals->flow_list, next, tmp) {
144                 lret = bond_flow_destroy(dev, flow, err);
145                 if (unlikely(lret != 0))
146                         ret = lret;
147         }
148         if (unlikely(ret != 0))
149                 RTE_BOND_LOG(ERR, "Failed to flush flow in all slaves");
150         return ret;
151 }
152
153 static int
154 bond_flow_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
155                       const struct rte_flow_action *action,
156                       struct rte_flow_query_count *count,
157                       struct rte_flow_error *err)
158 {
159         struct bond_dev_private *internals = dev->data->dev_private;
160         struct rte_flow_query_count slave_count;
161         int i;
162         int ret;
163
164         count->bytes = 0;
165         count->hits = 0;
166         rte_memcpy(&slave_count, count, sizeof(slave_count));
167         for (i = 0; i < internals->slave_count; i++) {
168                 ret = rte_flow_query(internals->slaves[i].port_id,
169                                      flow->flows[i], action,
170                                      &slave_count, err);
171                 if (unlikely(ret != 0)) {
172                         RTE_BOND_LOG(ERR, "Failed to query flow on"
173                                      " slave %d: %d", i, ret);
174                         return ret;
175                 }
176                 count->bytes += slave_count.bytes;
177                 count->hits += slave_count.hits;
178                 slave_count.bytes = 0;
179                 slave_count.hits = 0;
180         }
181         return 0;
182 }
183
184 static int
185 bond_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
186                 const struct rte_flow_action *action, void *arg,
187                 struct rte_flow_error *err)
188 {
189         switch (action->type) {
190         case RTE_FLOW_ACTION_TYPE_COUNT:
191                 return bond_flow_query_count(dev, flow, action, arg, err);
192         default:
193                 return rte_flow_error_set(err, ENOTSUP,
194                                           RTE_FLOW_ERROR_TYPE_ACTION, arg,
195                                           rte_strerror(ENOTSUP));
196         }
197 }
198
199 static int
200 bond_flow_isolate(struct rte_eth_dev *dev, int set,
201                   struct rte_flow_error *err)
202 {
203         struct bond_dev_private *internals = dev->data->dev_private;
204         int i;
205         int ret;
206
207         for (i = 0; i < internals->slave_count; i++) {
208                 ret = rte_flow_isolate(internals->slaves[i].port_id, set, err);
209                 if (unlikely(ret != 0)) {
210                         RTE_BOND_LOG(ERR, "Operation rte_flow_isolate failed"
211                                      " for slave %d with error %d", i, ret);
212                         internals->flow_isolated_valid = 0;
213                         return ret;
214                 }
215         }
216         internals->flow_isolated = set;
217         internals->flow_isolated_valid = 1;
218         return 0;
219 }
220
221 const struct rte_flow_ops bond_flow_ops = {
222         .validate = bond_flow_validate,
223         .create = bond_flow_create,
224         .destroy = bond_flow_destroy,
225         .flush = bond_flow_flush,
226         .query = bond_flow_query,
227         .isolate = bond_flow_isolate,
228 };