-
Notifications
You must be signed in to change notification settings - Fork 28
/
switch.c
357 lines (287 loc) · 8.81 KB
/
switch.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Copyright 2014 Nodalink EURL
*
* This file is part of Packetgraph.
*
* Packetgraph is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* Packetgraph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Packetgraph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rte_config.h>
#include <rte_errno.h>
#include <rte_ether.h>
#include <rte_hash_crc.h>
#include <rte_memcpy.h>
#include <rte_prefetch.h>
#define HASH_ENTRIES (1024 * 32)
#define HASH_KEY_SIZE 8
#include <packetgraph/packetgraph.h>
#include "brick-int.h"
#include "packets.h"
#include "utils/bitmask.h"
#include "utils/mac-table.h"
/* this tell from where a given source mac address came */
struct pg_address_source {
uint16_t edge_index; /* index of the port the packet came from */
enum pg_side from; /* side of the switch the packet came from */
};
struct pg_switch_side {
struct pg_address_source *sources;
uint64_t *masks; /* outgoing packet masks (one per port) */
};
struct pg_switch_state {
struct pg_brick brick;
struct pg_mac_table table;
enum pg_side output;
/* sides of the switch */
struct pg_switch_side sides[PG_MAX_SIDE];
};
struct pg_switch_config {
enum pg_side output;
};
static inline void flood(struct pg_switch_state *state,
struct pg_address_source *source,
uint64_t mask)
{
enum pg_side i;
uint16_t j;
if (!mask)
return;
for (i = 0; i < PG_MAX_SIDE; i ) {
struct pg_switch_side *switch_side = &state->sides[i];
for (j = 0; j < state->brick.sides[i].nb; j )
switch_side->masks[j] |= mask;
}
}
static inline void zero_masks(struct pg_switch_state *state)
{
enum pg_side i;
for (i = 0; i < PG_MAX_SIDE; i )
memset(state->sides[i].masks, 0,
state->brick.sides[i].max * sizeof(uint64_t));
}
static inline int forward(struct pg_switch_state *state, enum pg_side to,
uint16_t index, struct rte_mbuf **pkts,
struct pg_error **errp)
{
struct pg_brick_edge *edge = &state->brick.sides[to].edges[index];
struct pg_switch_side *switch_side = &state->sides[to];
uint64_t mask;
if (!edge->link)
return 0;
mask = switch_side->masks[index];
if (!mask)
return 0;
switch_side->masks[index] = 0;
return pg_brick_burst(edge->link, pg_flip_side(to), edge->pair_index,
pkts, mask, errp);
}
static int forward_bursts(struct pg_switch_state *state,
struct pg_address_source *source,
struct rte_mbuf **pkts,
struct pg_error **errp)
{
struct pg_switch_side *switch_side = &state->sides[source->from];
enum pg_side i = state->output;
uint16_t j;
int ret;
/* never forward on source port */
switch_side->masks[source->edge_index] = 0;
for (j = 0; j < state->brick.sides[i].nb; j ) {
ret = forward(state, i, j, pkts, errp);
if (ret < 0)
return -1;
}
i = pg_flip_side(i);
for (j = 0; j < state->brick.sides[i].nb; j ) {
ret = forward(state, i, j, pkts, errp);
if (ret < 0)
return -1;
}
return 0;
}
static inline bool is_filtered(struct ether_addr *eth_addr)
{
return eth_addr->addr_bytes[0] == 0x01 &&
eth_addr->addr_bytes[1] == 0x80 &&
eth_addr->addr_bytes[2] == 0xC2 &&
eth_addr->addr_bytes[3] == 0x00 &&
eth_addr->addr_bytes[4] == 0x00 &&
eth_addr->addr_bytes[5] <= 0x0F;
}
static inline void learn_addr(struct pg_switch_state *state,
uint8_t *key,
struct pg_address_source *source)
{
pg_mac_table_ptr_set(&state->table, *((union pg_mac *)key),
source);
}
static void do_learn_filter_multicast(struct pg_switch_state *state,
struct pg_address_source *source,
struct rte_mbuf **pkts,
uint64_t pkts_mask,
uint64_t *unicast_mask,
struct pg_error **errp)
{
uint64_t filtered_mask = 0, flood_mask = 0, mask;
for (mask = pkts_mask; mask;) {
struct ether_hdr *eth_hdr;
struct rte_mbuf *pkt;
uint64_t bit;
uint16_t i;
pg_low_bit_iterate_full(mask, bit, i);
pkt = pkts[i];
eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
/* associate source mac address with its source port */
learn_addr(state, (void *) ð_hdr->s_addr,
source);
/* http://standards.ieee.org/regauth/groupmac/tutorial.html */
if (unlikely(is_filtered(ð_hdr->d_addr))) {
filtered_mask |= bit;
continue;
}
/* if the packet is multicast or broadcast flood it */
if (unlikely(is_multicast_ether_addr(ð_hdr->d_addr))) {
flood_mask |= bit;
continue;
}
}
flood(state, source, flood_mask);
*unicast_mask = pkts_mask & ~filtered_mask & ~flood_mask;
}
static void do_switch(struct pg_switch_state *state,
struct pg_address_source *source,
struct rte_mbuf **pkts,
uint64_t pkts_mask)
{
uint64_t flood_mask = 0;
struct ether_hdr *eth_hdr;
for ( ; pkts_mask; ) {
struct pg_switch_side *switch_side;
struct pg_address_source *entry;
uint16_t edge_index;
uint64_t bit;
uint16_t i;
pg_low_bit_iterate_full(pkts_mask, bit, i);
eth_hdr = rte_pktmbuf_mtod(pkts[i], struct ether_hdr *);
entry = pg_mac_table_ptr_get(
&state->table,
*((union pg_mac *)ð_hdr->d_addr));
if (entry) {
switch_side = &state->sides[entry->from];
edge_index = entry->edge_index;
/* the lookup table entry is stale due to a port hotplug */
} else {
flood_mask |= bit;
continue;
}
/* mark the packet for forwarding on the correct port */
switch_side->masks[edge_index] |= bit;
}
flood(state, source, flood_mask);
}
static int switch_burst(struct pg_brick *brick, enum pg_side from,
uint16_t edge_index, struct rte_mbuf **pkts,
uint64_t pkts_mask,
struct pg_error **errp)
{
struct pg_switch_state *state =
pg_brick_get_state(brick, struct pg_switch_state);
uint64_t unicast_mask = 0;
struct pg_address_source *source;
source = &state->sides[from].sources[edge_index];
source->from = from;
source->edge_index = edge_index;
do_learn_filter_multicast(state, source, pkts,
pkts_mask, &unicast_mask, errp);
do_switch(state, source, pkts, unicast_mask);
return forward_bursts(state, source, pkts, errp);
}
static int switch_init(struct pg_brick *brick,
struct pg_brick_config *config, struct pg_error **errp)
{
struct pg_switch_state *state =
pg_brick_get_state(brick, struct pg_switch_state);
enum pg_side i;
brick->burst = switch_burst;
if (pg_mac_table_init(&state->table) < 0) {
*errp = pg_error_new(
"Failed to create hash for switch brick '%s'",
brick->name);
return -1;
}
for (i = 0; i < PG_MAX_SIDE; i ) {
uint16_t max = brick->sides[i].max;
state->sides[i].masks = g_new0(uint64_t, max);
state->sides[i].sources = g_new0(struct pg_address_source, max);
}
zero_masks(state);
state->output =
((struct pg_switch_config *)config->brick_config)->output;
return 0;
}
static struct pg_brick_config *pg_switch_config_new(const char *name,
uint32_t west_max,
uint32_t east_max,
enum pg_side output)
{
struct pg_brick_config *config = g_new0(struct pg_brick_config, 1);
struct pg_switch_config *switch_config = g_new0(struct pg_switch_config,
1);
switch_config->output = output;
config->brick_config = switch_config;
return pg_brick_config_init(config, name, west_max,
east_max, PG_MULTIPOLE);
}
struct pg_brick *pg_switch_new(const char *name,
uint32_t west_max,
uint32_t east_max,
enum pg_side output,
struct pg_error **errp)
{
struct pg_brick_config *config = pg_switch_config_new(name, west_max,
east_max, output);
struct pg_brick *ret = pg_brick_new("switch", config, errp);
pg_brick_config_free(config);
return ret;
}
static void switch_destroy(struct pg_brick *brick, struct pg_error **errp)
{
struct pg_switch_state *state =
pg_brick_get_state(brick, struct pg_switch_state);
enum pg_side i;
for (i = 0; i < PG_MAX_SIDE; i ) {
g_free(state->sides[i].masks);
g_free(state->sides[i].sources);
}
pg_mac_table_free(&state->table);
}
static void switch_unlink_notify(struct pg_brick *brick,
enum pg_side side, uint16_t edge_index,
struct pg_error **errp)
{
struct pg_switch_state *state =
pg_brick_get_state(brick, struct pg_switch_state);
PG_MAC_TABLE_FOREACH_PTR(&state->table, cur_mac,
struct pg_address_source, src) {
if (src->from == side &&
src->edge_index == edge_index)
pg_mac_table_ptr_unset(&state->table, cur_mac);
}
}
static struct pg_brick_ops switch_ops = {
.name = "switch",
.state_size = sizeof(struct pg_switch_state),
.init = switch_init,
.destroy = switch_destroy,
.unlink = pg_brick_generic_unlink,
.unlink_notify = switch_unlink_notify,
};
pg_brick_register(switch, &switch_ops);