1 // SPDX-License-Identifier: GPL-2.0-only
11 #include <arpa/inet.h>
12 #include <sys/mount.h>
14 #include <sys/types.h>
16 #include <sys/eventfd.h>
18 #include <linux/err.h>
20 #include <linux/in6.h>
21 #include <linux/limits.h>
24 #include <linux/udp.h>
25 #include <netinet/tcp.h>
29 #include "network_helpers.h"
30 #include "test_progs.h"
32 #ifdef TRAFFIC_MONITOR
33 /* Prevent pcap.h from including pcap/bpf.h and causing conflicts */
34 #define PCAP_DONT_INCLUDE_PCAP_BPF_H 1
35 #include <pcap/pcap.h>
40 #define IPPROTO_MPTCP 262
43 #define clean_errno() (errno == 0 ? "None" : strerror(errno))
44 #define log_err(MSG, ...) ({ \
46 fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
47 __FILE__, __LINE__, clean_errno(), \
52 struct ipv4_packet pkt_v4 = {
53 .eth.h_proto = __bpf_constant_htons(ETH_P_IP),
55 .iph.protocol = IPPROTO_TCP,
56 .iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
61 struct ipv6_packet pkt_v6 = {
62 .eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
63 .iph.nexthdr = IPPROTO_TCP,
64 .iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
69 static const struct network_helper_opts default_opts;
71 int settimeo(int fd, int timeout_ms)
73 struct timeval timeout = { .tv_sec = 3 };
76 timeout.tv_sec = timeout_ms / 1000;
77 timeout.tv_usec = (timeout_ms % 1000) * 1000;
80 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
82 log_err("Failed to set SO_RCVTIMEO");
86 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout,
88 log_err("Failed to set SO_SNDTIMEO");
95 #define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; })
97 int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
98 const struct network_helper_opts *opts)
103 opts = &default_opts;
105 fd = socket(addr->ss_family, type, opts->proto);
107 log_err("Failed to create server socket");
111 if (settimeo(fd, opts->timeout_ms))
114 if (opts->post_socket_cb &&
115 opts->post_socket_cb(fd, opts->cb_opts)) {
116 log_err("Failed to call post_socket_cb");
120 if (bind(fd, (struct sockaddr *)addr, addrlen) < 0) {
121 log_err("Failed to bind socket");
125 if (type == SOCK_STREAM) {
126 if (listen(fd, opts->backlog ? MAX(opts->backlog, 0) : 1) < 0) {
127 log_err("Failed to listed on socket");
135 save_errno_close(fd);
139 int start_server_str(int family, int type, const char *addr_str, __u16 port,
140 const struct network_helper_opts *opts)
142 struct sockaddr_storage addr;
146 opts = &default_opts;
148 if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
151 return start_server_addr(type, &addr, addrlen, opts);
154 int start_server(int family, int type, const char *addr_str, __u16 port,
157 struct network_helper_opts opts = {
158 .timeout_ms = timeout_ms,
161 return start_server_str(family, type, addr_str, port, &opts);
164 static int reuseport_cb(int fd, void *opts)
168 return setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
171 int *start_reuseport_server(int family, int type, const char *addr_str,
172 __u16 port, int timeout_ms, unsigned int nr_listens)
174 struct network_helper_opts opts = {
175 .timeout_ms = timeout_ms,
176 .post_socket_cb = reuseport_cb,
178 struct sockaddr_storage addr;
179 unsigned int nr_fds = 0;
186 if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
189 fds = malloc(sizeof(*fds) * nr_listens);
193 fds[0] = start_server_addr(type, &addr, addrlen, &opts);
198 if (getsockname(fds[0], (struct sockaddr *)&addr, &addrlen))
201 for (; nr_fds < nr_listens; nr_fds++) {
202 fds[nr_fds] = start_server_addr(type, &addr, addrlen, &opts);
203 if (fds[nr_fds] == -1)
210 free_fds(fds, nr_fds);
214 void free_fds(int *fds, unsigned int nr_close_fds)
218 close(fds[--nr_close_fds]);
223 int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
226 struct sockaddr_storage addr;
227 socklen_t addrlen = sizeof(addr);
228 struct sockaddr_in *addr_in;
231 if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
232 log_err("Failed to get server addr");
236 addr_in = (struct sockaddr_in *)&addr;
237 fd = socket(addr_in->sin_family, SOCK_STREAM, 0);
239 log_err("Failed to create client socket");
243 if (settimeo(fd, timeout_ms))
246 ret = sendto(fd, data, data_len, MSG_FASTOPEN, (struct sockaddr *)&addr,
248 if (ret != data_len) {
249 log_err("sendto(data, %u) != %d\n", data_len, ret);
256 save_errno_close(fd);
260 int client_socket(int family, int type,
261 const struct network_helper_opts *opts)
266 opts = &default_opts;
268 fd = socket(family, type, opts->proto);
270 log_err("Failed to create client socket");
274 if (settimeo(fd, opts->timeout_ms))
277 if (opts->post_socket_cb &&
278 opts->post_socket_cb(fd, opts->cb_opts))
284 save_errno_close(fd);
288 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
289 const struct network_helper_opts *opts)
294 opts = &default_opts;
296 fd = client_socket(addr->ss_family, type, opts);
298 log_err("Failed to create client socket");
302 if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
303 log_err("Failed to connect to server");
304 save_errno_close(fd);
311 int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
312 const struct network_helper_opts *opts)
314 struct sockaddr_storage addr;
318 opts = &default_opts;
320 if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
323 return connect_to_addr(type, &addr, addrlen, opts);
326 int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
328 struct sockaddr_storage addr;
329 socklen_t addrlen, optlen;
333 opts = &default_opts;
335 optlen = sizeof(type);
336 if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
337 log_err("getsockopt(SOL_TYPE)");
341 addrlen = sizeof(addr);
342 if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
343 log_err("Failed to get server addr");
347 return connect_to_addr(type, &addr, addrlen, opts);
350 int connect_to_fd(int server_fd, int timeout_ms)
352 struct network_helper_opts opts = {
353 .timeout_ms = timeout_ms,
358 optlen = sizeof(protocol);
359 if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
360 log_err("getsockopt(SOL_PROTOCOL)");
363 opts.proto = protocol;
365 return connect_to_fd_opts(server_fd, &opts);
368 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
370 struct sockaddr_storage addr;
371 socklen_t len = sizeof(addr);
373 if (settimeo(client_fd, timeout_ms))
376 if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
377 log_err("Failed to get server addr");
381 if (connect(client_fd, (const struct sockaddr *)&addr, len)) {
382 log_err("Failed to connect to server");
389 int make_sockaddr(int family, const char *addr_str, __u16 port,
390 struct sockaddr_storage *addr, socklen_t *len)
392 if (family == AF_INET) {
393 struct sockaddr_in *sin = (void *)addr;
395 memset(addr, 0, sizeof(*sin));
396 sin->sin_family = AF_INET;
397 sin->sin_port = htons(port);
399 inet_pton(AF_INET, addr_str, &sin->sin_addr) != 1) {
400 log_err("inet_pton(AF_INET, %s)", addr_str);
406 } else if (family == AF_INET6) {
407 struct sockaddr_in6 *sin6 = (void *)addr;
409 memset(addr, 0, sizeof(*sin6));
410 sin6->sin6_family = AF_INET6;
411 sin6->sin6_port = htons(port);
413 inet_pton(AF_INET6, addr_str, &sin6->sin6_addr) != 1) {
414 log_err("inet_pton(AF_INET6, %s)", addr_str);
418 *len = sizeof(*sin6);
420 } else if (family == AF_UNIX) {
421 /* Note that we always use abstract unix sockets to avoid having
422 * to clean up leftover files.
424 struct sockaddr_un *sun = (void *)addr;
426 memset(addr, 0, sizeof(*sun));
427 sun->sun_family = family;
428 sun->sun_path[0] = 0;
429 strcpy(sun->sun_path + 1, addr_str);
431 *len = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(addr_str);
437 char *ping_command(int family)
439 if (family == AF_INET6) {
440 /* On some systems 'ping' doesn't support IPv6, so use ping6 if it is present. */
441 if (!system("which ping6 >/dev/null 2>&1"))
449 int remove_netns(const char *name)
454 r = asprintf(&cmd, "ip netns del %s >/dev/null 2>&1", name);
456 log_err("Failed to malloc cmd");
465 int make_netns(const char *name)
470 r = asprintf(&cmd, "ip netns add %s", name);
472 log_err("Failed to malloc cmd");
482 r = asprintf(&cmd, "ip -n %s link set lo up", name);
484 log_err("Failed to malloc cmd for setting up lo");
499 struct nstoken *open_netns(const char *name)
502 char nspath[PATH_MAX];
504 struct nstoken *token;
506 token = calloc(1, sizeof(struct nstoken));
508 log_err("Failed to malloc token");
512 token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
513 if (token->orig_netns_fd == -1) {
514 log_err("Failed to open(/proc/self/ns/net)");
518 snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name);
519 nsfd = open(nspath, O_RDONLY | O_CLOEXEC);
521 log_err("Failed to open(%s)", nspath);
525 err = setns(nsfd, CLONE_NEWNET);
528 log_err("Failed to setns(nsfd)");
534 if (token->orig_netns_fd != -1)
535 close(token->orig_netns_fd);
540 void close_netns(struct nstoken *token)
545 if (setns(token->orig_netns_fd, CLONE_NEWNET))
546 log_err("Failed to setns(orig_netns_fd)");
547 close(token->orig_netns_fd);
551 int get_socket_local_port(int sock_fd)
553 struct sockaddr_storage addr;
554 socklen_t addrlen = sizeof(addr);
557 err = getsockname(sock_fd, (struct sockaddr *)&addr, &addrlen);
561 if (addr.ss_family == AF_INET) {
562 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
564 return sin->sin_port;
565 } else if (addr.ss_family == AF_INET6) {
566 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&addr;
568 return sin->sin6_port;
574 int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param)
576 struct ifreq ifr = {0};
579 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
583 memcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
585 ring_param->cmd = ETHTOOL_GRINGPARAM;
586 ifr.ifr_data = (char *)ring_param;
588 if (ioctl(sockfd, SIOCETHTOOL, &ifr) < 0) {
598 int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param)
600 struct ifreq ifr = {0};
603 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
607 memcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
609 ring_param->cmd = ETHTOOL_SRINGPARAM;
610 ifr.ifr_data = (char *)ring_param;
612 if (ioctl(sockfd, SIOCETHTOOL, &ifr) < 0) {
622 struct send_recv_arg {
628 static void *send_recv_server(void *arg)
630 struct send_recv_arg *a = (struct send_recv_arg *)arg;
631 ssize_t nr_sent = 0, bytes = 0;
635 fd = accept(a->fd, NULL, NULL);
643 if (settimeo(fd, 0)) {
648 while (bytes < a->bytes && !READ_ONCE(a->stop)) {
649 nr_sent = send(fd, &batch,
650 MIN(a->bytes - bytes, sizeof(batch)), 0);
651 if (nr_sent == -1 && errno == EINTR)
660 if (bytes != a->bytes) {
661 log_err("send %zd expected %u", bytes, a->bytes);
663 err = bytes > a->bytes ? -E2BIG : -EINTR;
670 WRITE_ONCE(a->stop, 1);
676 int send_recv_data(int lfd, int fd, uint32_t total_bytes)
678 ssize_t nr_recv = 0, bytes = 0;
679 struct send_recv_arg arg = {
681 .bytes = total_bytes,
684 pthread_t srv_thread;
689 err = pthread_create(&srv_thread, NULL, send_recv_server, (void *)&arg);
691 log_err("Failed to pthread_create");
695 /* recv total_bytes */
696 while (bytes < total_bytes && !READ_ONCE(arg.stop)) {
697 nr_recv = recv(fd, &batch,
698 MIN(total_bytes - bytes, sizeof(batch)), 0);
699 if (nr_recv == -1 && errno == EINTR)
708 if (bytes != total_bytes) {
709 log_err("recv %zd expected %u", bytes, total_bytes);
711 err = bytes > total_bytes ? -E2BIG : -EINTR;
714 WRITE_ONCE(arg.stop, 1);
715 pthread_join(srv_thread, &thread_ret);
716 if (IS_ERR(thread_ret)) {
717 log_err("Failed in thread_ret %ld", PTR_ERR(thread_ret));
718 err = err ? : PTR_ERR(thread_ret);
724 #ifdef TRAFFIC_MONITOR
725 struct tmonitor_ctx {
727 pcap_dumper_t *dumper;
732 char pkt_fname[PATH_MAX];
736 /* Is this packet captured with a Ethernet protocol type? */
737 static bool is_ethernet(const u_char *packet)
741 memcpy(&arphdr_type, packet + 8, 2);
742 arphdr_type = ntohs(arphdr_type);
744 /* Except the following cases, the protocol type contains the
745 * Ethernet protocol type for the packet.
747 * https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html
749 switch (arphdr_type) {
750 case 770: /* ARPHRD_FRAD */
751 case 778: /* ARPHDR_IPGRE */
752 case 803: /* ARPHRD_IEEE80211_RADIOTAP */
753 printf("Packet captured: arphdr_type=%d\n", arphdr_type);
759 static const char * const pkt_types[] = {
763 "C", /* Captured with the promiscuous mode */
767 static const char *pkt_type_str(u16 pkt_type)
769 if (pkt_type < ARRAY_SIZE(pkt_types))
770 return pkt_types[pkt_type];
774 /* Show the information of the transport layer in the packet */
775 static void show_transport(const u_char *packet, u16 len, u32 ifindex,
776 const char *src_addr, const char *dst_addr,
777 u16 proto, bool ipv6, u8 pkt_type)
779 char *ifname, _ifname[IF_NAMESIZE];
780 const char *transport_str;
781 u16 src_port, dst_port;
785 ifname = if_indextoname(ifindex, _ifname);
787 snprintf(_ifname, sizeof(_ifname), "unknown(%d)", ifindex);
791 if (proto == IPPROTO_UDP) {
792 udp = (struct udphdr *)packet;
793 src_port = ntohs(udp->source);
794 dst_port = ntohs(udp->dest);
795 transport_str = "UDP";
796 } else if (proto == IPPROTO_TCP) {
797 tcp = (struct tcphdr *)packet;
798 src_port = ntohs(tcp->source);
799 dst_port = ntohs(tcp->dest);
800 transport_str = "TCP";
801 } else if (proto == IPPROTO_ICMP) {
802 printf("%-7s %-3s IPv4 %s > %s: ICMP, length %d, type %d, code %d\n",
803 ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
804 packet[0], packet[1]);
806 } else if (proto == IPPROTO_ICMPV6) {
807 printf("%-7s %-3s IPv6 %s > %s: ICMPv6, length %d, type %d, code %d\n",
808 ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
809 packet[0], packet[1]);
812 printf("%-7s %-3s %s %s > %s: protocol %d\n",
813 ifname, pkt_type_str(pkt_type), ipv6 ? "IPv6" : "IPv4",
814 src_addr, dst_addr, proto);
822 printf("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d",
823 ifname, pkt_type_str(pkt_type), src_addr, src_port,
824 dst_addr, dst_port, transport_str, len);
826 printf("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d",
827 ifname, pkt_type_str(pkt_type), src_addr, src_port,
828 dst_addr, dst_port, transport_str, len);
830 if (proto == IPPROTO_TCP) {
845 static void show_ipv6_packet(const u_char *packet, u32 ifindex, u8 pkt_type)
847 char src_buf[INET6_ADDRSTRLEN], dst_buf[INET6_ADDRSTRLEN];
848 struct ipv6hdr *pkt = (struct ipv6hdr *)packet;
849 const char *src, *dst;
852 src = inet_ntop(AF_INET6, &pkt->saddr, src_buf, sizeof(src_buf));
855 dst = inet_ntop(AF_INET6, &pkt->daddr, dst_buf, sizeof(dst_buf));
858 proto = pkt->nexthdr;
859 show_transport(packet + sizeof(struct ipv6hdr),
860 ntohs(pkt->payload_len),
861 ifindex, src, dst, proto, true, pkt_type);
864 static void show_ipv4_packet(const u_char *packet, u32 ifindex, u8 pkt_type)
866 char src_buf[INET_ADDRSTRLEN], dst_buf[INET_ADDRSTRLEN];
867 struct iphdr *pkt = (struct iphdr *)packet;
868 const char *src, *dst;
871 src = inet_ntop(AF_INET, &pkt->saddr, src_buf, sizeof(src_buf));
874 dst = inet_ntop(AF_INET, &pkt->daddr, dst_buf, sizeof(dst_buf));
877 proto = pkt->protocol;
878 show_transport(packet + sizeof(struct iphdr),
880 ifindex, src, dst, proto, false, pkt_type);
883 static void *traffic_monitor_thread(void *arg)
885 char *ifname, _ifname[IF_NAMESIZE];
886 const u_char *packet, *payload;
887 struct tmonitor_ctx *ctx = arg;
888 pcap_dumper_t *dumper = ctx->dumper;
889 int fd = ctx->pcap_fd, nfds, r;
890 int wake_fd = ctx->wake_fd;
891 struct pcap_pkthdr header;
892 pcap_t *pcap = ctx->pcap;
898 nfds = (fd > wake_fd ? fd : wake_fd) + 1;
903 FD_SET(wake_fd, &fds);
904 r = select(nfds, &fds, NULL, NULL, NULL);
910 log_err("Fail to select on pcap fd and wake fd");
914 /* This instance of pcap is non-blocking */
915 packet = pcap_next(pcap, &header);
919 /* According to the man page of pcap_dump(), first argument
920 * is the pcap_dumper_t pointer even it's argument type is
923 pcap_dump((u_char *)dumper, &header, packet);
925 /* Not sure what other types of packets look like. Here, we
926 * parse only Ethernet and compatible packets.
928 if (!is_ethernet(packet))
932 * https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html
934 * Although the document doesn't mention that, the payload
935 * doesn't include the Ethernet header. The payload starts
936 * from the first byte of the network layer header.
938 payload = packet + 20;
940 memcpy(&proto, packet, 2);
941 proto = ntohs(proto);
942 memcpy(&ifindex, packet + 4, 4);
943 ifindex = ntohl(ifindex);
946 if (proto == ETH_P_IPV6) {
947 show_ipv6_packet(payload, ifindex, ptype);
948 } else if (proto == ETH_P_IP) {
949 show_ipv4_packet(payload, ifindex, ptype);
951 ifname = if_indextoname(ifindex, _ifname);
953 snprintf(_ifname, sizeof(_ifname), "unknown(%d)", ifindex);
957 printf("%-7s %-3s Unknown network protocol type 0x%x\n",
958 ifname, pkt_type_str(ptype), proto);
965 /* Prepare the pcap handle to capture packets.
967 * This pcap is non-blocking and immediate mode is enabled to receive
968 * captured packets as soon as possible. The snaplen is set to 1024 bytes
969 * to limit the size of captured content. The format of the link-layer
970 * header is set to DLT_LINUX_SLL2 to enable handling various link-layer
973 static pcap_t *traffic_monitor_prepare_pcap(void)
975 char errbuf[PCAP_ERRBUF_SIZE];
979 /* Listen on all NICs in the namespace */
980 pcap = pcap_create("any", errbuf);
982 log_err("Failed to open pcap: %s", errbuf);
985 /* Limit the size of the packet (first N bytes) */
986 r = pcap_set_snaplen(pcap, 1024);
988 log_err("Failed to set snaplen: %s", pcap_geterr(pcap));
991 /* To receive packets as fast as possible */
992 r = pcap_set_immediate_mode(pcap, 1);
994 log_err("Failed to set immediate mode: %s", pcap_geterr(pcap));
997 r = pcap_setnonblock(pcap, 1, errbuf);
999 log_err("Failed to set nonblock: %s", errbuf);
1002 r = pcap_activate(pcap);
1004 log_err("Failed to activate pcap: %s", pcap_geterr(pcap));
1007 /* Determine the format of the link-layer header */
1008 r = pcap_set_datalink(pcap, DLT_LINUX_SLL2);
1010 log_err("Failed to set datalink: %s", pcap_geterr(pcap));
1020 static void encode_test_name(char *buf, size_t len, const char *test_name, const char *subtest_name)
1025 snprintf(buf, len, "%s__%s", test_name, subtest_name);
1027 snprintf(buf, len, "%s", test_name);
1028 while ((p = strchr(buf, '/')))
1030 while ((p = strchr(buf, ' ')))
1034 #define PCAP_DIR "/tmp/tmon_pcap"
1036 /* Start to monitor the network traffic in the given network namespace.
1038 * netns: the name of the network namespace to monitor. If NULL, the
1039 * current network namespace is monitored.
1040 * test_name: the name of the running test.
1041 * subtest_name: the name of the running subtest if there is. It should be
1042 * NULL if it is not a subtest.
1044 * This function will start a thread to capture packets going through NICs
1045 * in the give network namespace.
1047 struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
1048 const char *subtest_name)
1050 struct nstoken *nstoken = NULL;
1051 struct tmonitor_ctx *ctx;
1052 char test_name_buf[64];
1053 static int tmon_seq;
1057 nstoken = open_netns(netns);
1061 ctx = malloc(sizeof(*ctx));
1063 log_err("Failed to malloc ctx");
1066 memset(ctx, 0, sizeof(*ctx));
1068 encode_test_name(test_name_buf, sizeof(test_name_buf), test_name, subtest_name);
1069 snprintf(ctx->pkt_fname, sizeof(ctx->pkt_fname),
1070 PCAP_DIR "/packets-%d-%d-%s-%s.log", getpid(), tmon_seq++,
1071 test_name_buf, netns ? netns : "unknown");
1073 r = mkdir(PCAP_DIR, 0755);
1074 if (r && errno != EEXIST) {
1075 log_err("Failed to create " PCAP_DIR);
1079 ctx->pcap = traffic_monitor_prepare_pcap();
1082 ctx->pcap_fd = pcap_get_selectable_fd(ctx->pcap);
1083 if (ctx->pcap_fd < 0) {
1084 log_err("Failed to get pcap fd");
1088 /* Create a packet file */
1089 ctx->dumper = pcap_dump_open(ctx->pcap, ctx->pkt_fname);
1091 log_err("Failed to open pcap dump: %s", ctx->pkt_fname);
1095 /* Create an eventfd to wake up the monitor thread */
1096 ctx->wake_fd = eventfd(0, 0);
1097 if (ctx->wake_fd < 0) {
1098 log_err("Failed to create eventfd");
1102 r = pthread_create(&ctx->thread, NULL, traffic_monitor_thread, ctx);
1104 log_err("Failed to create thread");
1108 close_netns(nstoken);
1113 close(ctx->wake_fd);
1116 pcap_dump_close(ctx->dumper);
1117 unlink(ctx->pkt_fname);
1120 pcap_close(ctx->pcap);
1126 close_netns(nstoken);
1131 static void traffic_monitor_release(struct tmonitor_ctx *ctx)
1133 pcap_close(ctx->pcap);
1134 pcap_dump_close(ctx->dumper);
1136 close(ctx->wake_fd);
1141 /* Stop the network traffic monitor.
1143 * ctx: the context returned by traffic_monitor_start()
1145 void traffic_monitor_stop(struct tmonitor_ctx *ctx)
1152 /* Stop the monitor thread */
1154 /* Wake up the background thread. */
1155 write(ctx->wake_fd, &w, sizeof(w));
1156 pthread_join(ctx->thread, NULL);
1158 printf("Packet file: %s\n", strrchr(ctx->pkt_fname, '/') + 1);
1160 traffic_monitor_release(ctx);
1162 #endif /* TRAFFIC_MONITOR */