]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/tools/netmap/nm_util.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / tools / netmap / nm_util.h
1 /*
2  * Copyright (C) 2012 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  *   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 /*
27  * $FreeBSD$
28  * $Id$
29  *
30  * Some utilities to build netmap-based programs.
31  */
32
33 #ifndef _NM_UTIL_H
34 #define _NM_UTIL_H
35 #include <errno.h>
36 #include <signal.h>     /* signal */
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <inttypes.h>   /* PRI* macros */
40 #include <string.h>     /* strcmp */
41 #include <fcntl.h>      /* open */
42 #include <unistd.h>     /* close */
43 #include <ifaddrs.h>    /* getifaddrs */
44
45 #include <sys/mman.h>   /* PROT_* */
46 #include <sys/ioctl.h>  /* ioctl */
47 #include <sys/poll.h>
48 #include <sys/socket.h> /* sockaddr.. */
49 #include <arpa/inet.h>  /* ntohs */
50 #include <sys/param.h>
51 #include <sys/sysctl.h> /* sysctl */
52 #include <sys/time.h>   /* timersub */
53
54 #include <net/ethernet.h>
55 #include <net/if.h>     /* ifreq */
56
57 #include <netinet/in.h>
58 #include <netinet/ip.h>
59 #include <netinet/udp.h>
60
61 #include <net/netmap.h>
62 #include <net/netmap_user.h>
63
64 #ifndef MY_PCAP         /* use the system's pcap if available */
65
66 #ifdef NO_PCAP
67 #define PCAP_ERRBUF_SIZE        512
68 typedef void pcap_t;
69 struct pcap_pkthdr;
70 #define pcap_inject(a,b,c)      ((void)a, (void)b, (void)c, -1)
71 #define pcap_dispatch(a, b, c, d)       (void)c
72 #define pcap_open_live(a, b, c, d, e)   ((void)e, NULL)
73 #else /* !NO_PCAP */
74 #include <pcap/pcap.h> // XXX do we need it ?
75 #endif /* !NO_PCAP */
76
77 #endif // XXX hack
78
79 #include <pthread.h>    /* pthread_* */
80
81 #ifdef linux
82 #define ifr_flagshigh  ifr_flags
83 #define ifr_curcap     ifr_flags
84 #define ifr_reqcap     ifr_flags
85 #define IFF_PPROMISC   IFF_PROMISC
86 #include <linux/ethtool.h>
87 #include <linux/sockios.h>
88
89 #define CLOCK_REALTIME_PRECISE CLOCK_REALTIME
90 #include <netinet/ether.h>      /* ether_aton */
91 #include <linux/if_packet.h>    /* sockaddr_ll */
92 #endif  /* linux */
93
94 #ifdef __FreeBSD__
95 #include <sys/endian.h> /* le64toh */
96 #include <machine/param.h>
97
98 #include <pthread_np.h> /* pthread w/ affinity */
99 #include <sys/cpuset.h> /* cpu_set */
100 #include <net/if_dl.h>  /* LLADDR */
101 #endif  /* __FreeBSD__ */
102
103 #ifdef __APPLE__
104 #define ifr_flagshigh  ifr_flags        // XXX
105 #define IFF_PPROMISC   IFF_PROMISC
106 #include <net/if_dl.h>  /* LLADDR */
107 #define clock_gettime(a,b)      \
108         do {struct timespec t0 = {0,0}; *(b) = t0; } while (0)
109 #endif  /* __APPLE__ */
110
111 static inline int min(int a, int b) { return a < b ? a : b; }
112 extern int time_second;
113
114 /* debug support */
115 #define ND(format, ...) do {} while(0)
116 #define D(format, ...)                                  \
117         fprintf(stderr, "%s [%d] " format "\n",         \
118         __FUNCTION__, __LINE__, ##__VA_ARGS__)
119
120 #define RD(lps, format, ...)                            \
121         do {                                            \
122                 static int t0, cnt;                     \
123                 if (t0 != time_second) {                \
124                         t0 = time_second;               \
125                         cnt = 0;                        \
126                 }                                       \
127                 if (cnt++ < lps)                        \
128                         D(format, ##__VA_ARGS__);       \
129         } while (0)
130
131
132
133 // XXX does it work on 32-bit machines ?
134 static inline void prefetch (const void *x)
135 {
136         __asm volatile("prefetcht0 %0" :: "m" (*(const unsigned long *)x));
137 }
138
139 // XXX only for multiples of 64 bytes, non overlapped.
140 static inline void
141 pkt_copy(const void *_src, void *_dst, int l)
142 {
143         const uint64_t *src = _src;
144         uint64_t *dst = _dst;
145 #define likely(x)       __builtin_expect(!!(x), 1)
146 #define unlikely(x)       __builtin_expect(!!(x), 0)
147         if (unlikely(l >= 1024)) {
148                 bcopy(src, dst, l);
149                 return;
150         }
151         for (; l > 0; l-=64) {
152                 *dst++ = *src++;
153                 *dst++ = *src++;
154                 *dst++ = *src++;
155                 *dst++ = *src++;
156                 *dst++ = *src++;
157                 *dst++ = *src++;
158                 *dst++ = *src++;
159                 *dst++ = *src++;
160         }
161 }
162
163 /*
164  * info on a ring we handle
165  */
166 struct my_ring {
167         const char *ifname;
168         int fd;
169         char *mem;                      /* userspace mmap address */
170         u_int memsize;
171         u_int queueid;
172         u_int begin, end;               /* first..last+1 rings to check */
173         struct netmap_if *nifp;
174         struct netmap_ring *tx, *rx;    /* shortcuts */
175
176         uint32_t if_flags;
177         uint32_t if_reqcap;
178         uint32_t if_curcap;
179 };
180 int netmap_open(struct my_ring *me, int ringid, int promisc);
181 int netmap_close(struct my_ring *me);
182 int nm_do_ioctl(struct my_ring *me, u_long what, int subcmd);
183 #endif /* _NM_UTIL_H */