summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_u32_parse.h
blob: f59dd4b2ae6f970b6894924759832be6acb772f8 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * This file is part of the Chelsio T4 Ethernet driver for Linux.
 *
 * Copyright (c) 2016 Chelsio Communications, Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __CXGB4_TC_U32_PARSE_H
#define __CXGB4_TC_U32_PARSE_H

struct cxgb4_match_field {
	int off; /* Offset from the beginning of the header to match */
	/* Fill the value/mask pair in the spec if matched */
	int (*val)(struct ch_filter_specification *f, __be32 val, __be32 mask);
};

/* IPv4 match fields */
static inline int cxgb4_fill_ipv4_tos(struct ch_filter_specification *f,
				      __be32 val, __be32 mask)
{
	f->val.tos  = (ntohl(val)  >> 16) & 0x000000FF;
	f->mask.tos = (ntohl(mask) >> 16) & 0x000000FF;

	return 0;
}

static inline int cxgb4_fill_ipv4_frag(struct ch_filter_specification *f,
				       __be32 val, __be32 mask)
{
	u32 mask_val;
	u8 frag_val;

	frag_val = (ntohl(val) >> 13) & 0x00000007;
	mask_val = ntohl(mask) & 0x0000FFFF;

	if (frag_val == 0x1 && mask_val != 0x3FFF) { /* MF set */
		f->val.frag = 1;
		f->mask.frag = 1;
	} else if (frag_val == 0x2 && mask_val != 0x3FFF) { /* DF set */
		f->val.frag = 0;
		f->mask.frag = 1;
	} else {
		return -EINVAL;
	}

	return 0;
}

static inline int cxgb4_fill_ipv4_proto(struct ch_filter_specification *f,
					__be32 val, __be32 mask)
{
	f->val.proto  = (ntohl(val)  >> 16) & 0x000000FF;
	f->mask.proto = (ntohl(mask) >> 16) & 0x000000FF;

	return 0;
}

static inline int cxgb4_fill_ipv4_src_ip(struct ch_filter_specification *f,
					 __be32 val, __be32 mask)
{
	memcpy(&f->val.fip[0],  &val,  sizeof(u32));
	memcpy(&f->mask.fip[0], &mask, sizeof(u32));

	return 0;
}

static inline int cxgb4_fill_ipv4_dst_ip(struct ch_filter_specification *f,
					 __be32 val, __be32 mask)
{
	memcpy(&f->val.lip[0],  &val,  sizeof(u32));
	memcpy(&f->mask.lip[0], &mask, sizeof(u32));

	return 0;
}

static const struct cxgb4_match_field cxgb4_ipv4_fields[] = {
	{ .off = 0,  .val = cxgb4_fill_ipv4_tos },
	{ .off = 4,  .val = cxgb4_fill_ipv4_frag },
	{ .off = 8,  .val = cxgb4_fill_ipv4_proto },
	{ .off = 12, .val = cxgb4_fill_ipv4_src_ip },
	{ .off = 16, .val = cxgb4_fill_ipv4_dst_ip },
	{ .val = NULL }
};

/* IPv6 match fields */
static inline int cxgb4_fill_ipv6_tos(struct ch_filter_specification *f,
				      __be32 val, __be32 mask)
{
	f->val.tos  = (ntohl(val)  >> 20) & 0x000000FF;
	f->mask.tos = (ntohl(mask) >> 20) & 0x000000FF;

	return 0;
}

static inline int cxgb4_fill_ipv6_proto(struct ch_filter_specification *f,
					__be32 val, __be32 mask)
{
	f->val.proto  = (ntohl(val)  >> 8) & 0x000000FF;
	f->mask.proto = (ntohl(mask) >> 8) & 0x000000FF;

	return 0;
}

static inline int cxgb4_fill_ipv6_src_ip0(struct ch_filter_specification *f,
					  __be32 val, __be32 mask)
{
	memcpy(&f->val.fip[0],  &val,  sizeof(u32));
	memcpy(&f->mask.fip[0], &mask, sizeof(u32));

	return 0;
}

static inline int cxgb4_fill_ipv6_src_ip1(struct ch_filter_specification *f,
					  __be32 val, __be32 mask)
{
	memcpy(&f->val.fip[4],  &val,  sizeof(u32));
	memcpy(&f->mask.fip[4], &mask, sizeof(u32));

	return 0;
}

static inline int cxgb4_fill_ipv6_src_ip2(struct ch_filter_specification *f,
					  __be32 val, __be32 mask)
{
	memcpy(&f->val.fip[8],  &val,  sizeof(u32));
	memcpy(&f->mask.fip[8], &mask, sizeof(u32));

	return 0;
}

static inline int cxgb4_fill_ipv6_src_ip3(struct ch_filter_specification *f,
					  __be32 val, __be32 mask)
{
	memcpy(&f->val.fip[12],  &val,  sizeof(u32));
	memcpy(&f->mask.fip[12], &mask, sizeof(u32));

	return 0;
}

static inline int cxgb4_fill_ipv6_dst_ip0(struct ch_filter_specification *f,
					  __be32 val, __be32 mask)
{
	memcpy(&f->val.lip[0],  &val,  sizeof(u32));
	memcpy(&f->mask.lip[0], &mask, sizeof(u32));

	return 0;
}

