/*---------------------------------------------------------------------------*/ void eth_dev_init() { if(sixlbr_config_use_raw_ethernet) { eth_fd = eth_alloc(sixlbr_config_eth_device); } else { eth_fd = tap_alloc(sixlbr_config_eth_device); } if(eth_fd == -1) { LOG6LBR_FATAL("eth_dev_init() : %s\n", strerror(errno)); exit(1); } select_set_callback(eth_fd, ð_select_callback); LOG6LBR_INFO("opened device /dev/%s\n", sixlbr_config_eth_device); atexit(cleanup); signal(SIGHUP, sigcleanup); signal(SIGTERM, sigcleanup); signal(SIGINT, sigcleanup); ifconf(sixlbr_config_eth_device); #if !CETIC_6LBR_ONE_ITF if(sixlbr_config_use_raw_ethernet) { #endif fetch_mac(eth_fd, sixlbr_config_eth_device, ð_mac_addr); LOG6LBR_ETHADDR(INFO, ð_mac_addr, "Eth MAC address : "); eth_mac_addr_ready = 1; #if !CETIC_6LBR_ONE_ITF } #endif }
int tap_probe(struct device_d *dev) { struct eth_device *edev; struct tap_priv *priv; int ret = 0; priv = xzalloc(sizeof(struct tap_priv)); priv->name = "barebox"; priv->fd = tap_alloc(priv->name); if (priv->fd < 0) { ret = priv->fd; goto out; } edev = xzalloc(sizeof(struct eth_device)); edev->priv = priv; edev->parent = dev; edev->init = tap_eth_open; edev->open = tap_eth_open; edev->send = tap_eth_send; edev->recv = tap_eth_rx; edev->halt = tap_eth_halt; edev->get_ethaddr = tap_get_ethaddr; edev->set_ethaddr = tap_set_ethaddr; eth_register(edev); return 0; out: free(priv); return ret; }
struct vxlan_instance * create_vxlan_instance (u_int8_t * vni) { char cbuf[16]; u_int32_t vni32; struct vxlan_instance * vins; /* create socket and fdb */ vins = (struct vxlan_instance *) malloc (sizeof (struct vxlan_instance)); memset (vins, 0, sizeof (struct vxlan_instance)); memcpy (vins->vni, vni, VXLAN_VNISIZE); snprintf (cbuf, 16, "0x%02x%02x%02x", vins->vni[0],vins->vni[1], vins->vni[2]); vni32 = strtol (cbuf, NULL, 0); snprintf (vins->vxlan_tap_name, IFNAMSIZ, "%s%X", VXLAN_TUNNAME, vni32); vins->fdb = init_fdb (); vins->tap_sock = tap_alloc (vins->vxlan_tap_name); /* create out bound MAC/ARP/ND/RA access list */ init_hash (&vins->acl_mac, ETH_ALEN); init_hash (&vins->acl_ip4, sizeof (struct in_addr)); init_hash (&vins->acl_ip6, sizeof (struct in6_addr)); return vins; }
int main(int argc, char *argv[]) { co_rc_t rc; int exit_code = 0; co_daemon_handle_t daemon_handle_; int tap_fd; char tap_name[0x30]; int unit = 0; int colinux_instance = 0; co_terminal_print("Cooperative Linux TAP network daemon\n"); if (argc < 3) { syntax(); return -1; } snprintf(tap_name, sizeof(tap_name), "conet-host-%d-%d", colinux_instance, unit); co_terminal_print("creating network %s\n", tap_name); tap_fd = tap_alloc(tap_name); if (tap_fd < 0) { co_terminal_print("Error opening TAP\nn"); exit_code = -1; goto out; } co_terminal_print("TAP interface %s created\n", tap_name); rc = co_os_open_daemon_pipe(0, CO_MODULE_CONET0, &daemon_handle_); if (!CO_OK(rc)) { co_terminal_print("Error opening a pipe to the daemon\n"); goto out_close; } wait_loop(daemon_handle_, tap_fd); co_os_daemon_close(daemon_handle_); out_close: close(tap_fd); out: return exit_code; }
int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required) { char dev[10]=""; int fd; if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){ fprintf(stderr, "Cannot allocate TAP device\n"); return -1; } pstrcpy(ifname, ifname_size, dev); if (*vnet_hdr) { /* Solaris doesn't have IFF_VNET_HDR */ *vnet_hdr = 0; if (vnet_hdr_required && !*vnet_hdr) { error_report("vnet_hdr=1 requested, but no kernel " "support for IFF_VNET_HDR available"); close(fd); return -1; } } fcntl(fd, F_SETFL, O_NONBLOCK); return fd; }
int main(int argc, char **argv) { char buf[BUF_SIZE], dev[IFNAMSIZ]="/dev/net/tun", tapdev[IFNAMSIZ], *progname; int tap, r, tun = IFF_TUN; int c, verbose = 0, interval = 0, keep = 0, keep_count = 0; time_t last_keep; struct timeval tv; struct rlimit rlim; fd_set rfd; while ( (c=getopt(argc,argv,"vi:k:")) != -1 ) { switch (c) { case 'v': verbose++; break; case 'i': interval = atoi(optarg); break; case 'k': keep = atoi(optarg); break; } } if ((progname = strrchr(argv[0], '/')) == NULL) { progname = argv[0]; } else { progname++; } if (keep && !interval) { write_str(STDERR_FILENO, progname); write_cstr(STDERR_FILENO, ": keep alive need interval value.\n"); return 1; } signal(SIGPIPE,signalHandler); if (strcmp("tunio", progname)) tun = IFF_TAP; if (argc>optind) { if (!strncmp(argv[optind],"/dev/",5)) argv[optind] += 5; if (verbose) { write_str(STDERR_FILENO, progname); write_cstr(STDERR_FILENO, ": Forced to "); write_str(STDERR_FILENO,argv[optind]); write_cstr(STDERR_FILENO, "\n"); } strstart(tapdev); strarray(tapdev); strannex(tapdev,argv[optind]); tap = tap_alloc (dev, tapdev, tun); } else { strstart(tapdev); for (r=0 ; r<1000 ; r++) { strarray(tapdev); if (tun == IFF_TUN) { strannex(tapdev, "tun"); } else { strannex(tapdev, "tap"); } strannex_uint(tapdev, r); tap = tap_alloc (dev, tapdev, tun); if (tap > 0) break; // success } } if (tap <= 0) { write_str(STDERR_FILENO, progname); if (tun == IFF_TUN) { write_cstr(STDERR_FILENO, ": Cannot open TUN\n"); } else { write_cstr(STDERR_FILENO, ": Cannot open TAP\n"); } return 1; } gettimeofday(&tv, NULL); last_keep = tv.tv_sec; rlim.rlim_cur = 0; setrlimit(RLIMIT_NOFILE, &rlim); /* ------------ main loop without length ---------- */ for (;;) { FD_ZERO(&rfd); FD_SET(STDIN_FILENO, &rfd); FD_SET(tap, &rfd); tv.tv_sec = 1; tv.tv_usec = 0; r = select(tap+1,&rfd,NULL,NULL,&tv); if(r == -1) { write_str(STDERR_FILENO, progname); write_cstr(STDERR_FILENO, ": select() error.\n"); return errno; } if (interval) { gettimeofday(&tv, NULL); if (verbose && (tv.tv_sec % 10)==0) { write_str(STDERR_FILENO, progname); write_str_uint(STDERR_FILENO, ": keep count: ", keep_count); writeln_str_uint(STDERR_FILENO, ", keep: ", keep); } if (tv.tv_sec >= (last_keep+interval)) { write(STDOUT_FILENO, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20); if (verbose) { write_str(STDERR_FILENO, progname); write_cstr(STDERR_FILENO, ": Sent a keep alive.\n"); } last_keep = tv.tv_sec; } } if (keep) { keep_count++; if (verbose) { write_str(STDERR_FILENO, progname); writeln_str_uint(STDERR_FILENO, ": keep count increased: ", keep_count); } } if (FD_ISSET(STDIN_FILENO,&rfd)) { r = read(STDIN_FILENO, buf, BUF_SIZE); if (r > 0 && keep) { // reset count on read. keep_count = 0; if (verbose) { write_str(STDERR_FILENO, progname); write_cstr(STDERR_FILENO, ": data received, keep count reseted.\n"); } } if (r > 20) { // ignore packet inferior to 21 bytes, keep alive write(tap, buf, r); } else if (r <= 0) { if (verbose) { write_str(STDERR_FILENO, progname); write_cstr(STDERR_FILENO, ": stdin error.\n"); } return 1; } } if (FD_ISSET(tap,&rfd)) { r = read(tap, buf, BUF_SIZE); if (r > 0) { write(STDOUT_FILENO, buf, r); } else if (r <= 0) { if (verbose) { write_str(STDERR_FILENO, progname); write_cstr(STDERR_FILENO, ": stdout error.\n"); } return 2; } } if (keep && keep_count >= keep) { write_str(STDERR_FILENO, progname); writeln_str_uint(STDERR_FILENO, ": Keep count exceeded ", keep_count); kill(getppid(), SIGPIPE); return 0; } } return 0; }
int main(int argc, char **argv) { int tapfd, socketfd, len; struct sockaddr_in local_addr = {}; struct sockaddr_in rem_addr = {}; char *a_name = malloc(sizeof(char)*SIZE); struct hdr *hdr_buf; unsigned char *buf; /* receive buffer */ buf = (unsigned char *)malloc(BUFSIZE); if (argc != 4) usage(argv[0]); /* Implies exit() */ a_name[0]='\0'; tapfd = tap_alloc(a_name, IFF_TAP | IFF_NO_PI); socketfd = socket(AF_INET, SOCK_DGRAM, 0); if (socketfd < 0) { perror("ERROR opening socket"); exit(1); } printf("Binding: IPv4\n"); local_addr.sin_family = AF_INET; local_addr.sin_addr.s_addr = htonl(INADDR_ANY); local_addr.sin_port = htons(atoi(argv[2])); bind(socketfd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr_in)); rem_addr.sin_family = AF_INET; rem_addr.sin_addr.s_addr = inet_addr(argv[1]); rem_addr.sin_port = htons(atoi(argv[2])); for(;;) { struct pollfd fds[] = { { socketfd, POLLIN }, { tapfd, POLLIN } }; if ( poll(fds,2,500) > 0 ) { if (fds[0].revents & POLLIN) { len = sizeof(rem_addr); len = recvfrom(socketfd, buf, BUFSIZE, 0, (struct sockaddr *)&rem_addr, &len); hdr_buf = (struct hdr *)buf; //buf = xorcypher(buf,len, argv[3]); hdr_buf = (struct hdr*)xorcypher((char*)hdr_buf,len, argv[3]); len = write(tapfd, hdr_buf->data, len-sizeof(struct hdr)); fprintf(stdout, "Wrote %d bytes from network to tap\n", len); fprintf(stdout, "and got '%s' as text\n", hdr_buf->text); } if (fds[1].revents & POLLIN) { len = read(tapfd, buf+sizeof(struct hdr), BUFSIZE); hdr_buf = (struct hdr *)buf; strcpy(hdr_buf->text,"Helloooo!!"); //buf = xorcypher(buf,len,argv[3]); hdr_buf = (struct hdr*)xorcypher((char*)hdr_buf,len+sizeof(struct hdr),argv[3]); len = sendto(socketfd, hdr_buf, len+sizeof(struct hdr), 0, (struct sockaddr *)&rem_addr, sizeof(rem_addr)); fprintf(stdout, "Wrote %d bytes from tap to network\n", len); } } } return 0; }
int main(int arc, char **argv) { int i; struct glob_arg g; int ch; int wait_link = 2; int devqueues = 1; /* how many device queues */ bzero(&g, sizeof(g)); g.main_fd = -1; g.td_body = receiver_body; g.report_interval = 1000; /* report interval */ g.affinity = -1; /* ip addresses can also be a range x.x.x.x-x.x.x.y */ g.src_ip.name = "10.0.0.1"; g.dst_ip.name = "10.1.0.1"; g.dst_mac.name = "ff:ff:ff:ff:ff:ff"; g.src_mac.name = NULL; g.pkt_size = 60; g.burst = 512; // default g.nthreads = 1; g.cpus = 1; g.forever = 1; g.tx_rate = 0; g.frags = 1; g.nmr_config = ""; g.virt_header = 0; while ( (ch = getopt(arc, argv, "a:f:F:n:i:Il:d:s:D:S:b:c:o:p:T:w:WvR:XC:H:e:m:P:zZ")) != -1) { struct sf *fn; switch(ch) { default: D("bad option %c %s", ch, optarg); usage(); break; case 'n': g.npackets = atoi(optarg); break; case 'F': i = atoi(optarg); if (i < 1 || i > 63) { D("invalid frags %d [1..63], ignore", i); break; } g.frags = i; break; case 'f': for (fn = func; fn->key; fn++) { if (!strcmp(fn->key, optarg)) break; } if (fn->key) g.td_body = fn->f; else D("unrecognised function %s", optarg); break; case 'o': /* data generation options */ g.options = atoi(optarg); break; case 'a': /* force affinity */ g.affinity = atoi(optarg); break; case 'i': /* interface */ /* a prefix of tap: netmap: or pcap: forces the mode. * otherwise we guess */ D("interface is %s", optarg); if (strlen(optarg) > MAX_IFNAMELEN - 8) { D("ifname too long %s", optarg); break; } strcpy(g.ifname, optarg); if (!strcmp(optarg, "null")) { g.dev_type = DEV_NETMAP; g.dummy_send = 1; } else if (!strncmp(optarg, "tap:", 4)) { g.dev_type = DEV_TAP; strcpy(g.ifname, optarg + 4); } else if (!strncmp(optarg, "pcap:", 5)) { g.dev_type = DEV_PCAP; strcpy(g.ifname, optarg + 5); } else if (!strncmp(optarg, "netmap:", 7) || !strncmp(optarg, "vale", 4)) { g.dev_type = DEV_NETMAP; } else if (!strncmp(optarg, "tap", 3)) { g.dev_type = DEV_TAP; } else { /* prepend netmap: */ g.dev_type = DEV_NETMAP; sprintf(g.ifname, "netmap:%s", optarg); } break; case 'I': g.options |= OPT_INDIRECT; /* XXX use indirect buffer */ break; case 'l': /* pkt_size */ g.pkt_size = atoi(optarg); break; case 'd': g.dst_ip.name = optarg; break; case 's': g.src_ip.name = optarg; break; case 'T': /* report interval */ g.report_interval = atoi(optarg); break; case 'w': wait_link = atoi(optarg); break; case 'W': /* XXX changed default */ g.forever = 0; /* do not exit rx even with no traffic */ break; case 'b': /* burst */ g.burst = atoi(optarg); break; case 'c': g.cpus = atoi(optarg); break; case 'p': g.nthreads = atoi(optarg); break; case 'D': /* destination mac */ g.dst_mac.name = optarg; break; case 'S': /* source mac */ g.src_mac.name = optarg; break; case 'v': verbose++; break; case 'R': g.tx_rate = atoi(optarg); break; case 'X': g.options |= OPT_DUMP; break; case 'C': g.nmr_config = strdup(optarg); break; case 'H': g.virt_header = atoi(optarg); break; case 'e': /* extra bufs */ g.extra_bufs = atoi(optarg); break; case 'm': if (strcmp(optarg, "tx") == 0) { g.options |= OPT_MONITOR_TX; } else if (strcmp(optarg, "rx") == 0) { g.options |= OPT_MONITOR_RX; } else { D("unrecognized monitor mode %s", optarg); } break; case 'P': g.packet_file = strdup(optarg); break; case 'z': g.options |= OPT_RANDOM_SRC; break; case 'Z': g.options |= OPT_RANDOM_DST; break; } } if (strlen(g.ifname) <=0 ) { D("missing ifname"); usage(); } i = system_ncpus(); if (g.cpus < 0 || g.cpus > i) { D("%d cpus is too high, have only %d cpus", g.cpus, i); usage(); } if (g.cpus == 0) g.cpus = i; if (g.pkt_size < 16 || g.pkt_size > MAX_PKTSIZE) { D("bad pktsize %d [16..%d]\n", g.pkt_size, MAX_PKTSIZE); usage(); } if (g.src_mac.name == NULL) { static char mybuf[20] = "00:00:00:00:00:00"; /* retrieve source mac address. */ if (source_hwaddr(g.ifname, mybuf) == -1) { D("Unable to retrieve source mac"); // continue, fail later } g.src_mac.name = mybuf; } /* extract address ranges */ extract_ip_range(&g.src_ip); extract_ip_range(&g.dst_ip); extract_mac_range(&g.src_mac); extract_mac_range(&g.dst_mac); if (g.src_ip.start != g.src_ip.end || g.src_ip.port0 != g.src_ip.port1 || g.dst_ip.start != g.dst_ip.end || g.dst_ip.port0 != g.dst_ip.port1) g.options |= OPT_COPY; if (g.virt_header != 0 && g.virt_header != VIRT_HDR_1 && g.virt_header != VIRT_HDR_2) { D("bad virtio-net-header length"); usage(); } if (g.dev_type == DEV_TAP) { D("want to use tap %s", g.ifname); g.main_fd = tap_alloc(g.ifname); if (g.main_fd < 0) { D("cannot open tap %s", g.ifname); usage(); } #ifndef NO_PCAP } else if (g.dev_type == DEV_PCAP) { char pcap_errbuf[PCAP_ERRBUF_SIZE]; pcap_errbuf[0] = '\0'; // init the buffer g.p = pcap_open_live(g.ifname, 256 /* XXX */, 1, 100, pcap_errbuf); if (g.p == NULL) { D("cannot open pcap on %s", g.ifname); usage(); } g.main_fd = pcap_fileno(g.p); D("using pcap on %s fileno %d", g.ifname, g.main_fd); #endif /* !NO_PCAP */ } else if (g.dummy_send) { /* but DEV_NETMAP */ D("using a dummy send routine"); } else { struct nmreq base_nmd; bzero(&base_nmd, sizeof(base_nmd)); parse_nmr_config(g.nmr_config, &base_nmd); if (g.extra_bufs) { base_nmd.nr_arg3 = g.extra_bufs; } /* * Open the netmap device using nm_open(). * * protocol stack and may cause a reset of the card, * which in turn may take some time for the PHY to * reconfigure. We do the open here to have time to reset. */ g.nmd = nm_open(g.ifname, &base_nmd, 0, NULL); if (g.nmd == NULL) { D("Unable to open %s: %s", g.ifname, strerror(errno)); goto out; } g.main_fd = g.nmd->fd; D("mapped %dKB at %p", g.nmd->req.nr_memsize>>10, g.nmd->mem); /* get num of queues in tx or rx */ if (g.td_body == sender_body) devqueues = g.nmd->req.nr_tx_rings; else devqueues = g.nmd->req.nr_rx_rings; /* validate provided nthreads. */ if (g.nthreads < 1 || g.nthreads > devqueues) { D("bad nthreads %d, have %d queues", g.nthreads, devqueues); // continue, fail later } if (verbose) { struct netmap_if *nifp = g.nmd->nifp; struct nmreq *req = &g.nmd->req; D("nifp at offset %d, %d tx %d rx region %d", req->nr_offset, req->nr_tx_rings, req->nr_rx_rings, req->nr_arg2); for (i = 0; i <= req->nr_tx_rings; i++) { struct netmap_ring *ring = NETMAP_TXRING(nifp, i); D(" TX%d at 0x%lx slots %d", i, (char *)ring - (char *)nifp, ring->num_slots); } for (i = 0; i <= req->nr_rx_rings; i++) { struct netmap_ring *ring = NETMAP_RXRING(nifp, i); D(" RX%d at 0x%lx slots %d", i, (char *)ring - (char *)nifp, ring->num_slots); } } /* Print some debug information. */ fprintf(stdout, "%s %s: %d queues, %d threads and %d cpus.\n", (g.td_body == sender_body) ? "Sending on" : "Receiving from", g.ifname, devqueues, g.nthreads, g.cpus); if (g.td_body == sender_body) { fprintf(stdout, "%s -> %s (%s -> %s)\n", g.src_ip.name, g.dst_ip.name, g.src_mac.name, g.dst_mac.name); } out: /* Exit if something went wrong. */ if (g.main_fd < 0) { D("aborting"); usage(); } } if (g.options) { D("--- SPECIAL OPTIONS:%s%s%s%s%s\n", g.options & OPT_PREFETCH ? " prefetch" : "", g.options & OPT_ACCESS ? " access" : "", g.options & OPT_MEMCPY ? " memcpy" : "", g.options & OPT_INDIRECT ? " indirect" : "", g.options & OPT_COPY ? " copy" : ""); } g.tx_period.tv_sec = g.tx_period.tv_nsec = 0; if (g.tx_rate > 0) { /* try to have at least something every second, * reducing the burst size to some 0.01s worth of data * (but no less than one full set of fragments) */ uint64_t x; int lim = (g.tx_rate)/300; if (g.burst > lim) g.burst = lim; if (g.burst < g.frags) g.burst = g.frags; x = ((uint64_t)1000000000 * (uint64_t)g.burst) / (uint64_t) g.tx_rate; g.tx_period.tv_nsec = x; g.tx_period.tv_sec = g.tx_period.tv_nsec / 1000000000; g.tx_period.tv_nsec = g.tx_period.tv_nsec % 1000000000; } if (g.td_body == sender_body) D("Sending %d packets every %ld.%09ld s", g.burst, g.tx_period.tv_sec, g.tx_period.tv_nsec); /* Wait for PHY reset. */ D("Wait %d secs for phy reset", wait_link); sleep(wait_link); D("Ready..."); /* Install ^C handler. */ global_nthreads = g.nthreads; signal(SIGINT, sigint_h); start_threads(&g); main_thread(&g); return 0; }