Skip to content

Commit 1eaad26

Browse files
committed
GTPU: Remove some double map lookups
1 parent 9815696 commit 1eaad26

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

lib/gtpu/gtpu_demux_impl.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,51 @@ bool gtpu_demux_impl::add_tunnel(gtpu_teid_t te
2525
gtpu_tunnel_common_rx_upper_layer_interface* tunnel)
2626
{
2727
std::lock_guard<std::mutex> guard(map_mutex);
28-
if (teid_to_tunnel.find(teid) != teid_to_tunnel.end()) {
28+
auto it = teid_to_tunnel.try_emplace(teid, gtpu_demux_tunnel_ctx_t{&tunnel_exec, tunnel});
29+
if (not it.second) {
2930
logger.error("Tunnel already exists. teid={}", teid);
3031
return false;
3132
}
33+
3234
logger.info("Tunnel added. teid={}", teid);
33-
teid_to_tunnel[teid] = {&tunnel_exec, tunnel};
3435
return true;
3536
}
3637

3738
bool gtpu_demux_impl::remove_tunnel(gtpu_teid_t teid)
3839
{
3940
std::lock_guard<std::mutex> guard(map_mutex);
40-
if (teid_to_tunnel.find(teid) == teid_to_tunnel.end()) {
41+
auto it = teid_to_tunnel.find(teid);
42+
if (it == teid_to_tunnel.end()) {
4143
logger.error("Tunnel not found. teid={}", teid);
4244
return false;
4345
}
46+
4447
logger.info("Tunnel removed. teid={}", teid);
45-
teid_to_tunnel.erase(teid);
48+
teid_to_tunnel.erase(it);
4649
return true;
4750
}
4851

4952
void gtpu_demux_impl::handle_pdu(byte_buffer pdu, const sockaddr_storage& src_addr)
5053
{
5154
uint32_t read_teid = 0x01; // default to test DRB
52-
if (!cfg.test_mode) {
53-
if (!gtpu_read_teid(read_teid, pdu, logger)) {
55+
if (not cfg.test_mode) {
56+
if (not gtpu_read_teid(read_teid, pdu, logger)) {
5457
logger.error("Failed to read TEID from GTP-U PDU. pdu_len={}", pdu.length());
5558
return;
5659
}
5760
}
58-
gtpu_teid_t teid{read_teid};
5961

6062
std::lock_guard<std::mutex> guard(map_mutex);
61-
const auto& it = teid_to_tunnel.find(teid);
63+
64+
gtpu_teid_t teid{read_teid};
65+
auto it = teid_to_tunnel.find(teid);
6266
if (it == teid_to_tunnel.end()) {
6367
logger.info("Dropped GTP-U PDU, tunnel not found. teid={}", teid);
6468
return;
6569
}
6670

67-
auto fn = [this, teid, p = std::move(pdu), src_addr]() mutable { handle_pdu_impl(teid, std::move(p), src_addr); };
68-
69-
if (not it->second.tunnel_exec->defer(std::move(fn))) {
71+
if (not it->second.tunnel_exec->defer(
72+
[this, teid, p = std::move(pdu), src_addr]() mutable { handle_pdu_impl(teid, std::move(p), src_addr); })) {
7073
if (not cfg.warn_on_drop) {
7174
logger.info("Dropped GTP-U PDU, queue is full. teid={}", teid);
7275
} else {
@@ -90,19 +93,18 @@ void gtpu_demux_impl::handle_pdu_impl(gtpu_teid_t teid, byte_buffer pdu, const s
9093

9194
gtpu_tunnel_common_rx_upper_layer_interface* tunnel = nullptr;
9295
{
93-
/// Get GTP-U tunnel. We lookup the tunnel again, as the tunnel could have been
94-
/// removed between the time PDU processing was enqueued and the time we actually
95-
/// run the task.
96+
// Get GTP-U tunnel.
97+
// We lookup the tunnel again, as the tunnel could have been removed between the time PDU processing was enqueued
98+
// and the time we actually run the task.
9699
std::lock_guard<std::mutex> guard(map_mutex);
97-
const auto& it = teid_to_tunnel.find(teid);
100+
auto it = teid_to_tunnel.find(teid);
98101
if (it == teid_to_tunnel.end()) {
99102
logger.info("Dropped GTP-U PDU, tunnel not found. teid={}", teid);
100103
return;
101104
}
102105
tunnel = it->second.tunnel;
103106
}
104-
// Forward entire PDU to the tunnel
105-
// As removal happens in the same thread as handling the PDU, we no longer
106-
// need the lock.
107+
// Forward entire PDU to the tunnel.
108+
// As removal happens in the same thread as handling the PDU, we no longer need the lock.
107109
tunnel->handle_pdu(std::move(pdu), src_addr);
108110
}

0 commit comments

Comments
 (0)