aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/bonding/rte_eth_bond_flow.c
blob: 31e4bcaeb3470ba359f935c9c8938daa6ef9441c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright 2018 Mellanox Technologies, Ltd
 */

#include <sys/queue.h>

#include <rte_malloc.h>
#include <rte_tailq.h>
#include <rte_flow.h>

#include "rte_eth_bond_private.h"

static struct rte_flow *
bond_flow_alloc(int numa_node, const struct rte_flow_attr *attr,
		   const struct rte_flow_item *items,
		   const struct rte_flow_action *actions)
{
	struct rte_flow *flow;
	size_t fdsz;

	fdsz = rte_flow_copy(NULL, 0, attr, items, actions);
	flow = rte_zmalloc_socket(NULL, sizeof(struct rte_flow) + fdsz,
				  RTE_CACHE_LINE_SIZE, numa_node);
	if (unlikely(flow == NULL)) {
		RTE_BOND_LOG(ERR, "Could not allocate new flow");
		return NULL;
	}
	flow->fd = (void *)((uintptr_t)flow + sizeof(*flow));
	if (unlikely(rte_flow_copy(flow->fd, fdsz, attr, items, actions) !=
		     fdsz)) {
		RTE_BOND_LOG(ERR, "Failed to copy flow description");
		rte_free(flow);
		return NULL;
	}
	return flow;
}

static void
bond_flow_release(struct rte_flow **flow)
{
	rte_free(*flow);
	*flow = NULL;
}

static int
bond_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
		   const struct rte_flow_item patterns[],
		   const struct rte_flow_action actions[],
		   struct rte_flow_error *err)
{
	struct bond_dev_private *internals = dev->data->dev_private;
	int i;
	int ret;

	for (i = 0; i < internals->slave_count; i++) {
		ret = rte_flow_validate(internals->slaves[i].port_id, attr,
					patterns, actions, err);
		if (ret) {
			RTE_BOND_LOG(ERR, "Operation rte_flow_validate failed"
				     " for slave %d with error %d", i, ret);
			return ret;
		}
	}
	return 0;
}

static struct rte_flow *
bond_flow_create(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
		 const struct rte_flow_item patterns[],
		 const struct rte_flow_action actions[],
		 struct rte_flow_error *err)
{
	struct bond_dev_private *internals = dev->data->dev_private;
	struct rte_flow *flow;
	int i;

	flow = bond_flow_alloc(dev->data->numa_node, attr, patterns, actions);
	if (unlikely(flow == NULL)) {
		rte_flow_error_set(err, ENOMEM, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
				   NULL, rte_strerror(ENOMEM));
		return NULL;
	}
	for (i = 0; i < internals->slave_count; i++) {
		flow->flows[i] = rte_flow_create(internals->slaves[i].port_id,
						 attr, patterns, actions, err);
		if (unlikely(flow->flows[i] == NULL)) {
			RTE_BOND_LOG(ERR, "Failed to create flow on slave %d",
				     i);
			goto err;
		}
	}
	TAILQ_INSERT_TAIL(&internals->flow_list, flow, next);
	return flow;
err:
	/* Destroy all slaves flows. */
	for (i = 0; i < internals->slave_count; i++) {
		if (flow->flows[i] != NULL)
			rte_flow_destroy(internals->slaves[i].port_id,
					 flow->flows[i], err);
	}
	bond_flow_release(&flow);
	return NULL;
}

static int
bond_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
		  struct rte_flow_error *err)
{
	struct bond_dev_private *internals = dev->data->dev_private;
	int i;
	int ret = 0;

	for (i = 0; i < internals->slave_count; i++) {
		int lret;

		if (unlikely(flow->flows[i] == NULL))
			continue;
		lret = rte_flow_destroy(internals->slaves[i].port_id,
					flow->flows[i], err);
		if (unlikely(lret != 0)) {
			RTE_BOND_LOG(ERR, "Failed to destroy flow on slave %d:"
				     " %d", i, lret);
			ret = lret;
		}
	}
	TAILQ_REMOVE(&internals->flow_list, flow, next);
	bond_flow_release(&flow);
	return ret;
}

static int
bond_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *err)
{
	struct bond_dev_private *internals = dev->data->dev_private;
	struct rte_flow *flow;
	void *tmp;
	int ret = 0;
	int lret;

	/* Destroy all bond flows from its slaves instead of flushing them to
	 * keep the LACP flow or any other external flows.
	 */
	TAILQ_FOREACH_SAFE(flow, &internals->flow_list, next, tmp) {
		lret = bond_flow_destroy(dev, flow, err);
		if (unlikely(lret != 0))
			ret = lret;
	}
	if (unlikely(ret != 0))
		RTE_BOND_LOG(ERR, "Failed to flush flow in all slaves");
	return ret;
}

static int
bond_flow_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
		      const struct rte_flow_action *action,
		      struct rte_flow_query_count *count,
		      struct rte_flow_error *err)
{
	struct bond_dev_private *internals = dev->data->dev_private;
	struct rte_flow_query_count slave_count;
	int i;
	int ret;

	count->bytes = 0;
	count->hits = 0;
	rte_memcpy(&slave_count, count, sizeof(slave_count));
	for (i = 0; i < internals->slave_count; i++) {
		ret = rte_flow_query(internals->slaves[i].port_id,
				     flow->flows[i], action,
				     &slave_count, err);
		if (unlikely(ret != 0)) {
			RTE_BOND_LOG(ERR, "Failed to query flow on"
				     " slave %d: %d", i, ret);
			return ret;
		}
		count->bytes += slave_count.bytes;
		count->hits += slave_count.hits;
		slave_count.bytes = 0;
		slave_count.hits = 0;
	}
	return 0;
}

static int
bond_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
		const struct rte_flow_action *action, void *arg,
		struct rte_flow_error *err)
{
	switch (action->type) {
	case RTE_FLOW_ACTION_TYPE_COUNT:
		return bond_flow_query_count(dev, flow, action, arg, err);
	default:
		return rte_flow_error_set(err, ENOTSUP,
					  RTE_FLOW_ERROR_TYPE_ACTION, arg,
					  rte_strerror(ENOTSUP));
	}
}

static int
bond_flow_isolate(struct rte_eth_dev *dev, int set,
		  struct rte_flow_error *err)
{
	struct bond_dev_private *internals = dev->data->dev_private;
	int i;
	int ret;

	for (i = 0; i < internals->slave_count; i++) {
		ret = rte_flow_isolate(internals->slaves[i].port_id, set, err);
		if (unlikely(ret != 0)) {
			RTE_BOND_LOG(ERR, "Operation rte_flow_isolate failed"
				     " for slave %d with error %d", i, ret);
			internals->flow_isolated_valid = 0;
			return ret;
		}
	}
	internals->flow_isolated = set;
	internals->flow_isolated_valid = 1;
	return 0;
}

const struct rte_flow_ops bond_flow_ops = {
	.validate = bond_flow_validate,
	.create = bond_flow_create,
	.destroy = bond_flow_destroy,
	.flush = bond_flow_flush,
	.query = bond_flow_query,
	.isolate = bond_flow_isolate,
};