1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2018 Intel Corporation. */
10 #include <sys/socket.h>
18 static const char *opt_if = "";
20 static struct option long_options[] = {
21 {"interface", required_argument, 0, 'i'},
25 static void usage(const char *prog)
28 " Usage: %s [OPTIONS]\n"
30 " -i, --interface=n Run on interface n\n"
32 fprintf(stderr, "%s\n", str);
37 static void parse_command_line(int argc, char **argv)
44 c = getopt_long(argc, argv, "i:",
45 long_options, &option_index);
54 usage(basename(argv[0]));
59 static int send_xsks_map_fd(int sock, int fd)
61 char cmsgbuf[CMSG_SPACE(sizeof(int))];
67 fprintf(stderr, "Incorrect fd = %d\n", fd);
70 iov.iov_base = &value;
71 iov.iov_len = sizeof(int);
78 msg.msg_control = cmsgbuf;
79 msg.msg_controllen = CMSG_LEN(sizeof(int));
81 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
83 cmsg->cmsg_level = SOL_SOCKET;
84 cmsg->cmsg_type = SCM_RIGHTS;
85 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
87 *(int *)CMSG_DATA(cmsg) = fd;
88 int ret = sendmsg(sock, &msg, 0);
91 fprintf(stderr, "Sendmsg failed with %s", strerror(errno));
99 main(int argc, char **argv)
101 struct sockaddr_un server;
111 parse_command_line(argc, argv);
113 ifindex = if_nametoindex(opt_if);
115 fprintf(stderr, "Unable to get ifindex for Interface %s. Reason:%s",
116 opt_if, strerror(errno));
120 sock = socket(AF_UNIX, SOCK_STREAM, 0);
122 fprintf(stderr, "Opening socket stream failed: %s", strerror(errno));
126 server.sun_family = AF_UNIX;
127 strcpy(server.sun_path, SOCKET_NAME);
129 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
131 if (bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un))) {
132 fprintf(stderr, "Binding to socket stream failed: %s", strerror(errno));
136 listen(sock, MAX_NUM_OF_CLIENTS);
138 err = xsk_setup_xdp_prog(ifindex, &xsks_map_fd);
140 fprintf(stderr, "Setup of xdp program failed\n");
145 msgsock = accept(sock, 0, 0);
147 fprintf(stderr, "Error accepting connection: %s", strerror(errno));
151 err = send_xsks_map_fd(msgsock, xsks_map_fd);
153 fprintf(stderr, "Error %d sending xsks_map_fd\n", err);
157 rval = read(msgsock, &cmd, sizeof(int));
159 fprintf(stderr, "Error reading stream message");
161 if (cmd != CLOSE_CONN)
162 fprintf(stderr, "Recv unknown cmd = %d\n", cmd);
172 /* Unset fd for given ifindex */
173 err = bpf_set_link_xdp_fd(ifindex, -1, 0);
175 fprintf(stderr, "Error when unsetting bpf prog_fd for ifindex(%d)\n", ifindex);