Skip to content

Commit 31be1c8

Browse files
committed
Clean up fast path
Signed-off-by: Hoooao <hao021014@163.com>
1 parent 8a96aa9 commit 31be1c8

File tree

7 files changed

+69
-79
lines changed

7 files changed

+69
-79
lines changed

configure.ac

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ AS_IF([test "x$enable_debugger" = "xyes"], [
6363
])
6464
])
6565

66+
# TODO: add this to cmake build as well
67+
pkt_fanout_enabled=no
68+
AC_ARG_ENABLE([pkt_fanout],
69+
AS_HELP_STRING([--enable-pkt-fanout], [Enable packet fanout support]))
70+
AS_IF([test "x$enable_pkt_fanout" = "xyes"], [
71+
pkt_fanout_enabled=yes
72+
AC_DEFINE([PKT_FANOUT_ON], [], [Enable packet fanout support])
73+
])
74+
6675
logging_macros_enabled=no
6776
AC_ARG_ENABLE([logging_macros],
6877
AS_HELP_STRING([--disable-logging-macros],

include/bm/bm_sim/fanout_pkt_mgr.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ using SelectorIface = ActionProfile::GroupSelectionIface;
3030

3131
struct FanoutCtx {
3232
bool hit{false};
33-
std::vector<Packet *> fanout_pkts;
3433
const Packet * cur_pkt{nullptr};
3534
ActionProfile *action_profile{nullptr};
3635
MatchTableIndirect *cur_table{nullptr};
36+
std::function<void(const bm::Packet *)> buffer_push_fn;
37+
38+
FanoutCtx(const std::function<void(const bm::Packet *)> &buffer_push_fn)
39+
: buffer_push_fn(buffer_push_fn) { }
3740
};
3841

3942
class FanoutPktSelection: public SelectorIface{
@@ -67,23 +70,26 @@ class FanoutPktMgr {
6770
return instance_;
6871
}
6972

70-
inline void register_thread(std::thread::id thread_id) {
73+
inline void register_thread(std::thread::id thread_id,
74+
const std::function<void(const bm::Packet *)> &buffer_push_fn) {
7175
BMLOG_DEBUG("Registering thread {}", thread_id);
72-
fanout_ctx_map.emplace(thread_id, FanoutCtx());
76+
fanout_ctx_map.emplace(thread_id, FanoutCtx(buffer_push_fn));
7377
}
7478
inline SelectorIface* get_grp_selector() {
7579
return grp_selector;
7680
}
7781

78-
79-
80-
std::vector<Packet *>& get_fanout_pkts();
8182
FanoutCtx& get_fanout_ctx();
8283
void set_ctx(MatchTableIndirect *table, const Packet &pkt,
8384
ActionProfile *action_profile, bool hit);
8485
void reset_ctx();
8586
void replicate_for_entries(const std::vector<const ActionEntry*> &entries);
8687

88+
#ifdef BM_PKT_FANOUT_ON
89+
static constexpr bool pkt_fanout_on = true;
90+
#else
91+
static constexpr bool pkt_fanout_on = false;
92+
#endif
8793
std::mutex fanout_pkt_mutex;
8894
// TODO(Hao): deduplicate packets fanout, optional
8995

src/bm_sim/P4Objects.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,15 +1657,7 @@ P4Objects::init_pipelines(const Json::Value &cfg_root,
16571657
std::unique_ptr<ActionProfile> action_profile(
16581658
new ActionProfile(act_prof_name, act_prof_id, with_selection));
16591659
if (with_selection) {
1660-
// TODO(Hao): clean debug logs
1661-
BMLOG_DEBUG(
1662-
"Action profile '{}' with id {} has a selector",
1663-
act_prof_name, act_prof_id);
1664-
16651660
if (is_selector_fanout(cfg_act_prof["selector"])) {
1666-
BMLOG_DEBUG(
1667-
"Action profile '{}' with id {} enabled selector fanout",
1668-
act_prof_name, act_prof_id);
16691661
action_profile->set_selector_fanout();
16701662
} else {
16711663
auto calc = process_cfg_selector(cfg_act_prof["selector"]);
@@ -2514,8 +2506,13 @@ P4Objects::check_hash(const std::string &name) const {
25142506
}
25152507

25162508
bool P4Objects::is_selector_fanout(const Json::Value &cfg_selector) const {
2517-
return cfg_selector.isMember("algo") &&
2509+
bool is_fanout = cfg_selector.isMember("algo") &&
25182510
cfg_selector["algo"].asString() == "selector_fanout";
2511+
if(is_fanout && !FanoutPktMgr::pkt_fanout_on){
2512+
throw std::runtime_error("Selector fanout is not enabled, but"
2513+
" found selector_fanout mode used");
2514+
}
2515+
return is_fanout;
25192516
}
25202517

25212518
std::unique_ptr<Calculation>

src/bm_sim/action_profile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ ActionProfile::mbr_hdl_t
640640
ActionProfile::choose_from_group(grp_hdl_t grp, const Packet &pkt) const {
641641
// TODO(Hao): PI resets to it own grp_selector, might be a bug, so I just sets
642642
// here for now..
643-
if (selector_fanout_enabled) {
643+
if (FanoutPktMgr::pkt_fanout_on && selector_fanout_enabled) {
644644
return FanoutPktMgr::instance().get_grp_selector()->get_from_hash(grp, 0);
645645
}
646646
if (!hash) return grp_selector->get_from_hash(grp, 0);

src/bm_sim/fanout_pkt_mgr.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,6 @@ FanoutPktSelection::get_from_hash(grp_hdl_t grp, hash_t h) const {
5353
return selected_mbr;
5454
}
5555

56-
57-
std::vector<Packet *>& FanoutPktMgr::get_fanout_pkts() {
58-
std::thread::id thread_id = std::this_thread::get_id();
59-
BMLOG_DEBUG("Getting fanout packets for thread {}", thread_id);
60-
61-
std::lock_guard<std::mutex> lock(fanout_pkt_mutex);
62-
auto it = fanout_ctx_map.find(thread_id);
63-
if (it == fanout_ctx_map.end()) {
64-
BMLOG_ERROR("No fanout vector registered for thread {}", thread_id);
65-
throw std::runtime_error("Fanout vector not found for thread");
66-
}
67-
return it->second.fanout_pkts;
68-
}
69-
7056
FanoutCtx& FanoutPktMgr::get_fanout_ctx() {
7157
std::thread::id thread_id = std::this_thread::get_id();
7258
auto it = fanout_ctx_map.find(thread_id);
@@ -98,7 +84,6 @@ void FanoutPktMgr::reset_ctx() {
9884

9985
void FanoutPktMgr::replicate_for_entries(
10086
const std::vector<const ActionEntry*> &entries) {
101-
auto &fanout_pkts = get_fanout_pkts();
10287
auto &ctx = get_fanout_ctx();
10388
auto *match_table = ctx.cur_table;
10489
const Packet &pkt = *ctx.cur_pkt;
@@ -126,7 +111,7 @@ void FanoutPktMgr::replicate_for_entries(
126111
act_id, next_node->get_name());
127112
}
128113
rep_pkt->set_next_node(next_node);
129-
fanout_pkts.push_back(rep_pkt);
114+
ctx.buffer_push_fn(rep_pkt);
130115

131116
BMELOG(fanout_gen, *rep_pkt, table_id, parent_pkt_copy_id);
132117
}

src/bm_sim/match_tables.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,10 +715,14 @@ MatchTableIndirect::lookup(const Packet &pkt,
715715
pkt, "Lookup in table '{}' yielded empty group", get_name());
716716
return empty_action;
717717
}
718-
// A bit hacky, in order to get the next_table for fanout.
719-
if (action_profile->is_selector_fanout_enabled()) {
720-
FanoutPktMgr::instance().set_ctx(this, pkt, action_profile, *hit);
718+
719+
if (FanoutPktMgr::pkt_fanout_on) {
720+
// A bit hacky, in order to get the next_table for fanout.
721+
if (action_profile->is_selector_fanout_enabled()) {
722+
FanoutPktMgr::instance().set_ctx(this, pkt, action_profile, *hit);
723+
}
721724
}
725+
722726
const auto &entry = action_profile->lookup(pkt, index);
723727
// Unfortunately this has to be done at this stage and cannot be done when
724728
// inserting a member because for 2 match tables sharing the same action

targets/simple_switch/simple_switch.cpp

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -278,23 +278,37 @@ SimpleSwitch::receive_(port_t port_num, const char *buffer, int len) {
278278
void
279279
SimpleSwitch::start_and_return_() {
280280
check_queueing_metadata();
281-
#ifdef PKT_FANOUT_ON
282-
auto ingress_thread = std::thread(&SimpleSwitch::ingress_thread, this);
283-
FanoutPktMgr::instance().register_thread(
284-
ingress_thread.get_id());
285-
threads_.push_back(std::move(ingress_thread));
286-
for (size_t i = 0; i < nb_egress_threads; i++) {
287-
auto egress_thread = std::thread(&SimpleSwitch::egress_thread, this, i);
281+
if (FanoutPktMgr::pkt_fanout_on) {
282+
auto ingress_thread = std::thread(&SimpleSwitch::ingress_thread, this);
288283
FanoutPktMgr::instance().register_thread(
289-
egress_thread.get_id());
290-
threads_.push_back(std::move(egress_thread));
291-
}
292-
#else
293-
threads_.push_back(std::thread(&SimpleSwitch::ingress_thread, this));
294-
for (size_t i = 0; i < nb_egress_threads; i++) {
295-
threads_.push_back(std::thread(&SimpleSwitch::egress_thread, this, i));
284+
ingress_thread.get_id(), [&](const bm::Packet *pkt) {
285+
this->input_buffer->push_front(
286+
InputBuffer::PacketType::SELECTOR_FANOUT,
287+
std::unique_ptr<bm::Packet>(const_cast<bm::Packet *>(pkt)));
288+
BMLOG_DEBUG_PKT(*pkt,
289+
"SELECTOR_FANOUT packet pushed to ingress_buffer");
290+
});
291+
292+
threads_.push_back(std::move(ingress_thread));
293+
for (size_t i = 0; i < nb_egress_threads; i++) {
294+
auto egress_thread = std::thread(&SimpleSwitch::egress_thread, this, i);
295+
296+
FanoutPktMgr::instance().register_thread(
297+
egress_thread.get_id(), [&](const bm::Packet *pkt) {
298+
this->egress_buffers.push_front(i, 0,
299+
std::unique_ptr<bm::Packet>(const_cast<bm::Packet *>(pkt)));
300+
BMLOG_DEBUG_PKT(*pkt,
301+
"SELECTOR_FANOUT packet pushed to egress_buffer");
302+
});
303+
304+
threads_.push_back(std::move(egress_thread));
305+
}
306+
} else {
307+
threads_.push_back(std::thread(&SimpleSwitch::ingress_thread, this));
308+
for (size_t i = 0; i < nb_egress_threads; i++) {
309+
threads_.push_back(std::thread(&SimpleSwitch::egress_thread, this, i));
310+
}
296311
}
297-
#endif
298312
threads_.push_back(std::thread(&SimpleSwitch::transmit_thread, this));
299313
}
300314

@@ -524,9 +538,9 @@ SimpleSwitch::ingress_thread() {
524538
deparser. TODO? */
525539
const Packet::buffer_state_t packet_in_state = packet->save_buffer_state();
526540

527-
// Check if the packet has an optional continue node
541+
// Check if the packet has an optional continue node for pkt fanout
528542
// TODO(Hao): update the doc/simple_switch.md
529-
if (packet->has_next_node()) {
543+
if (FanoutPktMgr::pkt_fanout_on && packet->has_next_node()) {
530544
ingress_mau->apply_from_next_node(packet.get());
531545
} else {
532546
parser->parse(packet.get());
@@ -542,20 +556,6 @@ SimpleSwitch::ingress_thread() {
542556

543557
ingress_mau->apply(packet.get());
544558
}
545-
546-
#ifdef PKT_FANOUT_ON
547-
{
548-
auto &fanout_pkts = FanoutPktMgr::instance().get_fanout_pkts();
549-
for (auto pkt : fanout_pkts) {
550-
input_buffer->push_front(InputBuffer::PacketType::SELECTOR_FANOUT,
551-
std::unique_ptr<bm::Packet>(pkt));
552-
BMLOG_DEBUG_PKT(*pkt,
553-
"SELECTOR_FANOUT packet pushed to ingress_buffer");
554-
}
555-
fanout_pkts.clear();
556-
}
557-
#endif
558-
559559
packet->reset_exit();
560560

561561
Field &f_egress_spec = phv->get_field("standard_metadata.egress_spec");
@@ -694,7 +694,8 @@ SimpleSwitch::egress_thread(size_t worker_id) {
694694
Pipeline *egress_mau = this->get_pipeline("egress");
695695

696696
phv = packet->get_phv();
697-
697+
Field &f_egress_spec = phv->get_field("standard_metadata.egress_spec");
698+
698699
if (packet->has_next_node()) {
699700
egress_mau->apply_from_next_node(packet.get());
700701
} else {
@@ -717,7 +718,6 @@ SimpleSwitch::egress_thread(size_t worker_id) {
717718
}
718719

719720
phv->get_field("standard_metadata.egress_port").set(port);
720-
Field &f_egress_spec = phv->get_field("standard_metadata.egress_spec");
721721
// When egress_spec == drop_port the packet will be dropped, thus
722722
// here we initialize egress_spec to a value different from drop_port.
723723
f_egress_spec.set(drop_port + 1);
@@ -728,17 +728,6 @@ SimpleSwitch::egress_thread(size_t worker_id) {
728728
egress_mau->apply(packet.get());
729729
}
730730

731-
#ifdef PKT_FANOUT_ON
732-
{
733-
auto &fanout_pkts = FanoutPktMgr::instance().get_fanout_pkts();
734-
for (auto pkt : fanout_pkts) {
735-
egress_buffers.push_front(worker_id, 0, std::unique_ptr<Packet>(pkt));
736-
BMLOG_DEBUG_PKT(*pkt, "SELECTOR_FANOUT packet pushed to egress_buffer");
737-
}
738-
fanout_pkts.clear();
739-
}
740-
#endif
741-
742731
auto clone_mirror_session_id =
743732
RegisterAccess::get_clone_mirror_session_id(packet.get());
744733
auto clone_field_list = RegisterAccess::get_clone_field_list(packet.get());

0 commit comments

Comments
 (0)