Annotation of libaitio/src/bpf.c, revision 1.1.2.11
1.1.2.1 misho 1: #include "global.h"
2:
3:
1.1.2.2 misho 4: /*
5: * io_get1stiface() - Get first interface of host
6: *
7: * @szIface = interface string buffer
8: * @iflen = size of interface buffer
9: * return: -1 error or 0 ok
10: */
11: int
12: io_get1stiface(char *szIface, int iflen)
13: {
14: struct ifaddrs *ifa;
15:
16: if (!szIface || !iflen)
17: return -1;
18:
19: getifaddrs(&ifa);
20: strlcpy(szIface, ifa->ifa_name, iflen);
21: freeifaddrs(ifa);
22: return 0;
23: }
24:
25: /*
26: * io_etherClose() - Close BPF interface
27: *
28: * @eth = bpf handle
1.1.2.3 misho 29: * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
1.1.2.2 misho 30: * return: none
31: */
32: void
1.1.2.3 misho 33: io_etherClose(int eth, void **zcbuf)
1.1.2.2 misho 34: {
1.1.2.3 misho 35: #ifdef __FreeBSD__
36: struct bpf_zbuf *zbuf = NULL;
37: #endif
38:
1.1.2.2 misho 39: if (eth > STDERR_FILENO)
40: close(eth);
1.1.2.3 misho 41:
42: if (zcbuf && *zcbuf) {
43: #ifdef __FreeBSD__
44: zbuf = *zcbuf;
45: munmap(zbuf->bz_bufb, zbuf->bz_buflen);
46: munmap(zbuf->bz_bufa, zbuf->bz_buflen);
47: e_free(*zcbuf);
48: *zcbuf = NULL;
49: #endif
50: }
51: }
52:
53: #ifdef __FreeBSD__
54: static inline struct bpf_zbuf *
55: allocZCbuf(u_int len)
56: {
57: struct bpf_zbuf *zbuf = NULL;
58:
59: zbuf = e_malloc(sizeof(struct bpf_zbuf));
60: if (!zbuf) {
61: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
62: return NULL;
63: } else
64: memset(zbuf, 0, sizeof(struct bpf_zbuf));
65:
66: zbuf->bz_buflen = roundup(len, getpagesize());
67: zbuf->bz_bufa = mmap(NULL, zbuf->bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
68: if (zbuf->bz_bufa == MAP_FAILED) {
69: LOGERR;
70: e_free(zbuf);
71: return NULL;
72: } else
73: memset(zbuf->bz_bufa, 0, zbuf->bz_buflen);
74: zbuf->bz_bufb = mmap(NULL, zbuf->bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
75: if (zbuf->bz_bufb == MAP_FAILED) {
76: LOGERR;
77: munmap(zbuf->bz_bufa, zbuf->bz_buflen);
78: e_free(zbuf);
79: return NULL;
80: } else
81: memset(zbuf->bz_bufb, 0, zbuf->bz_buflen);
82:
83: return zbuf;
1.1.2.2 misho 84: }
1.1.2.3 misho 85: #endif
1.1.2.2 misho 86:
87: /*
88: * io_etherOpen() - Open BPF interface to device
89: *
90: * @csIface = interface name
91: * @flags = open flags
1.1.2.3 misho 92: * @whdr = with complete headers
93: * @buflen = buffer length
94: * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
1.1.2.2 misho 95: * return: -1 error or >-1 bpf handle
96: */
97: int
1.1.2.4 misho 98: io_etherOpen(const char *csIface, int flags, int whdr, u_int *buflen, void **zcbuf)
1.1.2.2 misho 99: {
1.1.2.11! misho 100: int eth = -1;
1.1.2.2 misho 101: register int i;
102: char szStr[STRSIZ];
103: struct ifreq ifr;
1.1.2.11! misho 104: u_int n = 1;
! 105:
! 106: #ifndef __FreeBSD__
! 107: if (zcbuf) {
! 108: io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
! 109: return -1;
! 110: }
! 111: #endif
1.1.2.2 misho 112:
113: for (i = 0; i < BPF_DEV_MAX; i++) {
114: memset(szStr, 0, sizeof szStr);
115: snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
116: eth = open(szStr, flags);
117: if (eth > STDERR_FILENO)
118: break;
119: }
120: if (eth < 3) {
121: LOGERR;
122: return -1;
123: }
124:
125: if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) {
126: LOGERR;
127: close(eth);
128: return -1;
129: }
1.1.2.3 misho 130: if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) {
1.1.2.2 misho 131: LOGERR;
132: close(eth);
133: return -1;
134: }
1.1.2.11! misho 135:
! 136: if (!zcbuf) {
! 137: if (ioctl(eth, BIOCGBLEN, &n) == -1) {
! 138: LOGERR;
! 139: close(eth);
! 140: return -1;
! 141: } else
! 142: n = (buflen && *buflen) ? MIN(n, *buflen) : n;
! 143: if (ioctl(eth, BIOCSBLEN, &n) == -1) {
! 144: LOGERR;
! 145: close(eth);
! 146: return -1;
! 147: }
! 148: if (buflen && *buflen)
! 149: *buflen = n;
! 150: } else {
1.1.2.3 misho 151: #ifdef __FreeBSD__
1.1.2.11! misho 152: n = BPF_BUFMODE_ZBUF;
! 153: if (ioctl(eth, BIOCSETBUFMODE, &n) == -1) {
! 154: LOGERR;
! 155: close(eth);
! 156: return -1;
! 157: }
! 158: if (ioctl(eth, BIOCGETZMAX, &n) == -1) {
! 159: LOGERR;
! 160: close(eth);
! 161: return -1;
! 162: } else
! 163: n = (buflen && *buflen) ? MIN(n, *buflen) : n;
! 164: if (!(*zcbuf = allocZCbuf(n))) {
! 165: close(eth);
! 166: return -1;
! 167: }
! 168: if (ioctl(eth, BIOCSETZBUF, (struct bpf_zbuf*) *zcbuf) == -1) {
! 169: LOGERR;
! 170: io_etherClose(eth, zcbuf);
! 171: return -1;
1.1.2.3 misho 172: }
1.1.2.11! misho 173: if (buflen && *buflen)
! 174: *buflen = n;
! 175: #endif
1.1.2.3 misho 176: }
1.1.2.11! misho 177:
1.1.2.2 misho 178: if (csIface)
179: strlcpy(szStr, csIface, sizeof szStr);
180: else if (io_get1stiface(szStr, sizeof szStr) == -1) {
1.1.2.3 misho 181: io_etherClose(eth, zcbuf);
1.1.2.2 misho 182: return -1;
183: }
1.1.2.10 misho 184: memset(&ifr, 0, sizeof ifr);
1.1.2.2 misho 185: strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name);
186: if (ioctl(eth, BIOCSETIF, &ifr) == -1) {
187: LOGERR;
1.1.2.3 misho 188: io_etherClose(eth, zcbuf);
1.1.2.2 misho 189: return -1;
190: }
191:
192: return eth;
193: }
1.1.2.6 misho 194:
195: /*
196: * io_etherSend() - Send packet to bpf
197: *
198: * @eth = bpf handle
199: * @buf = buffer
200: * @buflen = buffer length
201: * return: -1 error or !=-1 written bytes
202: */
203: ssize_t
204: io_etherSend(int eth, const void *buf, size_t buflen)
205: {
206: ssize_t wlen = 0;
207:
208: if (!buf || !buflen) {
209: io_SetErr(EINVAL, "invalid arguments");
210: return -1;
211: }
212:
213: wlen = write(eth, buf, buflen);
214: if (wlen == -1)
215: LOGERR;
216: return wlen;
217: }
218:
219: #ifdef __FreeBSD__
1.1.2.8 misho 220: static inline void
221: ackZCbuf(struct bpf_zbuf_header * __restrict bzh)
222: {
223: atomic_store_rel_int(&bzh->bzh_user_gen, bzh->bzh_kernel_gen);
224: }
225:
226: static inline int
227: chkZCbuf(struct bpf_zbuf_header * __restrict bzh)
228: {
229: /* return true if userspace owns buffer, and false otherwise. */
230: return (bzh->bzh_user_gen != atomic_load_acq_int(&bzh->bzh_kernel_gen));
231: }
232:
1.1.2.6 misho 233: static inline ssize_t
1.1.2.7 misho 234: nextZCbuf(void ** __restrict zcache, struct bpf_zbuf * __restrict zbuf,
235: const void * __restrict buf)
1.1.2.6 misho 236: {
237: ssize_t rlen = -1;
238: struct bpf_zbuf_header *bzh;
239:
240: if (!*zcache || *zcache == zbuf->bz_bufb) {
241: bzh = (struct bpf_zbuf_header *) zbuf->bz_bufa;
1.1.2.8 misho 242: if (chkZCbuf(bzh)) {
1.1.2.6 misho 243: rlen = atomic_load_acq_int(&bzh->bzh_kernel_len);
244: *zcache = zbuf->bz_bufa;
1.1.2.7 misho 245: if (buf)
246: buf = ((caddr_t) *zcache) + sizeof(struct bpf_zbuf_header);
1.1.2.8 misho 247: ackZCbuf(bzh);
1.1.2.9 misho 248: } else
249: io_SetErr(EAGAIN, "kernel owns the buffer");
1.1.2.6 misho 250: } else if (*zcache == zbuf->bz_bufa) {
251: bzh = (struct bpf_zbuf_header *) zbuf->bz_bufb;
1.1.2.8 misho 252: if (chkZCbuf(bzh)) {
1.1.2.6 misho 253: rlen = atomic_load_acq_int(&bzh->bzh_kernel_len);
254: *zcache = zbuf->bz_bufb;
1.1.2.7 misho 255: if (buf)
256: buf = ((caddr_t) *zcache) + sizeof(struct bpf_zbuf_header);
1.1.2.8 misho 257: ackZCbuf(bzh);
1.1.2.9 misho 258: } else
259: io_SetErr(EAGAIN, "kernel owns the buffer");
1.1.2.6 misho 260: }
261:
262: return rlen;
263: }
264: #endif
265:
266: /*
267: * io_etherRecv() - Receive packet from bpf
268: *
269: * @eth = bpf handle
270: * @buf = buffer
271: * @buflen = buffer length
272: * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
273: * return: -1 error or !=-1 readed bytes
274: */
275: ssize_t
276: io_etherRecv(int eth, void * __restrict buf, size_t buflen, void * __restrict zcbuf)
277: {
278: ssize_t rlen = 0;
279: void **zcache = NULL;
280:
281: if (!buf || !buflen) {
282: io_SetErr(EINVAL, "invalid arguments");
283: return -1;
284: }
285:
286: if (!zcbuf) {
287: rlen = read(eth, buf, buflen);
288: if (rlen == -1)
289: LOGERR;
290: } else {
291: #ifdef __FreeBSD__
1.1.2.7 misho 292: rlen = nextZCbuf(zcache, (struct bpf_zbuf*) zcbuf, buf);
1.1.2.6 misho 293: #else
294: io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
295: #endif
296: }
297:
298: return rlen;
299: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>