]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/netmap/vale-ctl.c
Import libc++ 3.4 release. This contains a lot of bugfixes, and some
[FreeBSD/FreeBSD.git] / tools / tools / netmap / vale-ctl.c
1 /*
2  * Copyright (C) 2013-2014 Michio Honda. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *   1. Redistributions of source code must retain the above copyright
8  *      notice, this list of conditions and the following disclaimer.
9  *   2. Redistributions in binary form must reproduce the above copyright
10  *      notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 /* $FreeBSD$ */
27
28 #include <errno.h>
29 #include <stdio.h>
30 #include <inttypes.h>   /* PRI* macros */
31 #include <string.h>     /* strcmp */
32 #include <fcntl.h>      /* open */
33 #include <unistd.h>     /* close */
34 #include <sys/ioctl.h>  /* ioctl */
35 #include <sys/param.h>
36 #include <net/if.h>     /* ifreq */
37 #include <net/netmap.h>
38 #include <net/netmap_user.h>
39 #include <libgen.h>     /* basename */
40
41 /* debug support */
42 #define ND(format, ...) do {} while(0)
43 #define D(format, ...)                                  \
44         fprintf(stderr, "%s [%d] " format "\n",         \
45         __FUNCTION__, __LINE__, ##__VA_ARGS__)
46
47 static int
48 bdg_ctl(const char *name, int nr_cmd, int nr_arg)
49 {
50         struct nmreq nmr;
51         int error = 0;
52         int fd = open("/dev/netmap", O_RDWR);
53
54         if (fd == -1) {
55                 D("Unable to open /dev/netmap");
56                 return -1;
57         }
58
59         bzero(&nmr, sizeof(nmr));
60         nmr.nr_version = NETMAP_API;
61         if (name != NULL) /* might be NULL */
62                 strncpy(nmr.nr_name, name, sizeof(nmr.nr_name));
63         nmr.nr_cmd = nr_cmd;
64
65         switch (nr_cmd) {
66         case NETMAP_BDG_ATTACH:
67         case NETMAP_BDG_DETACH:
68                 if (nr_arg && nr_arg != NETMAP_BDG_HOST)
69                         nr_arg = 0;
70                 nmr.nr_arg1 = nr_arg;
71                 error = ioctl(fd, NIOCREGIF, &nmr);
72                 if (error == -1) {
73                         ND("Unable to %s %s to the bridge", nr_cmd ==
74                             NETMAP_BDG_DETACH?"detach":"attach", name);
75                         perror(name);
76                 } else
77                         ND("Success to %s %s to the bridge", nr_cmd ==
78                             NETMAP_BDG_DETACH?"detach":"attach", name);
79                 break;
80
81         case NETMAP_BDG_LIST:
82                 if (strlen(nmr.nr_name)) { /* name to bridge/port info */
83                         error = ioctl(fd, NIOCGINFO, &nmr);
84                         if (error) {
85                                 ND("Unable to obtain info for %s", name);
86                                 perror(name);
87                         } else
88                                 D("%s at bridge:%d port:%d", name, nmr.nr_arg1,
89                                     nmr.nr_arg2);
90                         break;
91                 }
92
93                 /* scan all the bridges and ports */
94                 nmr.nr_arg1 = nmr.nr_arg2 = 0;
95                 for (; !ioctl(fd, NIOCGINFO, &nmr); nmr.nr_arg2++) {
96                         D("bridge:%d port:%d %s", nmr.nr_arg1, nmr.nr_arg2,
97                             nmr.nr_name);
98                         nmr.nr_name[0] = '\0';
99                 }
100
101                 break;
102
103         default: /* GINFO */
104                 nmr.nr_cmd = nmr.nr_arg1 = nmr.nr_arg2 = 0;
105                 error = ioctl(fd, NIOCGINFO, &nmr);
106                 if (error) {
107                         ND("Unable to get if info for %s", name);
108                         perror(name);
109                 } else
110                         D("%s: %d queues.", name, nmr.nr_rx_rings);
111                 break;
112         }
113         close(fd);
114         return error;
115 }
116
117 int
118 main(int argc, char *argv[])
119 {
120         int ch, nr_cmd = 0, nr_arg = 0;
121         const char *command = basename(argv[0]);
122         char *name = NULL;
123
124         if (argc > 3) {
125 usage:
126                 fprintf(stderr,
127                         "Usage:\n"
128                         "%s arguments\n"
129                         "\t-g interface interface name to get info\n"
130                         "\t-d interface interface name to be detached\n"
131                         "\t-a interface interface name to be attached\n"
132                         "\t-h interface interface name to be attached with the host stack\n"
133                         "\t-l list all or specified bridge's interfaces (default)\n"
134                         "", command);
135                 return 0;
136         }
137
138         while ((ch = getopt(argc, argv, "d:a:h:g:l")) != -1) {
139                 name = optarg; /* default */
140                 switch (ch) {
141                 default:
142                         fprintf(stderr, "bad option %c %s", ch, optarg);
143                         goto usage;
144                 case 'd':
145                         nr_cmd = NETMAP_BDG_DETACH;
146                         break;
147                 case 'a':
148                         nr_cmd = NETMAP_BDG_ATTACH;
149                         break;
150                 case 'h':
151                         nr_cmd = NETMAP_BDG_ATTACH;
152                         nr_arg = NETMAP_BDG_HOST;
153                         break;
154                 case 'g':
155                         nr_cmd = 0;
156                         break;
157                 case 'l':
158                         nr_cmd = NETMAP_BDG_LIST;
159                         if (optind < argc && argv[optind][0] == '-')
160                                 name = NULL;
161                         break;
162                 }
163                 if (optind != argc) {
164                         // fprintf(stderr, "optind %d argc %d\n", optind, argc);
165                         goto usage;
166                 }
167         }
168         if (argc == 1)
169                 nr_cmd = NETMAP_BDG_LIST;
170         return bdg_ctl(name, nr_cmd, nr_arg) ? 1 : 0;
171 }