1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/* Copyright (C) 2020 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <bpf/libbpf.h>
#include <errno.h>
#include <ifaddrs.h>
#include <linux/ethtool.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "contrib/openbsd/strlcpy.h"
#include "contrib/sockaddr.h"
#include "libknot/attribute.h"
#include "libknot/errcode.h"
#include "libknot/xdp/eth.h"
_public_
int knot_eth_queues(const char *devname)
{
if (devname == NULL) {
return KNOT_EINVAL;
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
return knot_map_errno();
}
struct ethtool_channels ch = {
.cmd = ETHTOOL_GCHANNELS
};
struct ifreq ifr = {
.ifr_data = &ch
};
strlcpy(ifr.ifr_name, devname, IFNAMSIZ);
int ret = ioctl(fd, SIOCETHTOOL, &ifr);
if (ret != 0) {
if (errno == EOPNOTSUPP) {
ret = 1;
} else {
ret = knot_map_errno();
}
} else {
if (ch.combined_count == 0) {
ret = 1;
} else {
ret = ch.combined_count;
}
}
close(fd);
return ret;
}
_public_
int knot_eth_name_from_addr(const struct sockaddr_storage *addr, char *out,
size_t out_len)
{
if (addr == NULL || out == NULL) {
return KNOT_EINVAL;
}
struct ifaddrs *ifaces = NULL;
if (getifaddrs(&ifaces) != 0) {
return -errno;
}
size_t matches = 0;
char *match_name = NULL;
for (struct ifaddrs *ifa = ifaces; ifa != NULL; ifa = ifa->ifa_next) {
const struct sockaddr_storage *ifss = (struct sockaddr_storage *)ifa->ifa_addr;
if (ifss == NULL) { // Observed on interfaces without any address.
continue;
}
if ((ifss->ss_family == addr->ss_family && sockaddr_is_any(addr)) ||
sockaddr_cmp(ifss, addr, true) == 0) {
matches++;
match_name = ifa->ifa_name;
}
}
if (matches == 1) {
size_t len = strlcpy(out, match_name, out_len);
freeifaddrs(ifaces);
return (len >= out_len) ? KNOT_ESPACE : KNOT_EOK;
}
freeifaddrs(ifaces);
return matches == 0 ? KNOT_EADDRNOTAVAIL : KNOT_ELIMIT;
}
_public_
knot_xdp_mode_t knot_eth_xdp_mode(int if_index)
{
struct xdp_link_info info;
int ret = bpf_get_link_xdp_info(if_index, &info, sizeof(info), 0);
if (ret != 0) {
return KNOT_XDP_MODE_NONE;
}
switch (info.attach_mode) {
case XDP_ATTACHED_DRV:
case XDP_ATTACHED_HW:
return KNOT_XDP_MODE_FULL;
case XDP_ATTACHED_SKB:
return KNOT_XDP_MODE_EMUL;
default:
return KNOT_XDP_MODE_NONE;
}
}
|