2 * Copyright (C) 2012 Luigi Rizzo. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
30 * Some utilities to build netmap-based programs.
36 #include <signal.h> /* signal */
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 */
45 #include <sys/mman.h> /* PROT_* */
46 #include <sys/ioctl.h> /* ioctl */
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 */
54 #include <net/ethernet.h>
55 #include <net/if.h> /* ifreq */
57 #include <netinet/in.h>
58 #include <netinet/ip.h>
59 #include <netinet/udp.h>
61 #include <net/netmap.h>
62 #include <net/netmap_user.h>
64 #ifndef MY_PCAP /* use the system's pcap if available */
67 #define PCAP_ERRBUF_SIZE 512
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)
74 #include <pcap/pcap.h> // XXX do we need it ?
79 #include <pthread.h> /* pthread_* */
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>
89 #define CLOCK_REALTIME_PRECISE CLOCK_REALTIME
90 #include <netinet/ether.h> /* ether_aton */
91 #include <linux/if_packet.h> /* sockaddr_ll */
95 #include <sys/endian.h> /* le64toh */
96 #include <machine/param.h>
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__ */
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__ */
111 static inline int min(int a, int b) { return a < b ? a : b; }
112 extern int time_second;
115 #define ND(format, ...) do {} while(0)
116 #define D(format, ...) \
117 fprintf(stderr, "%s [%d] " format "\n", \
118 __FUNCTION__, __LINE__, ##__VA_ARGS__)
120 #define RD(lps, format, ...) \
122 static int t0, cnt; \
123 if (t0 != time_second) { \
128 D(format, ##__VA_ARGS__); \
133 // XXX does it work on 32-bit machines ?
134 static inline void prefetch (const void *x)
136 __asm volatile("prefetcht0 %0" :: "m" (*(const unsigned long *)x));
139 // XXX only for multiples of 64 bytes, non overlapped.
141 pkt_copy(const void *_src, void *_dst, int l)
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)) {
151 for (; l > 0; l-=64) {
164 * info on a ring we handle
169 char *mem; /* userspace mmap address */
172 u_int begin, end; /* first..last+1 rings to check */
173 struct netmap_if *nifp;
174 struct netmap_ring *tx, *rx; /* shortcuts */
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 */