Annotation of libaitio/example/bpf.c, revision 1.3

1.2       misho       1: #include <stdio.h>
                      2: #include <stdlib.h>
                      3: #include <fcntl.h>
                      4: #include <unistd.h>
                      5: #include <string.h>
                      6: #include <errno.h>
                      7: #include <poll.h>
                      8: #include <getopt.h>
                      9: #include <pthread.h>
                     10: #include <assert.h>
                     11: #include <sys/types.h>
                     12: #include <sys/param.h>
                     13: #include <sys/limits.h>
                     14: #include <sys/socket.h>
                     15: #include <sys/ioctl.h>
                     16: #include <sys/mman.h>
                     17: #include <net/if.h>
                     18: #include <net/bpf.h>
                     19: #include <machine/atomic.h>
                     20: 
                     21: #ifdef __FreeBSD__
                     22: #include <net/ethernet.h>
                     23: #endif
                     24: #ifdef __OpenBSD__
                     25: #include <net/ethertypes.h>
                     26: #include <netinet/in_systm.h>
                     27: #endif
                     28: #include <net/if_arp.h>
                     29: #include <netinet/in.h>
                     30: #include <netinet/ip.h>
                     31: #include <netinet/ip6.h>
                     32: #include <netinet/udp.h>
                     33: #include <netinet/tcp.h>
                     34: #include <netinet/ip_icmp.h>
                     35: #include <arpa/inet.h>
                     36: #ifdef __OpenBSD__
                     37: #include <netinet/if_ether.h>
                     38: #endif
                     39: 
                     40: #include <aitio.h>
                     41: 
                     42: #ifndef roundup
                     43: #define roundup(x, y)   ((((x) + ((y) - 1)) / (y)) * (y))  /* to any y */
                     44: #endif
                     45: 
                     46: #define time_spec_sub(vvp, uvp)                                           \
                     47:         do {                                                            \
                     48:                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
                     49:                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
                     50:                 if ((vvp)->tv_nsec < 0) {                               \
                     51:                         (vvp)->tv_sec--;                                \
                     52:                         (vvp)->tv_nsec += 1000000000;                   \
                     53:                 }                                                       \
                     54:         } while (0)
                     55: 
                     56: 
                     57: int Verbose, flg;
                     58: 
                     59: 
                     60: static void *
                     61: ShowPkt(void *buffer)
                     62: {
                     63:        char Proto = 0, szStr[BUFSIZ], szLine[BUFSIZ], szWrk[BUFSIZ], szShow[USHRT_MAX] = { 0 };
                     64: #ifdef __FreeBSD__
                     65:        struct bpf_zbuf_header *bzh = buffer;
                     66:        struct icmphdr *icmp;
                     67: #endif
                     68: #ifdef __OpenBSD__
                     69:        struct icmp *icmp;
                     70: #endif
                     71:        struct ether_header *eth;
                     72:        struct ip *ip;
                     73:        struct ip6_hdr *ipv6;
                     74:        struct arphdr *arp;
                     75:        struct udphdr *udp;
                     76:        struct tcphdr *tcp;
                     77: 
                     78:        assert(buffer);
                     79: 
1.3     ! misho      80:        eth = (struct ether_header *) buffer;
1.2       misho      81: 
                     82:        switch (ntohs(eth->ether_type)) {
                     83:                case ETHERTYPE_ARP:
                     84:                        strlcpy(szWrk, "(ARP)", sizeof szWrk);
                     85:                        break;
                     86:                case ETHERTYPE_IP:
                     87:                        strlcpy(szWrk, "(IP)", sizeof szWrk);
                     88:                        break;
                     89:                case ETHERTYPE_IPV6:
                     90:                        strlcpy(szWrk, "(IPv6)", sizeof szWrk);
                     91:                        break;
                     92:                default:
                     93:                        memset(szWrk, 0, sizeof szWrk);
                     94:        }
                     95:        snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_shost[0], eth->ether_shost[1], 
                     96:                        eth->ether_shost[2], eth->ether_shost[3], eth->ether_shost[4], eth->ether_shost[5]);
                     97:        snprintf(szLine, BUFSIZ, "ether_type: %04x%s, src_mac: %s, ", ntohs(eth->ether_type), szWrk, szStr);
                     98:        strlcat(szShow, szLine, USHRT_MAX);
                     99:        snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_dhost[0], eth->ether_dhost[1], 
                    100:                        eth->ether_dhost[2], eth->ether_dhost[3], eth->ether_dhost[4], eth->ether_dhost[5]);
                    101:        snprintf(szLine, BUFSIZ, "dst_mac: %s\n", szStr);
                    102:        strlcat(szShow, szLine, USHRT_MAX);
                    103: 
                    104:        switch (ntohs(eth->ether_type)) {
                    105:                case ETHERTYPE_ARP:
                    106:                        arp = (struct arphdr*) (((caddr_t) eth) + ETHER_HDR_LEN);
                    107:                        strlcat(szShow, "\t>>> ARP ...\n", USHRT_MAX);
                    108: 
                    109:                        switch (ntohs(arp->ar_op)) {
                    110:                                case ARPOP_REQUEST:
                    111:                                        strlcpy(szStr, "Request", sizeof szStr);
                    112:                                        break;
                    113:                                case ARPOP_REPLY:
                    114:                                        strlcpy(szStr, "Reply", sizeof szStr);
                    115:                                        break;
                    116:                                case ARPOP_REVREQUEST:
                    117:                                        strlcpy(szStr, "RevRequest", sizeof szStr);
                    118:                                        break;
                    119:                                case ARPOP_REVREPLY:
                    120:                                        strlcpy(szStr, "RevReply", sizeof szStr);
                    121:                                        break;
                    122:                                case ARPOP_INVREQUEST:
                    123:                                        strlcpy(szStr, "InvRequest", sizeof szStr);
                    124:                                        break;
                    125:                                case ARPOP_INVREPLY:
                    126:                                        strlcpy(szStr, "InvReply", sizeof szStr);
                    127:                                        break;
                    128:                                default:
                    129:                                        strlcpy(szStr, "*Unknown*", sizeof szStr);
                    130:                        }
                    131:                        snprintf(szLine, BUFSIZ, "\tAddr_fmt: %d, Proto_fmt: %d, Addr_len: %d, Proto_len: %d, %s\n", 
                    132:                                        ntohs(arp->ar_hrd), ntohs(arp->ar_pro), arp->ar_hln, arp->ar_pln, szStr);
                    133:                        strlcat(szShow, szLine, USHRT_MAX);
                    134:                        break;
                    135:                case ETHERTYPE_IP:
                    136:                        ip = (struct ip*) (((caddr_t) eth) + ETHER_HDR_LEN);
                    137:                        strlcat(szShow, "\t>>> IP ...\n", USHRT_MAX);
                    138: 
                    139:                        snprintf(szStr, BUFSIZ, "Frag_off: %d", ntohs(ip->ip_off) & IP_OFFMASK);
                    140:                        if (ntohs(ip->ip_off) & IP_RF)
                    141:                                strlcat(szStr, "|RF", sizeof szStr);
                    142:                        if (ntohs(ip->ip_off) & IP_DF)
                    143:                                strlcat(szStr, "|DF", sizeof szStr);
                    144:                        if (ntohs(ip->ip_off) & IP_MF)
                    145:                                strlcat(szStr, "|MF", sizeof szStr);
                    146:                        snprintf(szLine, BUFSIZ, "\tTOS: %02x, TTL: %d, ID: %04x, Cksum: %04x, Len: %d, %s\n", 
                    147:                                        ip->ip_tos, ip->ip_ttl, ntohs(ip->ip_id), ntohs(ip->ip_sum), 
                    148:                                        ntohs(ip->ip_len), szStr);
                    149:                        strlcat(szShow, szLine, USHRT_MAX);
                    150:                        snprintf(szLine, BUFSIZ, "\tProto: %d, Src_addr: %s, Dst_addr: %s\n", 
                    151:                                        ip->ip_p, inet_ntop(AF_INET, &ip->ip_src, szStr, BUFSIZ), 
                    152:                                        inet_ntop(AF_INET, &ip->ip_dst, szWrk, BUFSIZ));
                    153:                        strlcat(szShow, szLine, USHRT_MAX);
                    154: 
                    155:                        Proto = ip->ip_p;
                    156:                        break;
                    157:                case ETHERTYPE_IPV6:
                    158:                        ipv6 = (struct ip6_hdr*) (((caddr_t) eth) + ETHER_HDR_LEN);
                    159:                        strlcat(szShow, "\t>>> IPv6 ...\n", USHRT_MAX);
                    160: 
                    161:                        snprintf(szLine, BUFSIZ, "\tHLim: %d, Payload_len: %d, Flow: %u\n", ipv6->ip6_hlim, 
                    162:                                        htons(ipv6->ip6_plen), htonl(ipv6->ip6_flow) & IPV6_FLOWLABEL_MASK);
                    163:                        strlcat(szShow, szLine, USHRT_MAX);
                    164:                        inet_ntop(AF_INET6, &ipv6->ip6_src, szStr, BUFSIZ);
                    165:                        inet_ntop(AF_INET6, &ipv6->ip6_dst, szWrk, BUFSIZ);
                    166:                        snprintf(szLine, BUFSIZ, "\tNext: %d, Src_addr: %s, Dst_addr: %s\n", ipv6->ip6_nxt, szStr, szWrk);
                    167:                        strlcat(szShow, szLine, USHRT_MAX);
                    168: 
                    169:                        Proto = ipv6->ip6_nxt;
                    170:                        break;
                    171:        }
                    172:        
                    173:        if (Proto && (ntohs(eth->ether_type) == ETHERTYPE_IP || ntohs(eth->ether_type) == ETHERTYPE_IPV6))
                    174:                switch (Proto) {
                    175:                        case IPPROTO_TCP:
                    176:                                strlcat(szShow, "\t\t>>> TCP ...\n", USHRT_MAX);
                    177:                                if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
                    178:                                        tcp = (struct tcphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
                    179:                                else
                    180:                                        tcp = (struct tcphdr*) (((caddr_t) ip) + sizeof(struct ip));
                    181: 
                    182:                                snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, seq: %u, ack: %u\n", 
                    183:                                                ntohs(tcp->th_sport), ntohs(tcp->th_dport), tcp->th_seq, tcp->th_ack);
                    184:                                strlcat(szShow, szLine, USHRT_MAX);
                    185:                                snprintf(szWrk, BUFSIZ, "%d", (u_char) tcp->th_off >> 4);
                    186:                                if (tcp->th_flags & TH_FIN)
                    187:                                        strlcat(szWrk, "|FIN", sizeof szWrk);
                    188:                                if (tcp->th_flags & TH_SYN)
                    189:                                        strlcat(szWrk, "|SYN", sizeof szWrk);
                    190:                                if (tcp->th_flags & TH_RST)
                    191:                                        strlcat(szWrk, "|RST", sizeof szWrk);
                    192:                                if (tcp->th_flags & TH_PUSH)
                    193:                                        strlcat(szWrk, "|PUSH", sizeof szWrk);
                    194:                                if (tcp->th_flags & TH_ACK)
                    195:                                        strlcat(szWrk, "|ACK", sizeof szWrk);
                    196:                                if (tcp->th_flags & TH_URG)
                    197:                                        strlcat(szWrk, "|URG", sizeof szWrk);
                    198:                                if (tcp->th_flags & TH_ECE)
                    199:                                        strlcat(szWrk, "|ECE", sizeof szWrk);
                    200:                                if (tcp->th_flags & TH_CWR)
                    201:                                        strlcat(szWrk, "|CWR", sizeof szWrk);
                    202:                                snprintf(szLine, BUFSIZ, "\t\toff: %s, win: %d, urp: %04x, cksum: %04x\n", 
                    203:                                                szWrk, ntohs(tcp->th_win), ntohs(tcp->th_urp), ntohs(tcp->th_sum));
                    204:                                strlcat(szShow, szLine, USHRT_MAX);
                    205:                                break;
                    206:                        case IPPROTO_UDP:
                    207:                                strlcat(szShow, "\t\t>>> UDP ...\n", USHRT_MAX);
                    208:                                if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
                    209:                                        udp = (struct udphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
                    210:                                else
                    211:                                        udp = (struct udphdr*) (((caddr_t) ip) + sizeof(struct ip));
                    212: 
                    213:                                snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, len: %d, cksum: %04x\n", 
                    214:                                                ntohs(udp->uh_sport), ntohs(udp->uh_dport), 
                    215:                                                ntohs(udp->uh_ulen), ntohs(udp->uh_sum));
                    216:                                strlcat(szShow, szLine, USHRT_MAX);
                    217:                                break;
                    218:                        case IPPROTO_ICMP:
                    219:                                strlcat(szShow, "\t\t>>> ICMP ...\n", USHRT_MAX);
                    220: #ifdef __FreeBSD__
                    221:                                if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
                    222:                                        icmp = (struct icmphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
                    223:                                else
                    224:                                        icmp = (struct icmphdr*) (((caddr_t) ip) + sizeof(struct ip));
                    225: #endif
                    226: #ifdef __OpenBSD__
                    227:                                if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
                    228:                                        icmp = (struct icmp*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
                    229:                                else
                    230:                                        icmp = (struct icmp*) (((caddr_t) ip) + sizeof(struct ip));
                    231: #endif
                    232: 
                    233:                                snprintf(szLine, BUFSIZ, "\t\ttype: %d, code: %d: cksum: %04x\n", 
                    234:                                                icmp->icmp_type, icmp->icmp_code, ntohs(icmp->icmp_cksum));
                    235:                                strlcat(szShow, szLine, USHRT_MAX);
                    236:                                break;
                    237:                }
                    238: 
                    239:        printf("%s===\n", szShow);
                    240:        return NULL;
                    241: }
                    242: 
                    243: // ----------------------
                    244: 
                    245: int
                    246: main(int argc, char **argv)
                    247: {
                    248:        u_int n, count = (u_int) -1;
                    249:        register int i;
                    250:        int dev, fd, ret, siz = 0;
                    251:        char szStr[BUFSIZ], szMap[MAXPATHLEN] = { 0 }, *buffer = NULL;
                    252:        struct ifreq ifr;
                    253:        struct pollfd pfd = { 0 };
                    254:        pthread_t tid;
                    255:        char ch, mode = 'R';
                    256:        struct timespec ts_start, ts_end;
                    257:        void *bz = NULL;
                    258: 
                    259:        while ((ch = getopt(argc, argv, "hvwzs:p:f:")) != -1)
                    260:                switch (ch) {
                    261:                        case 'w':
                    262:                                mode = 'W';
                    263:                                break;
                    264:                        case 's':
                    265:                                siz = strtol(optarg, NULL, 0);
                    266:                                break;
                    267:                        case 'p':
                    268:                                count = strtol(optarg, NULL, 0);
                    269:                                break;
                    270:                        case 'f':
                    271:                                strlcpy(szMap, optarg, sizeof szMap);
                    272:                                break;
                    273:                        case 'z':
                    274:                                flg++;
                    275:                                break;
                    276:                        case 'v':
                    277:                                Verbose++;
                    278:                                break;
                    279:                        case 'h':
                    280:                        default:
                    281:                                return 1;
                    282:                }
                    283:        argc -= optind;
                    284:        argv += optind;
                    285:        if (argc < 1)
                    286:                return 1;
                    287: 
                    288:        if (**argv == '/')
                    289:                strlcpy(szStr, strrchr(*argv, '/') + 1, sizeof szStr);
                    290:        else
                    291:                strlcpy(szStr, *argv, sizeof szStr);
                    292: 
                    293: #ifdef __FreeBSD__
1.3     ! misho     294:        dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, (flg) ? &bz : NULL);
1.2       misho     295:        if (dev == -1)
1.3     ! misho     296:                dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, NULL);
1.2       misho     297: #else
1.3     ! misho     298:        dev = io_etherOpen(szStr, O_RDWR, 42, 0, (u_int*) &siz, NULL);
1.2       misho     299: #endif
                    300:        if (dev == -1) {
                    301:                printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
                    302:                return 1;
                    303:        } else
                    304:                printf("dev=%d(%s)\n", dev, szStr);
                    305: 
                    306:        if (ioctl(dev, BIOCGBLEN, &siz) == -1) {
                    307:                perror("ioctl(BIOCGBLEN)");
                    308:                io_etherClose(dev, &bz);
                    309:                return 1;
                    310:        } else
                    311:                printf("BPF buffer len=%d\n", siz);
                    312: 
                    313:        if (*szMap) {
                    314:                fd = open(szMap, O_RDWR);
                    315:                if (fd == -1) {
                    316:                        perror("open(map)");
                    317:                        io_etherClose(dev, &bz);
                    318:                        return 1;
                    319:                }
                    320:                buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
                    321:                close(fd);
                    322:        } else
                    323:                buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
                    324:        if (buffer == MAP_FAILED) {
                    325:                perror("mmap()");
                    326:                io_etherClose(dev, &bz);
                    327:                return 1;
                    328:        }
                    329: 
                    330:        pfd.fd = dev;
                    331:        assert(!clock_gettime(CLOCK_REALTIME, &ts_start));
                    332:        if (mode == 'R') {
                    333:                pfd.events = POLLIN;
                    334:                for (i = 0; i < count; i++) {
                    335:                        if ((ret = poll(&pfd, 1, -1)) == -1)
                    336:                                break;
                    337: 
                    338:                        ret = io_etherRecv(dev, buffer, siz, bz);
                    339:                        if (!ret)
                    340:                                continue;
                    341:                        if (ret == -1)
                    342:                                printf("%d) io_etherRecv(%d) #%d - %s\n", i, ret, 
                    343:                                                io_GetErrno(), io_GetError());
                    344:                        if (Verbose) {
                    345:                                printf("%d) +readed %d bytes\n", i, ret);
                    346:                                ShowPkt(buffer);
                    347:                        }
                    348:                }
                    349:        } else {
                    350:                pfd.events = POLLOUT;
                    351:                for (i = 0; i < count; i++) {
                    352:                        if (poll(&pfd, 1, -1) == -1)
                    353:                                break;
                    354: 
                    355:                        ret = io_etherSend(dev, buffer, siz);
                    356:                        if (ret == -1)
                    357:                                printf("io_etherSend(%d) #%d - %s\n", ret, io_GetErrno(), io_GetError());
                    358:                        if (Verbose)
                    359:                                printf("+writed %d bytes\n", ret);
                    360:                }
                    361:        }
                    362:        assert(!clock_gettime(CLOCK_REALTIME, &ts_end));
                    363: 
                    364:        munmap(buffer, siz);
                    365:        io_etherClose(dev, &bz);
                    366: 
                    367:        time_spec_sub(&ts_end, &ts_start);
                    368:        printf("%d.%09lu for %d iterations\n", ts_end.tv_sec, ts_end.tv_nsec, i);
                    369:        ts_end.tv_sec *= 1000000000 / i;
                    370:        printf("0.%09lu per/iteration\n", ts_end.tv_sec + ts_end.tv_nsec / i);
                    371:        return 0;
                    372: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>