summaryrefslogtreecommitdiff
path: root/match.c
blob: 31bdb5f8a10ec5f5436ad21d8211730eaf448878 (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
/* PIPAPO - PIle PAcket POlicies
 *
 * match.c - Equivalent match implementation for kernel
 *
 * Author: Stefano Brivio <sbrivio@redhat.com>
 * License: GPLv2
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "pipapo.h"
#include "util.h"
#include "set.h"
#include "match.h"

#ifdef MATCH_AVX2
#include "avx2.h"
#endif

/**
 * match_init() - Initialise equivalent of matching data representation
 * @s:		Set data
 * @layout:	Set layout
 *
 * This conceptually corresponds to the creation of in-kernel matching data.
 *
 * Return: 0 on success, NULL on failure
 */
struct kernel_set *kernel_init(struct set *s, struct desc_spec **layout)
{
	struct kernel_set *ks;
	struct field *f;
	int i;

	ks = calloc(sizeof(*ks), 1);
	if (!ks)
		return NULL;

	for_each_field(f, s, layout) {
		ks->offset[i] = f->offset;
		ks->groups[i] = f->groups;
		ks->bsize[i] = f->bsize;
#ifdef MATCH_AVX2
		ks->lt[i] = aligned_alloc(32, f->groups * f->bsize * BUCKETS);
#else
		ks->lt[i] = malloc(f->groups * f->bsize * BUCKETS);
#endif
		memcpy(ks->lt[i], f->lt, f->groups * f->bsize * BUCKETS);

#ifdef MATCH_CTZL
		ks->mt[i] = aligned_alloc(8, f->rules * sizeof(*ks->mt[i]));
#else
		ks->mt[i] = malloc(f->rules * sizeof(*ks->mt[i]));
#endif
		memcpy(ks->mt[i], f->mt, f->rules * sizeof(*ks->mt[i]));

		if (f->bsize > ks->max_bsize)
			ks->max_bsize = f->bsize;
	}
	ks->groups[i] = 0;

#ifdef MATCH_AVX2
	ks->map[0] = aligned_alloc(32, round_up(ks->max_bsize, 32));
	ks->map[1] = aligned_alloc(32, round_up(ks->max_bsize, 32));
	memset(ks->map[0], 0, ks->max_bsize);
	memset(ks->map[1], 0, ks->max_bsize);
#else
	ks->map[0] = calloc(ks->max_bsize, 1);
	ks->map[1] = calloc(ks->max_bsize, 1);
#endif

	return ks;
}

#if defined(VERBOSE) && !defined(MATCH_UNROLLED) && !defined(MATCH_AVX2)
/**
 * show_match() - Show bucket and bitmap result for a single matching step
 * @res:	Resulting bitmap to be displayed
 * @group_lt:	Pointer to lookup table row for current bit group
 * @v:		Value of packet bytes for current group
 * @bsize:	Lookup bucket size, in bytes
 *
 * For documentation purposes only: this shows algorithm step 4.3 in detail.
 */
static void show_match(uint8_t *res, uint8_t *group_lt, int v, int bsize)
{
	uint8_t *bucket = group_lt + v * bsize;
	int i;

	fprintf(stdout, "  bucket: ");
	for (i = 0; i < bsize; i++)
		fprintf(stdout, "%02x ", bucket[i]);
	fprintf(stdout, "(value: %i)\n", v);

	fprintf(stdout, "  result: ");
	for (i = 0; i < bsize; i++)
		fprintf(stdout, "%02x ", res[i]);
	fprintf(stdout, "\n\n");
}
#else
#define show_match(...) do { } while (0)
#endif

#ifdef VERBOSE
/**
 * show_field() - Show field packet bytes and initial bitmap for given field
 * @f:		Index of current field in set
 * @pkt:	Packet bytes for this field
 * @init:	Initial bitmap
 * @bsize:	Lookup bucket size, in bytes
 *
 * For documentation purposes only: this shows algorithm steps 4.1 and 4.2.
 */
static void show_field(int f, uint8_t *pkt, int len, uint8_t *init, int bsize)
{
	int i;

	fprintf(stdout, "\nField %i, packet bytes:", f);
	for (i = 0; i < len; i++)
		fprintf(stdout, " %02x", pkt[i]);

	fprintf(stdout, ", initial bitmap:");
	for (i = 0; i < bsize; i++)
		fprintf(stdout, " %02x", init[i]);
	fprintf(stdout, "\n\n");
}
#else
#define show_field(...) do { } while (0)
#endif

/* Provide explicitly unrolled versions for lookup steps: the compiler doesn't
 * know that only some group sizes make sense. This speeds up non-AVX2 matching.
 */
#define MATCH_REPEAT_4(x)						       \
	andmem(map[idx], lt + (x +  0 + (*pkt_p >> 4)) * ks->bsize[f],	       \
	       ks->bsize[f]);						       \
	andmem(map[idx], lt + (x + 16 + (*pkt_p & 0x0f)) * ks->bsize[f],       \
	       ks->bsize[f]);						       \
	pkt_p++;							       \
	andmem(map[idx], lt + (x + 32 + (*pkt_p >> 4)) * ks->bsize[f],	       \
	       ks->bsize[f]);						       \
	andmem(map[idx], lt + (x + 48 + (*pkt_p & 0x0f)) * ks->bsize[f],       \
	       ks->bsize[f]);						       \
	pkt_p++;

#define MATCH_REPEAT_8(x)						\
	MATCH_REPEAT_4(x)						\
	MATCH_REPEAT_4(x + 64)

#define MATCH_REPEAT_12							\
	MATCH_REPEAT_8(0)						\
	MATCH_REPEAT_4(128)

#define MATCH_REPEAT_32							\
	MATCH_REPEAT_8(0)						\
	MATCH_REPEAT_8(128)						\
	MATCH_REPEAT_8(256)						\
	MATCH_REPEAT_8(384)

/**
 * match() - Equivalent of in-kernel matching implementation
 * @ks:		Kernel representation of set data
 * @packet:	Packet bytes
 *
 * This conceptually corresponds to the in-kernel matching function, and it
 * implements algorithm steps 4.1 to 4.5.
 *
 * Return: matched key if any, 0 otherwise
 */
uint32_t match(struct kernel_set *ks, uint8_t *packet)
{
	int f, g, b, match = 0, offset, idx = ks->map_idx;
	uint8_t *pkt_p, *lt, v, **map = ks->map;

	(void)g;
	(void)v;
	(void)offset;
	(void)match;

#ifndef MATCH_AVX2
	/* AVX2 implementation loads all matching buckets first, so an explicit
	 * initial all-ones bitmap isn't needed.
	 */
	memset(map[idx], 0xff, ks->max_bsize);
#endif

	/* Go through each set field */
	for (f = 0; ks->groups[f]; f++) {
		lt = ks->lt[f];
		pkt_p = packet + ks->offset[f];

		show_field(f, pkt_p, ks->groups[f] / 2, map[idx], ks->bsize[f]);

		/* For each 4-bit group, select lookup table bucket depending on
		 * packet bytes value, AND bucket values (steps 4.1 - 4.3).
		 */
#ifdef MATCH_UNROLLED
		if (ks->groups[f] == 4) {
			MATCH_REPEAT_4(0);
		} else if (ks->groups[f] == 8) {
			MATCH_REPEAT_8(0);
		} else if (ks->groups[f] == 12) {
			MATCH_REPEAT_12;
		} else if (ks->groups[f] == 32) {
			MATCH_REPEAT_32;
		}
#elif defined(MATCH_AVX2)
		match = avx2_lookup(map[idx], lt, pkt_p, ks->groups[f],
				    ks->bsize[f], f == 0, !ks->groups[f + 1]);
		if (match < 0) {
			ks->map_idx = idx;
			return 0;
		}
#else /* !MATCH_UNROLLED && !MATCH_AVX2 */
		for (g = 0; g < ks->groups[f]; g++) {
			if (g % 2) {
				v = *pkt_p & 0x0f;
				pkt_p++;
			} else {
				v = *pkt_p >> 4;
			}
			andmem(map[idx], lt + v * ks->bsize[f], ks->bsize[f]);
			show_match(map[idx], lt, v, ks->bsize[f]);
			lt += ks->bsize[f] * BUCKETS;
		}
#endif

		/* Now populate the bitmap for the next field, unless this is
		 * the last field, in which case return the matched key if any
		 * (steps 4.4 - 4.5). Now map[idx] contains the matching bitmap,
		 * and map[!idx] is the bitmap for the next field.
		 *
		 * If we used the AVX2-based lookup, and this is the last field,
		 * we can already give ffs_and_fill() a hint about the position
		 * of the first bit set: that won't be before a 'match' offset.
		 */
#ifdef MATCH_CTZL
		b = ffs_and_fill(map[idx], match, ks->bsize[f] / 8, map[!idx],
				 ks->mt[f], !ks->groups[f + 1]);
		if (b < 0) {
			ks->map_idx = idx;
			return 0;
		}
		if (!ks->groups[f + 1]) {
			/* Last field: we're just returning the key without
			 * filling the next bitmap, so _map[!idx]_ is clear and
			 * can be reused as *next* bitmap (not initial) for the
			 * next packet.
			 */
			ks->map_idx = idx;
			return ks->mt[f][b].key;
		}
#else
		offset = match = 0;
		while ((b = ffs_clear(map[idx], ks->bsize[f], offset)) >= 0) {
			if (!ks->groups[f + 1]) {
				ks->map_idx = !idx;
				return ks->mt[f][b].key;
			}

			offset = b / (sizeof(int) * 8);
			match = 1;
			verbose("  map bit %i: fill %i bit%s from %i\n", b,
				ks->mt[f][b].n, ks->mt[f][b].n == 1 ? "" : "s",
				ks->mt[f][b].to);
			fill(map[!idx], ks->mt[f][b].to, ks->mt[f][b].n);
		}

		if (!match) {
			ks->map_idx = !idx;
			return 0;
		}
#endif
		/* Swap bitmap indices: map[!idx] will be the initial bitmap,
		 * and map[idx] is guaranteed to be all-zeroes at this point.
		 */
		idx = !idx;
	}

	return 0;
}