1 /* Copyright (c) 2016 PLUMgrid
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
8 #include <linux/netlink.h>
9 #include <linux/rtnetlink.h>
16 #include <sys/socket.h>
21 static int set_link_xdp_fd(int ifindex
, int fd
)
23 struct sockaddr_nl sa
;
24 int sock
, seq
= 0, len
, ret
= -1;
26 struct nlattr
*nla
, *nla_xdp
;
29 struct ifinfomsg ifinfo
;
35 memset(&sa
, 0, sizeof(sa
));
36 sa
.nl_family
= AF_NETLINK
;
38 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
40 printf("open netlink socket: %s\n", strerror(errno
));
44 if (bind(sock
, (struct sockaddr
*)&sa
, sizeof(sa
)) < 0) {
45 printf("bind to netlink: %s\n", strerror(errno
));
49 memset(&req
, 0, sizeof(req
));
50 req
.nh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
51 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
52 req
.nh
.nlmsg_type
= RTM_SETLINK
;
54 req
.nh
.nlmsg_seq
= ++seq
;
55 req
.ifinfo
.ifi_family
= AF_UNSPEC
;
56 req
.ifinfo
.ifi_index
= ifindex
;
57 nla
= (struct nlattr
*)(((char *)&req
)
58 + NLMSG_ALIGN(req
.nh
.nlmsg_len
));
59 nla
->nla_type
= NLA_F_NESTED
| 43/*IFLA_XDP*/;
61 nla_xdp
= (struct nlattr
*)((char *)nla
+ NLA_HDRLEN
);
62 nla_xdp
->nla_type
= 1/*IFLA_XDP_FD*/;
63 nla_xdp
->nla_len
= NLA_HDRLEN
+ sizeof(int);
64 memcpy((char *)nla_xdp
+ NLA_HDRLEN
, &fd
, sizeof(fd
));
65 nla
->nla_len
= NLA_HDRLEN
+ nla_xdp
->nla_len
;
67 req
.nh
.nlmsg_len
+= NLA_ALIGN(nla
->nla_len
);
69 if (send(sock
, &req
, req
.nh
.nlmsg_len
, 0) < 0) {
70 printf("send to netlink: %s\n", strerror(errno
));
74 len
= recv(sock
, buf
, sizeof(buf
), 0);
76 printf("recv from netlink: %s\n", strerror(errno
));
80 for (nh
= (struct nlmsghdr
*)buf
; NLMSG_OK(nh
, len
);
81 nh
= NLMSG_NEXT(nh
, len
)) {
82 if (nh
->nlmsg_pid
!= getpid()) {
83 printf("Wrong pid %d, expected %d\n",
84 nh
->nlmsg_pid
, getpid());
87 if (nh
->nlmsg_seq
!= seq
) {
88 printf("Wrong seq %d, expected %d\n",
92 switch (nh
->nlmsg_type
) {
94 err
= (struct nlmsgerr
*)NLMSG_DATA(nh
);
97 printf("nlmsg error %s\n", strerror(-err
->error
));
113 static void int_exit(int sig
)
115 set_link_xdp_fd(ifindex
, -1);
119 /* simple per-protocol drop counter
121 static void poll_stats(int interval
)
123 unsigned int nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
124 const unsigned int nr_keys
= 256;
125 __u64 values
[nr_cpus
], prev
[nr_keys
][nr_cpus
];
129 memset(prev
, 0, sizeof(prev
));
134 for (key
= 0; key
< nr_keys
; key
++) {
137 assert(bpf_lookup_elem(map_fd
[0], &key
, values
) == 0);
138 for (i
= 0; i
< nr_cpus
; i
++)
139 sum
+= (values
[i
] - prev
[key
][i
]);
141 printf("proto %u: %10llu pkt/s\n",
142 key
, sum
/ interval
);
143 memcpy(prev
[key
], values
, sizeof(values
));
148 int main(int ac
, char **argv
)
152 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
155 printf("usage: %s IFINDEX\n", argv
[0]);
159 ifindex
= strtoul(argv
[1], NULL
, 0);
161 if (load_bpf_file(filename
)) {
162 printf("%s", bpf_log_buf
);
167 printf("load_bpf_file: %s\n", strerror(errno
));
171 signal(SIGINT
, int_exit
);
173 if (set_link_xdp_fd(ifindex
, prog_fd
[0]) < 0) {
174 printf("link set xdp fd failed\n");