Created
March 17, 2023 10:06
-
-
Save uablrek/5f6cc824de6d03795475b05814495f30 to your computer and use it in GitHub Desktop.
AF_PACKET buffer increase test program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Provided under the MIT No Attribution License | |
| https://opensource.org/license/mit-0/ | |
| Copyright 2023 Lars Ekman <[email protected]> | |
| Permission is hereby granted, free of charge, to any person | |
| obtaining a copy of this software and associated documentation files | |
| (the “Software”), to deal in the Software without restriction, | |
| including without limitation the rights to use, copy, modify, merge, | |
| publish, distribute, sublicense, and/or sell copies of the Software, | |
| and to permit persons to whom the Software is furnished to do so. | |
| THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
| BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
| ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| */ | |
| /* | |
| A simple example of an AF_PACKET (raw) socket for Linux. The program | |
| counts packets and supports a configurable receive buffer size | |
| Some code taken from: | |
| http://www.microhowto.info/howto/capture_ethernet_frames_using_an_af_packet_socket_in_c.html | |
| Syntax; | |
| af_packet [interface] [recv-buffer-size] | |
| Also see; | |
| /proc/sys/net/core/rmem_default | |
| /proc/sys/net/core/rmem_max | |
| Build; | |
| gcc -Wall -Werror -o af_packet af_packet.c | |
| */ | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #include <arpa/inet.h> | |
| #include <net/ethernet.h> | |
| #include <linux/if_packet.h> | |
| #include <sys/ioctl.h> | |
| #include <net/if.h> | |
| int main(int argc, char* argv[]) | |
| { | |
| int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); | |
| if (fd < 0) { | |
| perror("socket"); | |
| return 1; | |
| } | |
| if (argc > 1) { | |
| struct ifreq ifr; | |
| strcpy(ifr.ifr_name, argv[1]); | |
| if (ioctl(fd,SIOCGIFINDEX, &ifr) == -1) { | |
| perror("ioctl"); | |
| return 1; | |
| } | |
| struct sockaddr_ll addr = {0}; | |
| addr.sll_family = AF_PACKET; | |
| addr.sll_ifindex = ifr.ifr_ifindex; | |
| addr.sll_protocol = htons(ETH_P_ALL); | |
| if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { | |
| perror("bind"); | |
| return 1; | |
| } | |
| } | |
| if (argc > 2) { | |
| int n = 0; | |
| socklen_t socklen = sizeof(n); | |
| if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &socklen) != 0) { | |
| perror("getsockopt"); | |
| return 1; | |
| } | |
| int newsize = atoi(argv[2]); | |
| printf("SO_RCVBUF: current = %d, new = %d\n", n, newsize); | |
| if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &newsize, socklen) != 0) { | |
| perror("getsockopt"); | |
| return 1; | |
| } | |
| if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &socklen) != 0) { | |
| perror("getsockopt"); | |
| return 1; | |
| } | |
| printf("SO_RCVBUF: after update = %d\n", n); | |
| } | |
| char buffer[1024*64]; | |
| struct sockaddr_ll src_addr; | |
| socklen_t src_addr_len = sizeof(src_addr); | |
| unsigned packets = 0; | |
| size_t count = recvfrom( | |
| fd, buffer, sizeof(buffer), 0, (struct sockaddr*)&src_addr, &src_addr_len); | |
| while (count > 0) { | |
| packets++; | |
| if (packets % 100 == 0) | |
| printf("Packets %u\r", packets); | |
| fflush(stdout); | |
| count = recvfrom( | |
| fd, buffer, sizeof(buffer), 0, (struct sockaddr*)&src_addr, &src_addr_len); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment