From a69a8e8f3a9ddbd5b2f675d23f1d61b4816ef056 Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Thu, 29 Feb 2024 12:41:14 -0800 Subject: [PATCH 1/8] Packet drop count --- src/dhcp_device.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index 9796a14db..af0e868c4 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -26,6 +26,10 @@ #include #include "subscriberstatetable.h" #include "select.h" +#include +#include +#include +#include #include "dhcp_devman.h" #include "dhcp_device.h" @@ -508,16 +512,14 @@ static dhcp_device_context_t *interface_to_dev_context(std::unordered_map *)arg; ssize_t buffer_sz; struct sockaddr_ll sll; socklen_t slen = sizeof sll; dhcp_device_context_t *context = NULL; - while ((buffer_sz = recvfrom(fd, tx_recv_buffer, snap_length, MSG_DONTWAIT, (struct sockaddr *)&sll, &slen)) > 0) - { + while ((buffer_sz = recvfrom(fd, tx_recv_buffer, snap_length, MSG_DONTWAIT, (struct sockaddr *)&sll, &slen)) > 0) { char interfaceName[IF_NAMESIZE]; if (if_indextoname(sll.sll_ifindex, interfaceName) == NULL) { syslog(LOG_WARNING, "invalid output interface index %d\n", sll.sll_ifindex); @@ -534,6 +536,14 @@ static void read_tx_callback(int fd, short event, void *arg) } } } + + struct tpacket_stats stats; + socklen_t len = sizeof(stats); + if (getsockopt(fd, SOL_PACKET, PACKET_STATISTICS, &stats, &len) == -1) { + syslog(LOG_ERR, "getsockopt failed to retrieve packet statistics"); + } else { + syslog(LOG_INFO, "Total packets: %u, Dropped packets: %u", stats.tp_packets, stats.tp_drops); + } } /** From fb27d4d2c0626675f44b87af8cbd338396753fdb Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Thu, 29 Feb 2024 12:49:16 -0800 Subject: [PATCH 2/8] Update dhcp_device.cpp --- src/dhcp_device.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index af0e868c4..e552af452 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -26,7 +26,6 @@ #include #include "subscriberstatetable.h" #include "select.h" -#include #include #include #include From 8911b5197f2829171b4c7fa97ddda6299e1c2a63 Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Thu, 29 Feb 2024 12:58:42 -0800 Subject: [PATCH 3/8] Update dhcp_device.cpp --- src/dhcp_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index e552af452..c24e331c8 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include "subscriberstatetable.h" #include "select.h" #include From a0fc823be182231178ca4f43214e7889595bd811 Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Fri, 1 Mar 2024 12:44:36 -0800 Subject: [PATCH 4/8] Update dhcp_device.cpp --- src/dhcp_device.cpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index c24e331c8..fefd8edba 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -23,12 +23,9 @@ #include #include #include -#include +#include #include "subscriberstatetable.h" #include "select.h" -#include -#include -#include #include "dhcp_devman.h" #include "dhcp_device.h" @@ -84,6 +81,8 @@ uint64_t db_counter[DHCP_MESSAGE_TYPE_COUNT] = {}; const std::string init_counter_str; +static const size_t new_rcvbuf_size = 16777216; + /** Berkeley Packet Filter program for "udp and (port 67 or port 68)". * This program is obtained using the following command tcpdump: * `tcpdump -dd "outbound and udp and (port 67 or port 68)"` @@ -511,14 +510,16 @@ static dhcp_device_context_t *interface_to_dev_context(std::unordered_map *)arg; ssize_t buffer_sz; struct sockaddr_ll sll; socklen_t slen = sizeof sll; dhcp_device_context_t *context = NULL; - while ((buffer_sz = recvfrom(fd, tx_recv_buffer, snap_length, MSG_DONTWAIT, (struct sockaddr *)&sll, &slen)) > 0) { + while ((buffer_sz = recvfrom(fd, tx_recv_buffer, snap_length, MSG_DONTWAIT, (struct sockaddr *)&sll, &slen)) > 0) + { char interfaceName[IF_NAMESIZE]; if (if_indextoname(sll.sll_ifindex, interfaceName) == NULL) { syslog(LOG_WARNING, "invalid output interface index %d\n", sll.sll_ifindex); @@ -535,14 +536,6 @@ static void read_tx_callback(int fd, short event, void *arg) { } } } - - struct tpacket_stats stats; - socklen_t len = sizeof(stats); - if (getsockopt(fd, SOL_PACKET, PACKET_STATISTICS, &stats, &len) == -1) { - syslog(LOG_ERR, "getsockopt failed to retrieve packet statistics"); - } else { - syslog(LOG_INFO, "Total packets: %u, Dropped packets: %u", stats.tp_packets, stats.tp_drops); - } } /** @@ -978,6 +971,10 @@ int dhcp_device_start_capture(size_t snaplen, struct event_base *base, in_addr_t exit(1); } + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &new_rcvbuf_size, sizeof(new_rcvbuf_size)) == -1) { + syslog(LOG_ALERT, "setsockopt: failed to set rcvbuf size '%s'\n", strerror(errno)); + } + rx_ev = event_new(base, rx_sock, EV_READ | EV_PERSIST, read_rx_callback, &intfs); tx_ev = event_new(base, tx_sock, EV_READ | EV_PERSIST, read_tx_callback, &intfs); From a771789c7d50e1e6986d8b2b7633265b876dd9c7 Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Fri, 1 Mar 2024 12:53:45 -0800 Subject: [PATCH 5/8] Update dhcp_device.cpp --- src/dhcp_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index fefd8edba..3a065fc0b 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -971,7 +971,7 @@ int dhcp_device_start_capture(size_t snaplen, struct event_base *base, in_addr_t exit(1); } - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &new_rcvbuf_size, sizeof(new_rcvbuf_size)) == -1) { + if (setsockopt(tx_sock, SOL_SOCKET, SO_RCVBUF, &new_rcvbuf_size, sizeof(new_rcvbuf_size)) == -1) { syslog(LOG_ALERT, "setsockopt: failed to set rcvbuf size '%s'\n", strerror(errno)); } From 215dac60efaa3e77b545bb0c6cff5af2e708d54f Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Fri, 1 Mar 2024 16:25:24 -0800 Subject: [PATCH 6/8] Update dhcp_device.cpp --- src/dhcp_device.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index 3a065fc0b..b5294e16d 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -81,8 +81,6 @@ uint64_t db_counter[DHCP_MESSAGE_TYPE_COUNT] = {}; const std::string init_counter_str; -static const size_t new_rcvbuf_size = 16777216; - /** Berkeley Packet Filter program for "udp and (port 67 or port 68)". * This program is obtained using the following command tcpdump: * `tcpdump -dd "outbound and udp and (port 67 or port 68)"` @@ -971,8 +969,14 @@ int dhcp_device_start_capture(size_t snaplen, struct event_base *base, in_addr_t exit(1); } - if (setsockopt(tx_sock, SOL_SOCKET, SO_RCVBUF, &new_rcvbuf_size, sizeof(new_rcvbuf_size)) == -1) { - syslog(LOG_ALERT, "setsockopt: failed to set rcvbuf size '%s'\n", strerror(errno)); + size_t maxBufferSize = getMaxBufferSize(); + if (maxBufferSize == 0) { + syslog(LOG_ALERT, "dhcp_device_start_capture: failed to get max buffer size, using default"); + } + else { + if (setsockopt(tx_sock, SOL_SOCKET, SO_RCVBUF, &maxBufferSize, sizeof(maxBufferSize)) == -1) { + syslog(LOG_ALERT, "setsockopt: failed to set rcvbuf size '%s'\n", strerror(errno)); + } } rx_ev = event_new(base, rx_sock, EV_READ | EV_PERSIST, read_rx_callback, &intfs); From 626953135e9c370f76a1ce6ec386cbba8fd69716 Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Fri, 1 Mar 2024 17:07:44 -0800 Subject: [PATCH 7/8] Update dhcp_device.cpp --- src/dhcp_device.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index b5294e16d..7ce9cadbc 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -920,6 +920,22 @@ int dhcp_device_init(dhcp_device_context_t **context, const char *intf, uint8_t return rv; } +/** + * @code getMaxBufferSize(); + * + * @get max buffer size from /proc/sys/net/core/rmem_max. + */ +size_t getMaxBufferSize() { + std::ifstream file("/proc/sys/net/core/rmem_max"); + int maxBufferSize = 0; + if (file) { + file >> maxBufferSize; + } else { + syslog(LOG_ALERT, "Could not open /proc/sys/net/core/rmem_max"); + } + return maxBufferSize; +} + /** * @code dhcp_device_start_capture(snaplen, base, giaddr_ip); * From 2ebab7c12d4fb5864e638f714ea7b9d4df8b248e Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Fri, 1 Mar 2024 17:29:05 -0800 Subject: [PATCH 8/8] Update dhcp_device.cpp --- src/dhcp_device.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index 7ce9cadbc..d23fabbd8 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "subscriberstatetable.h" #include "select.h"