]> The Tcpdump Group git mirrors - libpcap/blob - pcap-netmap.c
Add netmap support.
[libpcap] / pcap-netmap.c
1 /*
2 * Copyright (C) 2014 Luigi Rizzo. 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <poll.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #define NETMAP_WITH_LIBS
41 #include <net/netmap_user.h>
42
43 #include "pcap-int.h"
44 #include "pcap-netmap.h"
45
46 #if defined (linux)
47 /* On FreeBSD we use IFF_PPROMISC which is in ifr_flagshigh.
48 * remap to IFF_PROMISC on linux
49 */
50 #define IFF_PPROMISC IFF_PROMISC
51 #endif /* linux */
52
53 struct pcap_netmap {
54 struct nm_desc *d; /* pointer returned by nm_open() */
55 pcap_handler cb; /* callback and argument */
56 u_char *cb_arg;
57 int must_clear_promisc; /* flag */
58 uint64_t rx_pkts; /* # of pkts received before the filter */
59 };
60
61
62 static int
63 pcap_netmap_stats(pcap_t *p, struct pcap_stat *ps)
64 {
65 struct pcap_netmap *pn = p->priv;
66
67 ps->ps_recv = pn->rx_pkts;
68 ps->ps_drop = 0;
69 ps->ps_ifdrop = 0;
70 return 0;
71 }
72
73
74 static void
75 pcap_netmap_filter(u_char *arg, struct pcap_pkthdr *h, const u_char *buf)
76 {
77 pcap_t *p = (pcap_t *)arg;
78 struct pcap_netmap *pn = p->priv;
79 const struct bpf_insn *pc = p->fcode.bf_insns;
80
81 ++pn->rx_pkts;
82 if (pc == NULL || bpf_filter(pc, buf, h->len, h->caplen))
83 pn->cb(pn->cb_arg, h, buf);
84 }
85
86
87 static int
88 pcap_netmap_dispatch(pcap_t *p, int cnt, pcap_handler cb, u_char *user)
89 {
90 int ret;
91 struct pcap_netmap *pn = p->priv;
92 struct nm_desc *d = pn->d;
93 struct pollfd pfd = { .fd = p->fd, .events = POLLIN, .revents = 0 };
94
95 pn->cb = cb;
96 pn->cb_arg = user;
97
98 for (;;) {
99 if (p->break_loop) {
100 p->break_loop = 0;
101 return PCAP_ERROR_BREAK;
102 }
103 /* nm_dispatch won't run forever */
104
105 ret = nm_dispatch((void *)d, cnt, (void *)pcap_netmap_filter, (void *)p);
106 if (ret != 0)
107 break;
108 errno = 0;
109 ret = poll(&pfd, 1, p->opt.timeout);
110 }
111 return ret;
112 }
113
114
115 /* XXX need to check the NIOCTXSYNC/poll */
116 static int
117 pcap_netmap_inject(pcap_t *p, const void *buf, size_t size)
118 {
119 struct pcap_netmap *pn = p->priv;
120 struct nm_desc *d = pn->d;
121
122 return nm_inject(d, buf, size);
123 }
124
125
126 static int
127 pcap_netmap_ioctl(pcap_t *p, u_long what, uint32_t *if_flags)
128 {
129 struct pcap_netmap *pn = p->priv;
130 struct nm_desc *d = pn->d;
131 struct ifreq ifr;
132 int error, fd = d->fd;
133
134 #ifdef linux
135 fd = socket(AF_INET, SOCK_DGRAM, 0);
136 if (fd < 0) {
137 fprintf(stderr, "Error: cannot get device control socket.\n");
138 return -1;
139 }
140 #endif /* linux */
141 bzero(&ifr, sizeof(ifr));
142 strncpy(ifr.ifr_name, d->req.nr_name, sizeof(ifr.ifr_name));
143 switch (what) {
144 case SIOCSIFFLAGS:
145 ifr.ifr_flags = *if_flags;
146 #ifdef __FreeBSD__
147 ifr.ifr_flagshigh = *if_flags >> 16;
148 #endif /* __FreeBSD__ */
149 break;
150 }
151 error = ioctl(fd, what, &ifr);
152 if (!error) {
153 switch (what) {
154 case SIOCGIFFLAGS:
155 *if_flags = ifr.ifr_flags;
156 #ifdef __FreeBSD__
157 *if_flags |= (ifr.ifr_flagshigh << 16);
158 #endif /* __FreeBSD__ */
159 }
160 }
161 #ifdef linux
162 close(fd);
163 #endif /* linux */
164 return error ? -1 : 0;
165 }
166
167
168 static void
169 pcap_netmap_close(pcap_t *p)
170 {
171 struct pcap_netmap *pn = p->priv;
172 struct nm_desc *d = pn->d;
173 uint32_t if_flags = 0;
174
175 if (pn->must_clear_promisc) {
176 pcap_netmap_ioctl(p, SIOCGIFFLAGS, &if_flags); /* fetch flags */
177 if (if_flags & IFF_PPROMISC) {
178 if_flags &= ~IFF_PPROMISC;
179 pcap_netmap_ioctl(p, SIOCSIFFLAGS, &if_flags);
180 }
181 }
182 nm_close(d);
183 pcap_cleanup_live_common(p);
184 }
185
186
187 static int
188 pcap_netmap_activate(pcap_t *p)
189 {
190 struct pcap_netmap *pn = p->priv;
191 struct nm_desc *d = nm_open(p->opt.source, NULL, 0, NULL);
192 uint32_t if_flags = 0;
193
194 if (d == NULL) {
195 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
196 "netmap open: cannot access %s: %s\n",
197 p->opt.source, pcap_strerror(errno));
198 pcap_cleanup_live_common(p);
199 return (PCAP_ERROR);
200 }
201 if (0)
202 fprintf(stderr, "%s device %s priv %p fd %d ports %d..%d\n",
203 __FUNCTION__, p->opt.source, d, d->fd,
204 d->first_rx_ring, d->last_rx_ring);
205 pn->d = d;
206 p->fd = d->fd;
207 if (p->opt.promisc && !(d->req.nr_ringid & NETMAP_SW_RING)) {
208 pcap_netmap_ioctl(p, SIOCGIFFLAGS, &if_flags); /* fetch flags */
209 if (!(if_flags & IFF_PPROMISC)) {
210 pn->must_clear_promisc = 1;
211 if_flags |= IFF_PPROMISC;
212 pcap_netmap_ioctl(p, SIOCSIFFLAGS, &if_flags);
213 }
214 }
215 p->linktype = DLT_EN10MB;
216 p->selectable_fd = p->fd;
217 p->read_op = pcap_netmap_dispatch;
218 p->inject_op = pcap_netmap_inject,
219 p->setfilter_op = install_bpf_program;
220 p->setdirection_op = NULL;
221 p->set_datalink_op = NULL;
222 p->getnonblock_op = pcap_getnonblock_fd;
223 p->setnonblock_op = pcap_setnonblock_fd;
224 p->stats_op = pcap_netmap_stats;
225 p->cleanup_op = pcap_netmap_close;
226
227 return (0);
228 }
229
230
231 pcap_t *
232 pcap_netmap_create(const char *device, char *ebuf, int *is_ours)
233 {
234 pcap_t *p;
235
236 *is_ours = (!strncmp(device, "netmap:", 7) || !strncmp(device, "vale", 4));
237 if (! *is_ours)
238 return NULL;
239 p = pcap_create_common(ebuf, sizeof (struct pcap_netmap));
240 if (p == NULL)
241 return (NULL);
242 p->activate_op = pcap_netmap_activate;
243 return (p);
244 }
245
246 /*
247 * XXX - is there a way to enumerate the netmap devices?
248 */
249 int
250 pcap_netmap_findalldevs(pcap_if_list_t *devlistp, char *err_str)
251 {
252 return 0;
253 }
254