-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathsimple_switch.cpp
770 lines (668 loc) · 26.8 KB
/
simple_switch.cpp
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/* Copyright 2013-present Barefoot Networks, Inc.
* Copyright 2021 VMware, 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
*
*/
#include <bm/bm_sim/_assert.h>
#include <bm/bm_sim/parser.h>
#include <bm/bm_sim/tables.h>
#include <bm/bm_sim/logger.h>
#include <unistd.h>
#include <condition_variable>
#include <deque>
#include <fstream>
#include <iostream>
#include <mutex>
#include <string>
#include <unordered_map>
#include <utility>
#include "simple_switch.h"
#include "register_access.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 *simple_switch);
packet_id_t SimpleSwitch::packet_id = 0;
class SimpleSwitch::MirroringSessions {
public:
bool add_session(mirror_id_t mirror_id,
const MirroringSessionConfig &config) {
Lock lock(mutex);
if (0 <= mirror_id && mirror_id <= RegisterAccess::MAX_MIRROR_SESSION_ID) {
sessions_map[mirror_id] = config;
return true;
} else {
bm::Logger::get()->error("mirror_id out of range. No session added.");
return false;
}
}
bool delete_session(mirror_id_t mirror_id) {
Lock lock(mutex);
if (0 <= mirror_id && mirror_id <= RegisterAccess::MAX_MIRROR_SESSION_ID) {
return sessions_map.erase(mirror_id) == 1;
} else {
bm::Logger::get()->error("mirror_id out of range. No session deleted.");
return false;
}
}
bool get_session(mirror_id_t mirror_id,
MirroringSessionConfig *config) const {
Lock lock(mutex);
auto it = sessions_map.find(mirror_id);
if (it == sessions_map.end()) return false;
*config = it->second;
return true;
}
private:
using Mutex = std::mutex;
using Lock = std::lock_guard<Mutex>;
mutable std::mutex mutex;
std::unordered_map<mirror_id_t, MirroringSessionConfig> sessions_map;
};
// Arbitrates which packets are processed by the ingress thread. Resubmit and
// recirculate packets go to a high priority queue, while normal packets go to a
// low priority queue. We assume that starvation is not going to be a problem.
// Resubmit packets are dropped if the queue is full in order to make sure the
// ingress thread cannot deadlock. We do the same for recirculate packets even
// though the same argument does not apply for them. Enqueueing normal packets
// is blocking (back pressure is applied to the interface).
class SimpleSwitch::InputBuffer {
public:
enum class PacketType {
NORMAL,
RESUBMIT,
RECIRCULATE,
SENTINEL // signal for the ingress thread to terminate
};
InputBuffer(size_t capacity_hi, size_t capacity_lo)
: capacity_hi(capacity_hi), capacity_lo(capacity_lo) { }
int push_front(PacketType packet_type, std::unique_ptr<Packet> &&item) {
switch (packet_type) {
case PacketType::NORMAL:
return push_front(&queue_lo, capacity_lo, &cvar_can_push_lo,
std::move(item), true);
case PacketType::RESUBMIT:
case PacketType::RECIRCULATE:
return push_front(&queue_hi, capacity_hi, &cvar_can_push_hi,
std::move(item), false);
case PacketType::SENTINEL:
return push_front(&queue_hi, capacity_hi, &cvar_can_push_hi,
std::move(item), true);
}
_BM_UNREACHABLE("Unreachable statement");
return 0;
}
void pop_back(std::unique_ptr<Packet> *pItem) {
Lock lock(mutex);
cvar_can_pop.wait(
lock, [this] { return (queue_hi.size() + queue_lo.size()) > 0; });
// give higher priority to resubmit/recirculate queue
if (queue_hi.size() > 0) {
*pItem = std::move(queue_hi.back());
queue_hi.pop_back();
lock.unlock();
cvar_can_push_hi.notify_one();
} else {
*pItem = std::move(queue_lo.back());
queue_lo.pop_back();
lock.unlock();
cvar_can_push_lo.notify_one();
}
}
private:
using Mutex = std::mutex;
using Lock = std::unique_lock<Mutex>;
using QueueImpl = std::deque<std::unique_ptr<Packet> >;
int push_front(QueueImpl *queue, size_t capacity,
std::condition_variable *cvar,
std::unique_ptr<Packet> &&item, bool blocking) {
Lock lock(mutex);
while (queue->size() == capacity) {
if (!blocking) return 0;
cvar->wait(lock);
}
queue->push_front(std::move(item));
lock.unlock();
cvar_can_pop.notify_one();
return 1;
}
mutable std::mutex mutex;
mutable std::condition_variable cvar_can_push_hi;
mutable std::condition_variable cvar_can_push_lo;
mutable std::condition_variable cvar_can_pop;
size_t capacity_hi;
size_t capacity_lo;
QueueImpl queue_hi;
QueueImpl queue_lo;
};
SimpleSwitch::SimpleSwitch(bool enable_swap, port_t drop_port,
size_t nb_queues_per_port)
: Switch(enable_swap),
drop_port(drop_port),
input_buffer(new InputBuffer(
1024 /* normal capacity */, 1024 /* resubmit/recirc capacity */)),
nb_queues_per_port(nb_queues_per_port),
egress_buffers(nb_egress_threads,
64, EgressThreadMapper(nb_egress_threads),
nb_queues_per_port),
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](port_t port_num, packet_id_t pkt_id,
const char *buffer, int len) {
_BM_UNUSED(pkt_id);
this->transmit_fn(port_num, buffer, len);
}),
pre(new McSimplePreLAG()),
start(clock::now()),
mirroring_sessions(new MirroringSessions()) {
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", "egress_port");
force_arith_header("standard_metadata");
force_arith_header("queueing_metadata");
force_arith_header("intrinsic_metadata");
import_primitives(this);
}
int
SimpleSwitch::receive_(port_t port_num, const char *buffer, int len) {
// 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, packet_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();
RegisterAccess::clear_all(packet.get());
// 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(RegisterAccess::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(
InputBuffer::PacketType::NORMAL, 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));
}
void
SimpleSwitch::swap_notify_() {
bm::Logger::get()->debug(
"simple_switch target has been notified of a config swap");
check_queueing_metadata();
}
SimpleSwitch::~SimpleSwitch() {
input_buffer->push_front(
InputBuffer::PacketType::SENTINEL, nullptr);
for (size_t i = 0; i < nb_egress_threads; i++) {
// The push_front call is called inside a while loop because there is no
// guarantee that the sentinel was enqueued otherwise. It should not be an
// issue because at this stage the ingress thread has been sent a signal to
// stop, and only egress clones can be sent to the buffer.
while (egress_buffers.push_front(i, 0, nullptr) == 0) continue;
}
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();
}
bool
SimpleSwitch::mirroring_add_session(mirror_id_t mirror_id,
const MirroringSessionConfig &config) {
return mirroring_sessions->add_session(mirror_id, config);
}
bool
SimpleSwitch::mirroring_delete_session(mirror_id_t mirror_id) {
return mirroring_sessions->delete_session(mirror_id);
}
bool
SimpleSwitch::mirroring_get_session(mirror_id_t mirror_id,
MirroringSessionConfig *config) const {
return mirroring_sessions->get_session(mirror_id, config);
}
int
SimpleSwitch::set_egress_priority_queue_depth(size_t port, size_t priority,
const size_t depth_pkts) {
egress_buffers.set_capacity(port, priority, depth_pkts);
return 0;
}
int
SimpleSwitch::set_egress_queue_depth(size_t port, const size_t depth_pkts) {
egress_buffers.set_capacity(port, depth_pkts);
return 0;
}
int
SimpleSwitch::set_all_egress_queue_depths(const size_t depth_pkts) {
egress_buffers.set_capacity_for_all(depth_pkts);
return 0;
}
int
SimpleSwitch::set_egress_priority_queue_rate(size_t port, size_t priority,
const uint64_t rate_pps) {
egress_buffers.set_rate(port, priority, rate_pps);
return 0;
}
int
SimpleSwitch::set_egress_queue_rate(size_t port, const uint64_t rate_pps) {
egress_buffers.set_rate(port, rate_pps);
return 0;
}
int
SimpleSwitch::set_all_egress_queue_rates(const uint64_t rate_pps) {
egress_buffers.set_rate_for_all(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->get_packet_id(),
packet->data(), packet->get_data_size());
}
}
ts_res
SimpleSwitch::get_ts() const {
return duration_cast<ts_res>(clock::now() - start);
}
void
SimpleSwitch::enqueue(port_t egress_port, std::unique_ptr<Packet> &&packet) {
packet->set_egress_port(egress_port);
PHV *phv = packet->get_phv();
size_t priority = phv->has_field(SSWITCH_PRIORITY_QUEUEING_SRC) ?
phv->get_field(SSWITCH_PRIORITY_QUEUEING_SRC).get<size_t>() : 0u;
if (priority >= nb_queues_per_port) {
bm::Logger::get()->error("Priority out of range, dropping packet");
return;
}
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, priority));
}
egress_buffers.push_front(
egress_port, priority,
std::move(packet));
}
// 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;
return;
} else {
bm::Logger::get()->warn(
"Your JSON input defines some but not all queueing metadata fields");
}
}
with_queueing_metadata = false;
}
void
SimpleSwitch::multicast(Packet *packet, unsigned int mgid) {
auto *phv = packet->get_phv();
auto &f_rid = phv->get_field("intrinsic_metadata.egress_rid");
const auto pre_out = pre->replicate({mgid});
auto packet_size =
packet->get_register(RegisterAccess::PACKET_LENGTH_REG_IDX);
for (const auto &out : pre_out) {
auto egress_port = out.egress_port;
BMLOG_DEBUG_PKT(*packet, "Replicating packet on port {}", egress_port);
f_rid.set(out.rid);
std::unique_ptr<Packet> packet_copy = packet->clone_with_phv_ptr();
RegisterAccess::clear_all(packet_copy.get());
packet_copy->set_register(RegisterAccess::PACKET_LENGTH_REG_IDX,
packet_size);
enqueue(egress_port, std::move(packet_copy));
}
}
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();
port_t ingress_port = packet->get_ingress_port();
(void) ingress_port;
BMLOG_DEBUG_PKT(*packet, "Processing packet received on port {}",
ingress_port);
auto ingress_packet_size =
packet->get_register(RegisterAccess::PACKET_LENGTH_REG_IDX);
/* 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());
if (phv->has_field("standard_metadata.parser_error")) {
phv->get_field("standard_metadata.parser_error").set(
packet->get_error_code().get());
}
if (phv->has_field("standard_metadata.checksum_error")) {
phv->get_field("standard_metadata.checksum_error").set(
packet->get_checksum_error() ? 1 : 0);
}
ingress_mau->apply(packet.get());
packet->reset_exit();
Field &f_egress_spec = phv->get_field("standard_metadata.egress_spec");
port_t egress_spec = f_egress_spec.get_uint();
auto clone_mirror_session_id =
RegisterAccess::get_clone_mirror_session_id(packet.get());
auto clone_field_list = RegisterAccess::get_clone_field_list(packet.get());
int learn_id = RegisterAccess::get_lf_field_list(packet.get());
unsigned int mgid = 0u;
// 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();
}
// INGRESS CLONING
if (clone_mirror_session_id) {
BMLOG_DEBUG_PKT(*packet, "Cloning packet at ingress");
RegisterAccess::set_clone_mirror_session_id(packet.get(), 0);
RegisterAccess::set_clone_field_list(packet.get(), 0);
MirroringSessionConfig config;
// Extract the part of clone_mirror_session_id that contains the
// actual session id.
clone_mirror_session_id &= RegisterAccess::MIRROR_SESSION_ID_MASK;
bool is_session_configured = mirroring_get_session(
static_cast<mirror_id_t>(clone_mirror_session_id), &config);
if (is_session_configured) {
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_field_list;
std::unique_ptr<Packet> packet_copy = packet->clone_no_phv_ptr();
RegisterAccess::clear_all(packet_copy.get());
packet_copy->set_register(RegisterAccess::PACKET_LENGTH_REG_IDX,
ingress_packet_size);
// We need to parse again.
// The alternative would be to pay the (huge) price of PHV copy for
// every ingress packet.
// Since parsers can branch on the ingress port, we need to preserve it
// to ensure re-parsing gives the same result as the original parse.
// TODO(https://github.com/p4lang/behavioral-model/issues/795): other
// standard metadata should be preserved as well.
packet_copy->get_phv()
->get_field("standard_metadata.ingress_port")
.set(ingress_port);
parser->parse(packet_copy.get());
copy_field_list_and_set_type(packet, packet_copy,
PKT_INSTANCE_TYPE_INGRESS_CLONE,
field_list_id);
if (config.mgid_valid) {
BMLOG_DEBUG_PKT(*packet, "Cloning packet to MGID {}", config.mgid);
multicast(packet_copy.get(), config.mgid);
}
if (config.egress_port_valid) {
BMLOG_DEBUG_PKT(*packet, "Cloning packet to egress port {}",
config.egress_port);
enqueue(config.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
auto resubmit_flag = RegisterAccess::get_resubmit_flag(packet.get());
if (resubmit_flag) {
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 = resubmit_flag;
RegisterAccess::set_resubmit_flag(packet.get(), 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();
PHV *phv_copy = packet_copy->get_phv();
copy_field_list_and_set_type(packet, packet_copy,
PKT_INSTANCE_TYPE_RESUBMIT,
field_list_id);
RegisterAccess::clear_all(packet_copy.get());
packet_copy->set_register(RegisterAccess::PACKET_LENGTH_REG_IDX,
ingress_packet_size);
phv_copy->get_field("standard_metadata.packet_length")
.set(ingress_packet_size);
input_buffer->push_front(
InputBuffer::PacketType::RESUBMIT, std::move(packet_copy));
continue;
}
// MULTICAST
if (mgid != 0) {
BMLOG_DEBUG_PKT(*packet, "Multicast requested for packet");
auto &f_instance_type = phv->get_field("standard_metadata.instance_type");
f_instance_type.set(PKT_INSTANCE_TYPE_REPLICATION);
multicast(packet.get(), mgid);
// when doing multicast, we discard the original packet
continue;
}
port_t egress_port = egress_spec;
BMLOG_DEBUG_PKT(*packet, "Egress port is {}", egress_port);
if (egress_port == drop_port) { // drop packet
BMLOG_DEBUG_PKT(*packet, "Dropping packet at the end of ingress");
continue;
}
auto &f_instance_type = phv->get_field("standard_metadata.instance_type");
f_instance_type.set(PKT_INSTANCE_TYPE_NORMAL);
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;
size_t priority;
egress_buffers.pop_back(worker_id, &port, &priority, &packet);
if (packet == nullptr) break;
Deparser *deparser = this->get_deparser("deparser");
Pipeline *egress_mau = this->get_pipeline("egress");
phv = packet->get_phv();
if (phv->has_field("intrinsic_metadata.egress_global_timestamp")) {
phv->get_field("intrinsic_metadata.egress_global_timestamp")
.set(get_ts().count());
}
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, priority));
if (phv->has_field("queueing_metadata.qid")) {
auto &qid_f = phv->get_field("queueing_metadata.qid");
qid_f.set(priority);
}
}
phv->get_field("standard_metadata.egress_port").set(port);
Field &f_egress_spec = phv->get_field("standard_metadata.egress_spec");
// When egress_spec == drop_port the packet will be dropped, thus
// here we initialize egress_spec to a value different from drop_port.
f_egress_spec.set(drop_port + 1);
phv->get_field("standard_metadata.packet_length").set(
packet->get_register(RegisterAccess::PACKET_LENGTH_REG_IDX));
egress_mau->apply(packet.get());
auto clone_mirror_session_id =
RegisterAccess::get_clone_mirror_session_id(packet.get());
auto clone_field_list = RegisterAccess::get_clone_field_list(packet.get());
// EGRESS CLONING
if (clone_mirror_session_id) {
BMLOG_DEBUG_PKT(*packet, "Cloning packet at egress");
RegisterAccess::set_clone_mirror_session_id(packet.get(), 0);
RegisterAccess::set_clone_field_list(packet.get(), 0);
MirroringSessionConfig config;
// Extract the part of clone_mirror_session_id that contains the
// actual session id.
clone_mirror_session_id &= RegisterAccess::MIRROR_SESSION_ID_MASK;
bool is_session_configured = mirroring_get_session(
static_cast<mirror_id_t>(clone_mirror_session_id), &config);
if (is_session_configured) {
p4object_id_t field_list_id = clone_field_list;
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);
auto packet_size =
packet->get_register(RegisterAccess::PACKET_LENGTH_REG_IDX);
RegisterAccess::clear_all(packet_copy.get());
packet_copy->set_register(RegisterAccess::PACKET_LENGTH_REG_IDX,
packet_size);
if (config.mgid_valid) {
BMLOG_DEBUG_PKT(*packet, "Cloning packet to MGID {}", config.mgid);
multicast(packet_copy.get(), config.mgid);
}
if (config.egress_port_valid) {
BMLOG_DEBUG_PKT(*packet, "Cloning packet to egress port {}",
config.egress_port);
enqueue(config.egress_port, std::move(packet_copy));
}
}
}
// TODO(antonin): should not be done like this in egress pipeline
port_t egress_spec = f_egress_spec.get_uint();
if (egress_spec == drop_port) { // drop packet
BMLOG_DEBUG_PKT(*packet, "Dropping packet at the end of egress");
continue;
}
deparser->deparse(packet.get());
// RECIRCULATE
auto recirculate_flag = RegisterAccess::get_recirculate_flag(packet.get());
if (recirculate_flag) {
BMLOG_DEBUG_PKT(*packet, "Recirculating packet");
p4object_id_t field_list_id = recirculate_flag;
RegisterAccess::set_recirculate_flag(packet.get(), 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();
RegisterAccess::clear_all(packet_copy.get());
packet_copy->set_register(RegisterAccess::PACKET_LENGTH_REG_IDX,
packet_size);
phv_copy->get_field("standard_metadata.packet_length").set(packet_size);
// TODO(antonin): really it may be better to create a new packet here or
// to fold this functionality into the Packet class?
packet_copy->set_ingress_length(packet_size);
input_buffer->push_front(
InputBuffer::PacketType::RECIRCULATE, std::move(packet_copy));
continue;
}
output_buffer.push_front(std::move(packet));
}
}