33 #define SC_PCAP_DONT_INCLUDE_PCAP_H 1
38 #ifdef HAVE_PACKET_EBPF
41 #include <sys/resource.h>
55 #include <bpf/libbpf.h>
60 #define BPF_MAP_MAX_COUNT 16
72 struct bpf_maps_info {
73 struct bpf_map_item array[BPF_MAP_MAX_COUNT];
77 typedef struct BypassedIfaceList_ {
79 struct BypassedIfaceList_ *
next;
82 static void BpfMapsInfoFree(
void *bpf)
84 struct bpf_maps_info *bpfinfo = (
struct bpf_maps_info *)bpf;
86 for (i = 0; i < bpfinfo->last; i ++) {
87 if (bpfinfo->array[i].name) {
88 if (bpfinfo->array[i].to_unlink) {
89 char pinnedpath[PATH_MAX];
90 int ret = snprintf(pinnedpath,
sizeof(pinnedpath),
91 "/sys/fs/bpf/suricata-%s-%s",
92 bpfinfo->array[i].iface,
93 bpfinfo->array[i].name);
96 ret = unlink(pinnedpath);
100 "Unable to remove %s: %s (%d)", pinnedpath, strerror(error), error);
103 SCLogWarning(
"Unable to remove map %s", bpfinfo->array[i].name);
106 SCFree(bpfinfo->array[i].name);
112 static void BypassedListFree(
void *ifl)
114 BypassedIfaceList *mifl = (BypassedIfaceList *)ifl;
115 BypassedIfaceList *nifl;
123 void EBPFDeleteKey(
int fd,
void *key)
125 int ret = bpf_map_delete_elem(fd, key);
127 SCLogWarning(
"Unable to delete entry: %s (%d)", strerror(errno), errno);
131 static struct bpf_maps_info *EBPFGetBpfMap(
const char *iface)
138 return (
struct bpf_maps_info *)data;
148 int EBPFGetMapFDByName(
const char *iface,
const char *
name)
152 if (iface == NULL ||
name == NULL)
154 struct bpf_maps_info *bpf_maps = EBPFGetBpfMap(iface);
155 if (bpf_maps == NULL)
158 for (i = 0; i < BPF_MAP_MAX_COUNT; i++) {
159 if (!bpf_maps->array[i].name)
161 if (!strcmp(bpf_maps->array[i].name,
name)) {
162 SCLogDebug(
"Got fd %d for eBPF map '%s'", bpf_maps->array[i].fd,
name);
163 return bpf_maps->array[i].fd;
170 static int EBPFLoadPinnedMapsFile(
LiveDevice *livedev,
const char *file)
172 char pinnedpath[1024];
173 snprintf(pinnedpath,
sizeof(pinnedpath),
174 "/sys/fs/bpf/suricata-%s-%s",
178 return bpf_obj_get(pinnedpath);
181 static int EBPFLoadPinnedMaps(
LiveDevice *livedev,
struct ebpf_timeout_config *config)
183 int fd_v4 = -1, fd_v6 = -1;
186 if (config->pinned_maps_name) {
187 int ret = EBPFLoadPinnedMapsFile(livedev, config->pinned_maps_name);
194 if (config->mode == AFP_MODE_XDP_BYPASS) {
196 fd_v4 = EBPFLoadPinnedMapsFile(livedev,
"flow_table_v4");
202 fd_v6 = EBPFLoadPinnedMapsFile(livedev,
"flow_table_v6");
204 SCLogWarning(
"Found a flow_table_v4 map but no flow_table_v6 map");
209 struct bpf_maps_info *bpf_map_data =
SCCalloc(1,
sizeof(*bpf_map_data));
210 if (bpf_map_data == NULL) {
215 if (config->mode == AFP_MODE_XDP_BYPASS) {
216 bpf_map_data->array[0].fd = fd_v4;
217 bpf_map_data->array[0].name =
SCStrdup(
"flow_table_v4");
218 if (bpf_map_data->array[0].name == NULL) {
221 bpf_map_data->array[1].fd = fd_v6;
222 bpf_map_data->array[1].name =
SCStrdup(
"flow_table_v6");
223 if (bpf_map_data->array[1].name == NULL) {
226 bpf_map_data->last = 2;
228 bpf_map_data->last = 0;
232 int fd = EBPFLoadPinnedMapsFile(livedev,
"cpu_map");
234 bpf_map_data->array[bpf_map_data->last].fd = fd;
235 bpf_map_data->array[bpf_map_data->last].name =
SCStrdup(
"cpu_map");
236 if (bpf_map_data->array[bpf_map_data->last].name == NULL) {
239 bpf_map_data->last++;
241 fd = EBPFLoadPinnedMapsFile(livedev,
"cpus_available");
243 bpf_map_data->array[bpf_map_data->last].fd = fd;
244 bpf_map_data->array[bpf_map_data->last].name =
SCStrdup(
"cpus_available");
245 if (bpf_map_data->array[bpf_map_data->last].name == NULL) {
248 bpf_map_data->last++;
250 fd = EBPFLoadPinnedMapsFile(livedev,
"tx_peer");
252 bpf_map_data->array[bpf_map_data->last].fd = fd;
253 bpf_map_data->array[bpf_map_data->last].name =
SCStrdup(
"tx_peer");
254 if (bpf_map_data->array[bpf_map_data->last].name == NULL) {
257 bpf_map_data->last++;
259 fd = EBPFLoadPinnedMapsFile(livedev,
"tx_peer_int");
261 bpf_map_data->array[bpf_map_data->last].fd = fd;
262 bpf_map_data->array[bpf_map_data->last].name =
SCStrdup(
"tx_peer_int");
263 if (bpf_map_data->array[bpf_map_data->last].name == NULL) {
266 bpf_map_data->last++;
277 for (
int i = 0; i < bpf_map_data->last; i++) {
278 SCFree(bpf_map_data->array[i].name);
280 bpf_map_data->last = 0;
297 int EBPFLoadFile(
const char *iface,
const char *path,
const char * section,
298 int *val,
struct ebpf_timeout_config *config)
302 struct bpf_object *bpfobj = NULL;
303 struct bpf_program *bpfprog = NULL;
304 struct bpf_map *map = NULL;
312 if (config->flags & EBPF_XDP_CODE && config->flags & EBPF_PINNED_MAPS) {
314 if (EBPFLoadPinnedMaps(livedev, config) == 0) {
315 SCLogInfo(
"Loaded pinned maps, will use already loaded eBPF filter");
321 SCLogError(
"No file defined to load eBPF from");
329 struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
330 if (setrlimit(RLIMIT_MEMLOCK, &r) != 0) {
331 SCLogError(
"Unable to lock memory: %s (%d)", strerror(errno), errno);
337 bpfobj = bpf_object__open(path);
338 long error = libbpf_get_error(bpfobj);
341 libbpf_strerror(error, err_buf,
343 SCLogError(
"Unable to load eBPF objects in '%s': %s", path, err_buf);
347 if (config->flags & EBPF_XDP_HW_MODE) {
348 unsigned int ifindex = if_nametoindex(iface);
349 bpf_object__for_each_program(bpfprog, bpfobj) {
350 bpf_program__set_ifindex(bpfprog, ifindex);
352 bpf_map__for_each(map, bpfobj) {
353 bpf_map__set_ifindex(map, ifindex);
358 bpf_object__for_each_program(bpfprog, bpfobj) {
359 #ifdef HAVE_BPF_PROGRAM__SECTION_NAME
360 const char *title = bpf_program__section_name(bpfprog);
362 const char *title = bpf_program__title(bpfprog, 0);
364 if (!strcmp(title, section)) {
365 if (config->flags & EBPF_SOCKET_FILTER) {
366 #ifdef HAVE_BPF_PROGRAM__SET_TYPE
367 bpf_program__set_type(bpfprog, BPF_PROG_TYPE_SOCKET_FILTER);
370 bpf_program__set_socket_filter(bpfprog);
373 #ifdef HAVE_BPF_PROGRAM__SET_TYPE
374 bpf_program__set_type(bpfprog, BPF_PROG_TYPE_XDP);
377 bpf_program__set_xdp(bpfprog);
386 SCLogError(
"No section '%s' in '%s' file. Will not be able to use the file", section, path);
390 err = bpf_object__load(bpfobj);
393 SCLogError(
"Permission issue when loading eBPF object"
394 " (check libbpf error on stdout)");
397 libbpf_strerror(err, buf,
sizeof(buf));
398 SCLogError(
"Unable to load eBPF object: %s (%d)", buf, err);
406 struct bpf_maps_info *bpf_map_data =
SCCalloc(1,
sizeof(*bpf_map_data));
407 if (bpf_map_data == NULL) {
413 bpf_map__for_each(map, bpfobj) {
414 if (bpf_map_data->last == BPF_MAP_MAX_COUNT) {
415 SCLogError(
"Too many BPF maps in eBPF files");
418 if (strcmp(bpf_map__name(map),
"flow_table_v4") == 0) {
419 if (bpf_map__key_size(map) !=
sizeof(
struct flowv4_keys)) {
424 if (strcmp(bpf_map__name(map),
"flow_table_v6") == 0) {
425 if (bpf_map__key_size(map) !=
sizeof(
struct flowv6_keys)) {
430 SCLogDebug(
"Got a map '%s' with fd '%d'", bpf_map__name(map), bpf_map__fd(map));
431 bpf_map_data->array[bpf_map_data->last].fd = bpf_map__fd(map);
432 bpf_map_data->array[bpf_map_data->last].name =
SCStrdup(bpf_map__name(map));
433 snprintf(bpf_map_data->array[bpf_map_data->last].iface, IFNAMSIZ,
435 if (!bpf_map_data->array[bpf_map_data->last].name) {
437 BpfMapsInfoFree(bpf_map_data);
440 bpf_map_data->array[bpf_map_data->last].to_unlink = 0;
441 if (config->flags & EBPF_PINNED_MAPS) {
442 SCLogConfig(
"Pinning: %d to %s", bpf_map_data->array[bpf_map_data->last].fd,
443 bpf_map_data->array[bpf_map_data->last].name);
445 snprintf(buf,
sizeof(buf),
"/sys/fs/bpf/suricata-%s-%s", iface,
446 bpf_map_data->array[bpf_map_data->last].name);
447 int ret = bpf_obj_pin(bpf_map_data->array[bpf_map_data->last].fd, buf);
452 if (config->flags & EBPF_XDP_CODE) {
453 bpf_map_data->array[bpf_map_data->last].to_unlink = 0;
455 bpf_map_data->array[bpf_map_data->last].to_unlink = 1;
458 bpf_map_data->last++;
468 pfd = bpf_program__fd(bpfprog);
470 SCLogError(
"Unable to find %s section", section);
474 SCLogInfo(
"Successfully loaded eBPF file '%s' on '%s'", path, iface);
487 int EBPFSetupXDP(
const char *iface,
int fd, uint8_t
flags)
489 #ifdef HAVE_PACKET_XDP
490 unsigned int ifindex = if_nametoindex(iface);
495 #ifdef HAVE_BPF_XDP_ATTACH
496 int err = bpf_xdp_attach(ifindex, fd,
flags, NULL);
499 int err = bpf_set_link_xdp_fd(ifindex, fd,
flags);
503 libbpf_strerror(err, buf,
sizeof(buf));
504 SCLogError(
"Unable to set XDP on '%s': %s (%d)", iface, buf, err);
517 size_t skey,
FlowKey *flow_key,
struct timespec *ctime,
518 uint64_t pkts_cnt, uint64_t bytes_cnt,
519 int mapfd,
int cpus_count)
543 EBPFBypassData *eb =
SCCalloc(1,
sizeof(EBPFBypassData));
556 memcpy(mkey, key, skey);
559 eb->cpus_count = cpus_count;
567 EBPFBypassData *eb = (EBPFBypassData *) fc->
bypass_data;
574 if (eb->key[0] && eb->key[1]) {
585 memcpy(mkey, key, skey);
593 void EBPFBypassFree(
void *data)
595 EBPFBypassData *eb = (EBPFBypassData *)data;
613 EBPFBypassData *eb,
void *key,
617 uint64_t pkts_cnt = 0;
618 uint64_t bytes_cnt = 0;
621 BPF_DECLARE_PERCPU(
struct pair, values_array, eb->cpus_count);
622 memset(values_array, 0,
sizeof(values_array));
623 int res = bpf_map_lookup_elem(eb->mapfd, key, values_array);
625 SCLogDebug(
"errno: (%d) %s", errno, strerror(errno));
628 for (i = 0; i < eb->cpus_count; i++) {
630 SCLogDebug(
"%d: Adding pkts %lu bytes %lu", i,
631 BPF_PERCPU(values_array, i).packets,
632 BPF_PERCPU(values_array, i).bytes);
633 pkts_cnt += BPF_PERCPU(values_array, i).packets;
634 bytes_cnt += BPF_PERCPU(values_array, i).bytes;
658 bool EBPFBypassUpdate(
Flow *f,
void *data, time_t tsec)
660 EBPFBypassData *eb = (EBPFBypassData *)data;
668 bool activity = EBPFBypassCheckHalfFlow(f, fc, eb, eb->key[0], 0);
669 activity |= EBPFBypassCheckHalfFlow(f, fc, eb, eb->key[1], 1);
673 EBPFDeleteKey(eb->mapfd, eb->key[0]);
674 EBPFDeleteKey(eb->mapfd, eb->key[1]);
684 size_t skey,
FlowKey *flow_key,
struct timespec *ctime,
685 uint64_t pkts_cnt, uint64_t bytes_cnt,
686 int mapfd,
int cpus_count);
695 struct timespec *ctime,
696 struct ebpf_timeout_config *tcfg,
697 OpFlowForKey EBPFOpFlowForKey
701 int mapfd = EBPFGetMapFDByName(dev->
dev,
name);
705 struct flowv4_keys key = {}, next_key;
708 uint64_t hash_cnt = 0;
710 if (tcfg->cpus_count == 0) {
714 bool dead_flow =
false;
715 while (bpf_map_get_next_key(mapfd, &key, &next_key) == 0) {
716 uint64_t bytes_cnt = 0;
717 uint64_t pkts_cnt = 0;
720 EBPFDeleteKey(mapfd, &key);
725 BPF_DECLARE_PERCPU(
struct pair, values_array, tcfg->cpus_count);
726 memset(values_array, 0,
sizeof(values_array));
727 int res = bpf_map_lookup_elem(mapfd, &next_key, values_array);
729 SCLogDebug(
"no entry in v4 table for %d -> %d", key.port16[0], key.port16[1]);
730 SCLogDebug(
"errno: (%d) %s", errno, strerror(errno));
734 for (i = 0; i < tcfg->cpus_count; i++) {
736 SCLogDebug(
"%d: Adding pkts %lu bytes %lu", i,
737 BPF_PERCPU(values_array, i).packets,
738 BPF_PERCPU(values_array, i).bytes);
739 pkts_cnt += BPF_PERCPU(values_array, i).packets;
740 bytes_cnt += BPF_PERCPU(values_array, i).bytes;
745 if (tcfg->mode == AFP_MODE_XDP_BYPASS) {
746 flow_key.
sp = ntohs(next_key.port16[0]);
747 flow_key.
dp = ntohs(next_key.port16[1]);
748 flow_key.
src.addr_data32[0] = next_key.src;
749 flow_key.
dst.addr_data32[0] = next_key.dst;
751 flow_key.
sp = next_key.port16[0];
752 flow_key.
dp = next_key.port16[1];
753 flow_key.
src.addr_data32[0] = ntohl(next_key.src);
754 flow_key.
dst.addr_data32[0] = ntohl(next_key.dst);
757 flow_key.
src.addr_data32[1] = 0;
758 flow_key.
src.addr_data32[2] = 0;
759 flow_key.
src.addr_data32[3] = 0;
761 flow_key.
dst.addr_data32[1] = 0;
762 flow_key.
dst.addr_data32[2] = 0;
763 flow_key.
dst.addr_data32[3] = 0;
764 flow_key.
vlan_id[0] = next_key.vlan0;
765 flow_key.
vlan_id[1] = next_key.vlan1;
766 if (next_key.ip_proto == 1) {
767 flow_key.
proto = IPPROTO_TCP;
769 flow_key.
proto = IPPROTO_UDP;
773 dead_flow = EBPFOpFlowForKey(&flowstats, dev, &next_key,
sizeof(next_key), &flow_key,
774 ctime, pkts_cnt, bytes_cnt,
775 mapfd, tcfg->cpus_count);
787 EBPFDeleteKey(mapfd, &key);
793 SCLogInfo(
"IPv4 bypassed flow table size: %" PRIu64, hash_cnt);
806 struct timespec *ctime,
807 struct ebpf_timeout_config *tcfg,
808 OpFlowForKey EBPFOpFlowForKey
812 int mapfd = EBPFGetMapFDByName(dev->
dev,
name);
816 struct flowv6_keys key = {}, next_key;
819 uint64_t hash_cnt = 0;
821 if (tcfg->cpus_count == 0) {
826 uint64_t pkts_cnt = 0;
827 while (bpf_map_get_next_key(mapfd, &key, &next_key) == 0) {
828 uint64_t bytes_cnt = 0;
831 EBPFDeleteKey(mapfd, &key);
836 BPF_DECLARE_PERCPU(
struct pair, values_array, tcfg->cpus_count);
837 memset(values_array, 0,
sizeof(values_array));
838 int res = bpf_map_lookup_elem(mapfd, &next_key, values_array);
840 SCLogDebug(
"no entry in v4 table for %d -> %d", key.port16[0], key.port16[1]);
844 for (i = 0; i < tcfg->cpus_count; i++) {
846 SCLogDebug(
"%d: Adding pkts %lu bytes %lu", i,
847 BPF_PERCPU(values_array, i).packets,
848 BPF_PERCPU(values_array, i).bytes);
849 pkts_cnt += BPF_PERCPU(values_array, i).packets;
850 bytes_cnt += BPF_PERCPU(values_array, i).bytes;
855 if (tcfg->mode == AFP_MODE_XDP_BYPASS) {
856 flow_key.
sp = ntohs(next_key.port16[0]);
857 flow_key.
dp = ntohs(next_key.port16[1]);
859 flow_key.
src.addr_data32[0] = next_key.src[0];
860 flow_key.
src.addr_data32[1] = next_key.src[1];
861 flow_key.
src.addr_data32[2] = next_key.src[2];
862 flow_key.
src.addr_data32[3] = next_key.src[3];
864 flow_key.
dst.addr_data32[0] = next_key.dst[0];
865 flow_key.
dst.addr_data32[1] = next_key.dst[1];
866 flow_key.
dst.addr_data32[2] = next_key.dst[2];
867 flow_key.
dst.addr_data32[3] = next_key.dst[3];
869 flow_key.
sp = next_key.port16[0];
870 flow_key.
dp = next_key.port16[1];
872 flow_key.
src.addr_data32[0] = ntohl(next_key.src[0]);
873 flow_key.
src.addr_data32[1] = ntohl(next_key.src[1]);
874 flow_key.
src.addr_data32[2] = ntohl(next_key.src[2]);
875 flow_key.
src.addr_data32[3] = ntohl(next_key.src[3]);
877 flow_key.
dst.addr_data32[0] = ntohl(next_key.dst[0]);
878 flow_key.
dst.addr_data32[1] = ntohl(next_key.dst[1]);
879 flow_key.
dst.addr_data32[2] = ntohl(next_key.dst[2]);
880 flow_key.
dst.addr_data32[3] = ntohl(next_key.dst[3]);
882 flow_key.
vlan_id[0] = next_key.vlan0;
883 flow_key.
vlan_id[1] = next_key.vlan1;
884 if (next_key.ip_proto == 1) {
885 flow_key.
proto = IPPROTO_TCP;
887 flow_key.
proto = IPPROTO_UDP;
891 pkts_cnt = EBPFOpFlowForKey(&flowstats, dev, &next_key,
sizeof(next_key), &flow_key,
892 ctime, pkts_cnt, bytes_cnt,
893 mapfd, tcfg->cpus_count);
905 EBPFDeleteKey(mapfd, &key);
911 SCLogInfo(
"IPv6 bypassed flow table size: %" PRIu64, hash_cnt);
916 int EBPFCheckBypassedFlowCreate(
ThreadVars *
th_v,
struct timespec *curtime,
void *data)
919 struct ebpf_timeout_config *cfg = (
struct ebpf_timeout_config *)data;
921 EBPFForEachFlowV4Table(
th_v, ldev,
"flow_table_v4",
923 cfg, EBPFCreateFlowForKey);
924 EBPFForEachFlowV6Table(
th_v, ldev,
"flow_table_v6",
926 cfg, EBPFCreateFlowForKey);
932 void EBPFRegisterExtension(
void)
939 #ifdef HAVE_PACKET_XDP
941 static uint32_t g_redirect_iface_cpu_counter = 0;
943 static int EBPFAddCPUToMap(
const char *iface, uint32_t i)
945 int cpumap = EBPFGetMapFDByName(iface,
"cpu_map");
946 uint32_t queue_size = 4096;
953 ret = bpf_map_update_elem(cpumap, &i, &queue_size, 0);
955 SCLogError(
"Create CPU entry failed (err:%d)", ret);
958 int cpus_available = EBPFGetMapFDByName(iface,
"cpus_available");
959 if (cpus_available < 0) {
964 ret = bpf_map_update_elem(cpus_available, &g_redirect_iface_cpu_counter, &i, 0);
966 SCLogError(
"Create CPU entry failed (err:%d)", ret);
972 static void EBPFRedirectMapAddCPU(
int i,
void *data)
974 if (EBPFAddCPUToMap(data, i) < 0) {
977 g_redirect_iface_cpu_counter++;
981 void EBPFBuildCPUSet(
SCConfNode *node,
char *iface)
984 int mapfd = EBPFGetMapFDByName(iface,
"cpus_count");
986 SCLogError(
"Unable to find 'cpus_count' map");
989 g_redirect_iface_cpu_counter = 0;
991 bpf_map_update_elem(mapfd, &key0, &g_redirect_iface_cpu_counter,
996 SCLogWarning(
"Failed to parse XDP CPU redirect configuration");
999 bpf_map_update_elem(mapfd, &key0, &g_redirect_iface_cpu_counter,
1015 int EBPFSetPeerIface(
const char *iface,
const char *out_iface)
1017 int mapfd = EBPFGetMapFDByName(iface,
"tx_peer");
1022 int intmapfd = EBPFGetMapFDByName(iface,
"tx_peer_int");
1024 SCLogError(
"Unable to find 'tx_peer_int' map");
1029 unsigned int peer_index = if_nametoindex(out_iface);
1030 if (peer_index == 0) {
1034 int ret = bpf_map_update_elem(mapfd, &key0, &peer_index, BPF_ANY);
1036 SCLogError(
"Create peer entry failed (err:%d)", ret);
1039 ret = bpf_map_update_elem(intmapfd, &key0, &peer_index, BPF_ANY);
1041 SCLogError(
"Create peer entry failed (err:%d)", ret);
1052 int EBPFUpdateFlow(
Flow *f,
Packet *
p,
void *data)
1065 BypassedIfaceList *ldev = ifl;
1076 BypassedIfaceList *nifl =
SCCalloc(1,
sizeof(*nifl));