]>
git.proxmox.com Git - qemu.git/blob - qemu-bridge-helper.c
4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Richa Marwaha <rmarwah@linux.vnet.ibm.com>
9 * Corey Bryant <coreyb@linux.vnet.ibm.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #include "config-host.h"
27 #include <sys/types.h>
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
31 #include <sys/prctl.h>
35 #include <linux/sockios.h>
37 #include "net/tap-linux.h"
39 static void usage(void)
42 "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
45 static bool has_vnet_hdr(int fd
)
47 unsigned int features
= 0;
49 if (ioctl(fd
, TUNGETFEATURES
, &features
) == -1) {
53 if (!(features
& IFF_VNET_HDR
)) {
60 static void prep_ifreq(struct ifreq
*ifr
, const char *ifname
)
62 memset(ifr
, 0, sizeof(*ifr
));
63 snprintf(ifr
->ifr_name
, IFNAMSIZ
, "%s", ifname
);
66 static int send_fd(int c
, int fd
)
68 char msgbuf
[CMSG_SPACE(sizeof(fd
))];
70 .msg_control
= msgbuf
,
71 .msg_controllen
= sizeof(msgbuf
),
75 char req
[1] = { 0x00 };
77 cmsg
= CMSG_FIRSTHDR(&msg
);
78 cmsg
->cmsg_level
= SOL_SOCKET
;
79 cmsg
->cmsg_type
= SCM_RIGHTS
;
80 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
81 msg
.msg_controllen
= cmsg
->cmsg_len
;
84 iov
.iov_len
= sizeof(req
);
88 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
90 return sendmsg(c
, &msg
, 0);
93 int main(int argc
, char **argv
)
96 int fd
, ctlfd
, unixfd
= -1;
99 const char *bridge
= NULL
;
100 char iface
[IFNAMSIZ
];
102 int ret
= EXIT_SUCCESS
;
104 /* parse arguments */
105 for (index
= 1; index
< argc
; index
++) {
106 if (strcmp(argv
[index
], "--use-vnet") == 0) {
108 } else if (strncmp(argv
[index
], "--br=", 5) == 0) {
109 bridge
= &argv
[index
][5];
110 } else if (strncmp(argv
[index
], "--fd=", 5) == 0) {
111 unixfd
= atoi(&argv
[index
][5]);
118 if (bridge
== NULL
|| unixfd
== -1) {
123 /* open a socket to use to control the network interfaces */
124 ctlfd
= socket(AF_INET
, SOCK_STREAM
, 0);
126 fprintf(stderr
, "failed to open control socket: %s\n", strerror(errno
));
131 /* open the tap device */
132 fd
= open("/dev/net/tun", O_RDWR
);
134 fprintf(stderr
, "failed to open /dev/net/tun: %s\n", strerror(errno
));
139 /* request a tap device, disable PI, and add vnet header support if
140 * requested and it's available. */
141 prep_ifreq(&ifr
, "tap%d");
142 ifr
.ifr_flags
= IFF_TAP
|IFF_NO_PI
;
143 if (use_vnet
&& has_vnet_hdr(fd
)) {
144 ifr
.ifr_flags
|= IFF_VNET_HDR
;
147 if (ioctl(fd
, TUNSETIFF
, &ifr
) == -1) {
148 fprintf(stderr
, "failed to create tun device: %s\n", strerror(errno
));
153 /* save tap device name */
154 snprintf(iface
, sizeof(iface
), "%s", ifr
.ifr_name
);
156 /* get the mtu of the bridge */
157 prep_ifreq(&ifr
, bridge
);
158 if (ioctl(ctlfd
, SIOCGIFMTU
, &ifr
) == -1) {
159 fprintf(stderr
, "failed to get mtu of bridge `%s': %s\n",
160 bridge
, strerror(errno
));
168 /* set the mtu of the interface based on the bridge */
169 prep_ifreq(&ifr
, iface
);
171 if (ioctl(ctlfd
, SIOCSIFMTU
, &ifr
) == -1) {
172 fprintf(stderr
, "failed to set mtu of device `%s' to %d: %s\n",
173 iface
, mtu
, strerror(errno
));
178 /* add the interface to the bridge */
179 prep_ifreq(&ifr
, bridge
);
180 ifr
.ifr_ifindex
= if_nametoindex(iface
);
182 if (ioctl(ctlfd
, SIOCBRADDIF
, &ifr
) == -1) {
183 fprintf(stderr
, "failed to add interface `%s' to bridge `%s': %s\n",
184 iface
, bridge
, strerror(errno
));
189 /* bring the interface up */
190 prep_ifreq(&ifr
, iface
);
191 if (ioctl(ctlfd
, SIOCGIFFLAGS
, &ifr
) == -1) {
192 fprintf(stderr
, "failed to get interface flags for `%s': %s\n",
193 iface
, strerror(errno
));
198 ifr
.ifr_flags
|= IFF_UP
;
199 if (ioctl(ctlfd
, SIOCSIFFLAGS
, &ifr
) == -1) {
200 fprintf(stderr
, "failed to bring up interface `%s': %s\n",
201 iface
, strerror(errno
));
206 /* write fd to the domain socket */
207 if (send_fd(unixfd
, fd
) == -1) {
208 fprintf(stderr
, "failed to write fd to unix socket: %s\n",