static inline int cxgb4_fill_ipv6_dst_ip1(struct ch_filter_specification *f,
					  __be32 val, __be32 mask)
{
	memcpy(&f->val.lip[4],  &val,  sizeof(u32));
	memcpy(&f->mask.lip[4], &mask, sizeof(u32));

	return 0;
}

static inline int cxgb4_fill_ipv6_dst_ip2(struct ch_filter_specification *f,
					  __be32 val, __be32 mask)
{
	memcpy(&f->val.lip[8],  &val,  sizeof(u32));
	memcpy(&f->mask.lip[8], &mask, sizeof(u32));

	return 0;
}

static inline int cxgb4_fill_ipv6_dst_ip3(struct ch_filter_specification *f,
					  __be32 val, __be32 mask)
{
	memcpy(&f->val.lip[12],  &val,  sizeof(u32));
	memcpy(&f->mask.lip[12], &mask, sizeof(u32));

	return 0;
}

static const struct cxgb4_match_field cxgb4_ipv6_fields[] = {
	{ .off = 0,  .val = cxgb4_fill_ipv6_tos },
	{ .off = 4,  .val = cxgb4_fill_ipv6_proto },
	{ .off = 8,  .val = cxgb4_fill_ipv6_src_ip0 },
	{ .off = 12, .val = cxgb4_fill_ipv6_src_ip1 },
	{ .off = 16, .val = cxgb4_fill_ipv6_src_ip2 },
	{ .off = 20, .val = cxgb4_fill_ipv6_src_ip3 },
	{ .off = 24, .val = cxgb4_fill_ipv6_dst_ip0 },
	{ .off = 28, .val = cxgb4_fill_ipv6_dst_ip1 },
	{ .off = 32, .val = cxgb4_fill_ipv6_dst_ip2 },
	{ .off = 36, .val = cxgb4_fill_ipv6_dst_ip3 },
	{ .val = NULL }
};

/* TCP/UDP match */
static inline int cxgb4_fill_l4_ports(struct ch_filter_specification *f,
				      __be32 val, __be32 mask)
{
	f->val.fport  = ntohl(val)  >> 16;
	f->mask.fport = ntohl(mask) >> 16;
	f->val.lport  = ntohl(val)  & 0x0000FFFF;
	f->mask.lport = ntohl(mask) & 0x0000FFFF;

	return 0;
};

static const struct cxgb4_match_field cxgb4_tcp_fields[] = {
	{ .off = 0, .val = cxgb4_fill_l4_ports },
	{ .val = NULL }
};

static const struct cxgb4_match_field cxgb4_udp_fields[] = {
	{ .off = 0, .val = cxgb4_fill_l4_ports },
	{ .val = NULL }
};

struct cxgb4_next_header {
	/* Offset, shift, and mask added to beginning of the header
	 * to get to next header.  Useful when using a header
	 * field's value to jump to next header such as IHL field
	 * in IPv4 header.
	 */
	struct tc_u32_sel sel;
	struct tc_u32_key key;
	/* location of jump to make */
	const struct cxgb4_match_field *jump;
};

/* Accept a rule with a jump to transport layer header based on IHL field in
 * IPv4 header.
 */
static const struct cxgb4_next_header cxgb4_ipv4_jumps[] = {
	{
		/* TCP Jump */
		.sel = {
			.off = 0,
			.offoff = 0,
			.offshift = 6,
			.offmask = cpu_to_be16(0x0f00),
		},
		.key = {
			.off = 8,
			.val = cpu_to_be32(0x00060000),
			.mask = cpu_to_be32(0x00ff0000),
		},
		.jump = cxgb4_tcp_fields,
	},
	{
		/* UDP Jump */
		.sel = {
			.off = 0,
			.offoff = 0,
			.offshift = 6,
			.offmask = cpu_to_be16(0x0f00),
		},
		.key = {
			.off = 8,
			.val = cpu_to_be32(0x00110000),
			.mask = cpu_to_be32(0x00ff0000),
		},
		.jump = cxgb4_udp_fields,
	},
	{ .jump = NULL },
};

/* Accept a rule with a jump directly past the 40 Bytes of IPv6 fixed header
 * to get to transport layer header.
 */
static const struct cxgb4_next_header cxgb4_ipv6_jumps[] = {
	{
		/* TCP Jump */
		.sel = {
			.off = 40,
			.offoff = 0,
			.offshift = 0,
			.offmask = 0,
		},
		.key = {
			.off = 4,
			.val = cpu_to_be32(0x00000600),
			.mask = cpu_to_be32(0x0000ff00),
		},
		.jump = cxgb4_tcp_fields,
	},
	{
		/* UDP Jump */
		.sel = {
			.off = 40,
			.offoff = 0,
			.offshift = 0,
			.offmask = 0,
		},
		.key = {
			.off = 4,
			.val = cpu_to_be32(0x00001100),
			.mask = cpu_to_be32(0x0000ff00),
		},
		.jump = cxgb4_udp_fields,
	},
	{ .jump = NULL },
};

struct cxgb4_link {
	const struct cxgb4_match_field *match_field;  /* Next header */
	struct ch_filter_specification fs; /* Match spec associated with link */
	u32 link_handle;         /* Knode handle associated with the link */
	unsigned long *tid_map;  /* Bitmap for filter tids */
};

struct cxgb4_tc_u32_table {
	unsigned int size;          /* number of entries in table */
	struct cxgb4_link table[]; /* Jump table */
};
#endif /* __CXGB4_TC_U32_PARSE_H */