-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathsimple_switch.cpp
More file actions
556 lines (469 loc) · 18.2 KB
/
simple_switch.cpp
File metadata and controls
556 lines (469 loc) · 18.2 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Antonin Bas (antonin@barefootnetworks.com)
*
*/
#include <bm/bm_sim/parser.h>
#include <bm/bm_sim/tables.h>
#include <bm/bm_sim/logger.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include "simple_switch.h"
namespace {
struct hash_ex {
uint32_t operator()(const char *buf, size_t s) const {
const uint32_t p = 16777619;
uint32_t hash = 2166136261;
for (size_t i = 0; i < s; i++)
hash = (hash ^ buf[i]) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return static_cast<uint32_t>(hash);
}
};
struct bmv2_hash {
uint64_t operator()(const char *buf, size_t s) const {
return bm::hash::xxh64(buf, s);
}
};
} // namespace
// if REGISTER_HASH calls placed in the anonymous namespace, some compiler can
// give an unused variable warning
REGISTER_HASH(hash_ex);
REGISTER_HASH(bmv2_hash);
extern int import_primitives();
SimpleSwitch::SimpleSwitch(int max_port, bool enable_swap)
: Switch(enable_swap),
max_port(max_port),
input_buffer(1024),
#ifdef SSWITCH_PRIORITY_QUEUEING_ON
egress_buffers(max_port, nb_egress_threads,
64, EgressThreadMapper(nb_egress_threads),
SSWITCH_PRIORITY_QUEUEING_NB_QUEUES),
#else
egress_buffers(max_port, nb_egress_threads,
64, EgressThreadMapper(nb_egress_threads)),
#endif
output_buffer(128),
// cannot use std::bind because of a clang bug
// https://stackoverflow.com/questions/32030141/is-this-incorrect-use-of-stdbind-or-a-compiler-bug
my_transmit_fn([this](int port_num, const char *buffer, int len) {
this->transmit_fn(port_num, buffer, len); }),
pre(new McSimplePreLAG()),
start(clock::now()) {
add_component<McSimplePreLAG>(pre);
add_required_field("standard_metadata", "ingress_port");
add_required_field("standard_metadata", "packet_length");
add_required_field("standard_metadata", "instance_type");
add_required_field("standard_metadata", "egress_spec");
add_required_field("standard_metadata", "clone_spec");
add_required_field("standard_metadata", "egress_port");
force_arith_header("standard_metadata");
force_arith_header("queueing_metadata");
force_arith_header("intrinsic_metadata");
import_primitives();
}
#define PACKET_LENGTH_REG_IDX 0
int
SimpleSwitch::receive_(int port_num, const char *buffer, int len) {
static packet_id_t pkt_id = 0;
// this is a good place to call this, because blocking this thread will not
// block the processing of existing packet instances, which is a requirement
if (do_swap() == 0) {
check_queueing_metadata();
}
// we limit the packet buffer to original size + 512 bytes, which means we
// cannot add more than 512 bytes of header data to the packet, which should
// be more than enough
auto packet = new_packet_ptr(port_num, pkt_id++, len,
bm::PacketBuffer(len + 512, buffer, len));
BMELOG(packet_in, *packet);
PHV *phv = packet->get_phv();
// many current P4 programs assume this
// it is also part of the original P4 spec
phv->reset_metadata();
// setting standard metadata
phv->get_field("standard_metadata.ingress_port").set(port_num);
// using packet register 0 to store length, this register will be updated for
// each add_header / remove_header primitive call
packet->set_register(PACKET_LENGTH_REG_IDX, len);
phv->get_field("standard_metadata.packet_length").set(len);
Field &f_instance_type = phv->get_field("standard_metadata.instance_type");
f_instance_type.set(PKT_INSTANCE_TYPE_NORMAL);
if (phv->has_field("intrinsic_metadata.ingress_global_timestamp")) {
phv->get_field("intrinsic_metadata.ingress_global_timestamp")
.set(get_ts().count());
}
input_buffer.push_front(std::move(packet));
return 0;
}
void
SimpleSwitch::start_and_return_() {
check_queueing_metadata();
threads_.push_back(std::thread(&SimpleSwitch::ingress_thread, this));
for (size_t i = 0; i < nb_egress_threads; i++) {
threads_.push_back(std::thread(&SimpleSwitch::egress_thread, this, i));
}
threads_.push_back(std::thread(&SimpleSwitch::transmit_thread, this));
}
SimpleSwitch::~SimpleSwitch() {
input_buffer.push_front(nullptr);
for (size_t i = 0; i < nb_egress_threads; i++) {
#ifdef SSWITCH_PRIORITY_QUEUEING_ON
egress_buffers.push_front(i, 0, nullptr);
#else
egress_buffers.push_front(i, nullptr);
#endif
}
output_buffer.push_front(nullptr);
for (auto& thread_ : threads_) {
thread_.join();
}
}
void
SimpleSwitch::reset_target_state_() {
bm::Logger::get()->debug("Resetting simple_switch target-specific state");
get_component<McSimplePreLAG>()->reset_state();
}
int
SimpleSwitch::set_egress_queue_depth(int port, const size_t depth_pkts) {
egress_buffers.set_capacity(port, depth_pkts);
return 0;
}
int
SimpleSwitch::set_egress_priority_queue_depth(int port, size_t priority, const size_t depth_pkts) {
egress_buffers.set_capacity(port, priority, depth_pkts);
return 0;
}
int
SimpleSwitch::set_all_egress_queue_depths(const size_t depth_pkts) {
for (int i = 0; i < max_port; i++) {
set_egress_queue_depth(i, depth_pkts);
}
return 0;
}
int
SimpleSwitch::set_egress_queue_rate(int port, const uint64_t rate_pps) {
egress_buffers.set_rate(port, rate_pps);
return 0;
}
int
SimpleSwitch::set_egress_priority_queue_rate(int port, size_t priority, const uint64_t rate_pps) {
egress_buffers.set_rate(port, priority, rate_pps);
return 0;
}
int
SimpleSwitch::set_all_egress_queue_rates(const uint64_t rate_pps) {
for (int i = 0; i < max_port; i++) {
set_egress_queue_rate(i, rate_pps);
}
return 0;
}
uint64_t
SimpleSwitch::get_time_elapsed_us() const {
return get_ts().count();
}
uint64_t
SimpleSwitch::get_time_since_epoch_us() const {
auto tp = clock::now();
return duration_cast<ts_res>(tp.time_since_epoch()).count();
}
void
SimpleSwitch::set_transmit_fn(TransmitFn fn) {
my_transmit_fn = std::move(fn);
}
void
SimpleSwitch::transmit_thread() {
while (1) {
std::unique_ptr<Packet> packet;
output_buffer.pop_back(&packet);
if (packet == nullptr) break;
BMELOG(packet_out, *packet);
BMLOG_DEBUG_PKT(*packet, "Transmitting packet of size {} out of port {}",
packet->get_data_size(), packet->get_egress_port());
my_transmit_fn(packet->get_egress_port(),
packet->data(), packet->get_data_size());
}
}
ts_res
SimpleSwitch::get_ts() const {
return duration_cast<ts_res>(clock::now() - start);
}
void
SimpleSwitch::enqueue(int egress_port, std::unique_ptr<Packet> &&packet) {
packet->set_egress_port(egress_port);
PHV *phv = packet->get_phv();
if (with_queueing_metadata) {
phv->get_field("queueing_metadata.enq_timestamp").set(get_ts().count());
phv->get_field("queueing_metadata.enq_qdepth")
.set(egress_buffers.size(egress_port));
}
#ifdef SSWITCH_PRIORITY_QUEUEING_ON
size_t priority = phv->has_field(SSWITCH_PRIORITY_QUEUEING_SRC) ?
phv->get_field(SSWITCH_PRIORITY_QUEUEING_SRC).get<size_t>() : 0u;
if (priority >= SSWITCH_PRIORITY_QUEUEING_NB_QUEUES) {
bm::Logger::get()->error("Priority out of range, dropping packet");
return;
}
egress_buffers.push_front(
egress_port, SSWITCH_PRIORITY_QUEUEING_NB_QUEUES - 1 - priority,
std::move(packet));
#else
egress_buffers.push_front(egress_port, std::move(packet));
#endif
}
// used for ingress cloning, resubmit
void
SimpleSwitch::copy_field_list_and_set_type(
const std::unique_ptr<Packet> &packet,
const std::unique_ptr<Packet> &packet_copy,
PktInstanceType copy_type, p4object_id_t field_list_id) {
PHV *phv_copy = packet_copy->get_phv();
phv_copy->reset_metadata();
FieldList *field_list = this->get_field_list(field_list_id);
field_list->copy_fields_between_phvs(phv_copy, packet->get_phv());
phv_copy->get_field("standard_metadata.instance_type").set(copy_type);
}
void
SimpleSwitch::check_queueing_metadata() {
// TODO(antonin): add qid in required fields
bool enq_timestamp_e = field_exists("queueing_metadata", "enq_timestamp");
bool enq_qdepth_e = field_exists("queueing_metadata", "enq_qdepth");
bool deq_timedelta_e = field_exists("queueing_metadata", "deq_timedelta");
bool deq_qdepth_e = field_exists("queueing_metadata", "deq_qdepth");
if (enq_timestamp_e || enq_qdepth_e || deq_timedelta_e || deq_qdepth_e) {
if (enq_timestamp_e && enq_qdepth_e && deq_timedelta_e && deq_qdepth_e)
with_queueing_metadata = true;
else
bm::Logger::get()->warn(
"Your JSON input defines some but not all queueing metadata fields");
}
}
void
SimpleSwitch::ingress_thread() {
PHV *phv;
while (1) {
std::unique_ptr<Packet> packet;
input_buffer.pop_back(&packet);
if (packet == nullptr) break;
// TODO(antonin): only update these if swapping actually happened?
Parser *parser = this->get_parser("parser");
Pipeline *ingress_mau = this->get_pipeline("ingress");
phv = packet->get_phv();
int ingress_port = packet->get_ingress_port();
(void) ingress_port;
BMLOG_DEBUG_PKT(*packet, "Processing packet received on port {}",
ingress_port);
/* This looks like it comes out of the blue. However this is needed for
ingress cloning. The parser updates the buffer state (pops the parsed
headers) to make the deparser's job easier (the same buffer is
re-used). But for ingress cloning, the original packet is needed. This
kind of looks hacky though. Maybe a better solution would be to have the
parser leave the buffer unchanged, and move the pop logic to the
deparser. TODO? */
const Packet::buffer_state_t packet_in_state = packet->save_buffer_state();
parser->parse(packet.get());
ingress_mau->apply(packet.get());
packet->reset_exit();
Field &f_egress_spec = phv->get_field("standard_metadata.egress_spec");
int egress_spec = f_egress_spec.get_int();
Field &f_clone_spec = phv->get_field("standard_metadata.clone_spec");
unsigned int clone_spec = f_clone_spec.get_uint();
int learn_id = 0;
unsigned int mgid = 0u;
if (phv->has_field("intrinsic_metadata.lf_field_list")) {
Field &f_learn_id = phv->get_field("intrinsic_metadata.lf_field_list");
learn_id = f_learn_id.get_int();
}
// detect mcast support, if this is true we assume that other fields needed
// for mcast are also defined
if (phv->has_field("intrinsic_metadata.mcast_grp")) {
Field &f_mgid = phv->get_field("intrinsic_metadata.mcast_grp");
mgid = f_mgid.get_uint();
}
int egress_port;
// INGRESS CLONING
if (clone_spec) {
BMLOG_DEBUG_PKT(*packet, "Cloning packet at ingress");
egress_port = get_mirroring_mapping(clone_spec & 0xFFFF);
f_clone_spec.set(0);
if (egress_port >= 0) {
const Packet::buffer_state_t packet_out_state =
packet->save_buffer_state();
packet->restore_buffer_state(packet_in_state);
p4object_id_t field_list_id = clone_spec >> 16;
std::unique_ptr<Packet> packet_copy = packet->clone_no_phv_ptr();
// we need to parse again
// the alternative would be to pay the (huge) price of PHV copy for
// every ingress packet
parser->parse(packet_copy.get());
copy_field_list_and_set_type(packet, packet_copy,
PKT_INSTANCE_TYPE_INGRESS_CLONE,
field_list_id);
enqueue(egress_port, std::move(packet_copy));
packet->restore_buffer_state(packet_out_state);
}
}
// LEARNING
if (learn_id > 0) {
get_learn_engine()->learn(learn_id, *packet.get());
}
// RESUBMIT
if (phv->has_field("intrinsic_metadata.resubmit_flag")) {
Field &f_resubmit = phv->get_field("intrinsic_metadata.resubmit_flag");
if (f_resubmit.get_int()) {
BMLOG_DEBUG_PKT(*packet, "Resubmitting packet");
// get the packet ready for being parsed again at the beginning of
// ingress
packet->restore_buffer_state(packet_in_state);
p4object_id_t field_list_id = f_resubmit.get_int();
f_resubmit.set(0);
// TODO(antonin): a copy is not needed here, but I don't yet have an
// optimized way of doing this
std::unique_ptr<Packet> packet_copy = packet->clone_no_phv_ptr();
copy_field_list_and_set_type(packet, packet_copy,
PKT_INSTANCE_TYPE_RESUBMIT,
field_list_id);
input_buffer.push_front(std::move(packet_copy));
continue;
}
}
Field &f_instance_type = phv->get_field("standard_metadata.instance_type");
// MULTICAST
int instance_type = f_instance_type.get_int();
if (mgid != 0) {
BMLOG_DEBUG_PKT(*packet, "Multicast requested for packet");
Field &f_rid = phv->get_field("intrinsic_metadata.egress_rid");
const auto pre_out = pre->replicate({mgid});
auto packet_size = packet->get_register(PACKET_LENGTH_REG_IDX);
for (const auto &out : pre_out) {
egress_port = out.egress_port;
// if (ingress_port == egress_port) continue; // pruning
BMLOG_DEBUG_PKT(*packet, "Replicating packet on port {}", egress_port);
f_rid.set(out.rid);
f_instance_type.set(PKT_INSTANCE_TYPE_REPLICATION);
std::unique_ptr<Packet> packet_copy = packet->clone_with_phv_ptr();
packet_copy->set_register(PACKET_LENGTH_REG_IDX, packet_size);
enqueue(egress_port, std::move(packet_copy));
}
f_instance_type.set(instance_type);
// when doing multicast, we discard the original packet
continue;
}
egress_port = egress_spec;
BMLOG_DEBUG_PKT(*packet, "Egress port is {}", egress_port);
if (egress_port == 511) { // drop packet
BMLOG_DEBUG_PKT(*packet, "Dropping packet at the end of ingress");
continue;
}
enqueue(egress_port, std::move(packet));
}
}
void
SimpleSwitch::egress_thread(size_t worker_id) {
PHV *phv;
while (1) {
std::unique_ptr<Packet> packet;
size_t port;
#ifdef SSWITCH_PRIORITY_QUEUEING_ON
size_t priority;
egress_buffers.pop_back(worker_id, &port, &priority, &packet);
#else
egress_buffers.pop_back(worker_id, &port, &packet);
#endif
if (packet == nullptr) break;
Deparser *deparser = this->get_deparser("deparser");
Pipeline *egress_mau = this->get_pipeline("egress");
phv = packet->get_phv();
if (with_queueing_metadata) {
auto enq_timestamp =
phv->get_field("queueing_metadata.enq_timestamp").get<ts_res::rep>();
phv->get_field("queueing_metadata.deq_timedelta").set(
get_ts().count() - enq_timestamp);
phv->get_field("queueing_metadata.deq_qdepth").set(
egress_buffers.size(port));
if (phv->has_field("queueing_metadata.qid")) {
auto &qid_f = phv->get_field("queueing_metadata.qid");
#ifdef SSWITCH_PRIORITY_QUEUEING_ON
qid_f.set(SSWITCH_PRIORITY_QUEUEING_NB_QUEUES - 1 - priority);
#else
qid_f.set(0);
#endif
}
}
phv->get_field("standard_metadata.egress_port").set(port);
Field &f_egress_spec = phv->get_field("standard_metadata.egress_spec");
f_egress_spec.set(0);
phv->get_field("standard_metadata.packet_length").set(
packet->get_register(PACKET_LENGTH_REG_IDX));
egress_mau->apply(packet.get());
Field &f_clone_spec = phv->get_field("standard_metadata.clone_spec");
unsigned int clone_spec = f_clone_spec.get_uint();
// EGRESS CLONING
if (clone_spec) {
BMLOG_DEBUG_PKT(*packet, "Cloning packet at egress");
int egress_port = get_mirroring_mapping(clone_spec & 0xFFFF);
if (egress_port >= 0) {
f_clone_spec.set(0);
p4object_id_t field_list_id = clone_spec >> 16;
std::unique_ptr<Packet> packet_copy =
packet->clone_with_phv_reset_metadata_ptr();
PHV *phv_copy = packet_copy->get_phv();
FieldList *field_list = this->get_field_list(field_list_id);
field_list->copy_fields_between_phvs(phv_copy, phv);
phv_copy->get_field("standard_metadata.instance_type")
.set(PKT_INSTANCE_TYPE_EGRESS_CLONE);
enqueue(egress_port, std::move(packet_copy));
}
}
// TODO(antonin): should not be done like this in egress pipeline
int egress_spec = f_egress_spec.get_int();
if (egress_spec == 511) { // drop packet
BMLOG_DEBUG_PKT(*packet, "Dropping packet at the end of egress");
continue;
}
deparser->deparse(packet.get());
// RECIRCULATE
if (phv->has_field("intrinsic_metadata.recirculate_flag")) {
Field &f_recirc = phv->get_field("intrinsic_metadata.recirculate_flag");
if (f_recirc.get_int()) {
BMLOG_DEBUG_PKT(*packet, "Recirculating packet");
p4object_id_t field_list_id = f_recirc.get_int();
f_recirc.set(0);
FieldList *field_list = this->get_field_list(field_list_id);
// TODO(antonin): just like for resubmit, there is no need for a copy
// here, but it is more convenient for this first prototype
std::unique_ptr<Packet> packet_copy = packet->clone_no_phv_ptr();
PHV *phv_copy = packet_copy->get_phv();
phv_copy->reset_metadata();
field_list->copy_fields_between_phvs(phv_copy, phv);
phv_copy->get_field("standard_metadata.instance_type")
.set(PKT_INSTANCE_TYPE_RECIRC);
size_t packet_size = packet_copy->get_data_size();
packet_copy->set_register(PACKET_LENGTH_REG_IDX, packet_size);
phv_copy->get_field("standard_metadata.packet_length").set(packet_size);
input_buffer.push_front(std::move(packet_copy));
continue;
}
}
output_buffer.push_front(std::move(packet));
}
}