Exemplo n.º 1
0
int main(void)
{
	int fd, _read = 0;
	void *a;
	char c, buf[PRINT_CHUNK_SZ];

	fd = open(PROC_FILE, O_RDWR);
	if (fd < 0) {
		perror("open");
		exit(-1);
	}

	trans_ioctl_set_channel(fd, COS_TRANS_SERVICE_PONG);
	trans_ioctl_set_direction(fd, COS_TRANS_DIR_LTOC);
	a = mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	if (MAP_FAILED == a) {
		perror("mmap");
		exit(-1);
	}
	cringbuf_init(&sharedbuf, a, MAP_SIZE);
	/* wait for user to start */
	_read = read(0, buf, PRINT_CHUNK_SZ);
	assert(_read >= 0);
	while (1) {
		int off = 0;
		unsigned long long *p;


		//_read = read(0, buf, PRINT_CHUNK_SZ);
		_read = 8;
		p = (unsigned long long *)buf;
		rdtscll(*p);

		do {
			int p;

			p = cringbuf_produce(&sharedbuf, buf + off, _read);
			_read -= p;
			off += p;
			if (p) {
				write(fd, &c, 1);
			}
		} while (_read);
	}

	if (munmap(a, MAP_SIZE) < 0) {
		perror("munmap");
		exit(-1);
	}

	return 0;
}
int main(void)
{
	int fd;
	void *a;
	char c, buf[PRINT_CHUNK_SZ];

	fd = open(PROC_FILE, O_RDWR);
	if (fd < 0) {
		perror("open");
		exit(-1);
	}

	trans_ioctl_set_channel(fd, 0);
	a = mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	if (MAP_FAILED == a) {
		perror("mmap");
		exit(-1);
	}
	cringbuf_init(&sharedbuf, a, MAP_SIZE);
	
	while (1) {
		int amnt;
		/* wait for an event... */
		read(fd, &c, 1);
		do {
			amnt = cringbuf_consume(&sharedbuf, buf, PRINT_CHUNK_SZ);
			write(1 , buf, amnt); /* write to stdout */
		} while (amnt);
	}

	if (munmap(a, MAP_SIZE) < 0) {
		perror("munmap");
		exit(-1);
	}
	/* if (write(fd, name, strlen(name)) < 0) { */
	/* 	perror("read"); */
	/* 	exit(-1); */
	/* } */
	
	return 0;
}
Exemplo n.º 3
0
void *
recv_pkt(void *data)
{
    char **arguments = (char**) data;

    int msg_size = atoi(arguments[1]), sleep_var = atoi(arguments[2]);
    int fdr, ret;
    char buf[] = "ifconfig "IFNAME" inet "IPADDR" netmask 255.255.255.255 pointopoint "P2PPEER;
    struct ifreq ifr;

    int fd_low, fd_high, _read = 0;
    void *a, *b;
    char c, buf1[PRINT_CHUNK_SZ];

    printf("Message size is (%d)\n", msg_size);
    memset(buf1, 'b', PRINT_CHUNK_SZ);
   
    // open the tun device
    if((fdr = open("/dev/net/tun", O_RDWR)) < 0) {
        perror("open /dev/net/tun: ");
        exit(-1);
    }
    printf("tun FD (%d) created\n", fdr);
    
    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TUN | IFF_NO_PI;

    if(ioctl(fdr, TUNSETIFF, (void *) &ifr) < 0) {
        perror("ioctl TUNSETIFF: ");
        close(fdr);
        exit(-1);
    }
    
    printf("Running: %s\n", buf);
    if(system(buf) < 0) {
        perror("setting tun ipaddress: ");
        exit(-1);
    }
    printf("Done setting TUN. \n");

    fd_low = open(PROC_FILE, O_RDWR);
    fd_high = open(PROC_FILE, O_RDWR);
    if (fd_low < 0 || fd_high < 0) {
      perror("open");
      exit(-1);
    }
    printf("LO %d HI %d\n", COS_TRANS_SERVICE_LO, COS_TRANS_SERVICE_HI);
    trans_ioctl_set_channel(fd_low, COS_TRANS_SERVICE_LO);
    trans_ioctl_set_channel(fd_high, COS_TRANS_SERVICE_HI);
    trans_ioctl_set_direction(fd_low, COS_TRANS_DIR_LTOC);
    trans_ioctl_set_direction(fd_high, COS_TRANS_DIR_LTOC);
    a = mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd_low, 0);
    b = mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd_high, 0);
    if (MAP_FAILED == a || MAP_FAILED == b) {
      perror("mmap");
      exit(-1);
    }
    cringbuf_init(&sharedbuf, a, MAP_SIZE);
    cringbuf_init(&sharedbuf, b, MAP_SIZE);

    char portstr[7];
    int portnum;
    portstr[6] = '\0';
    printf("going to sleep for a few seconds; hurry and activate composite!\n");
    sleep(sleep_var); 
    int wl, wh; 
    do {
      int amread = 0;
    
      ret = read(fdr, buf1, msg_size);
      amread += snprintf(portstr, 7, "%#x", buf1[22] & 0xff);
      if((buf1[23] & 0xff) < 16) 
	portstr[amread++] = '0';
        amread += snprintf(portstr+amread, 7, "%x", buf1[23] & 0xff);
        portnum = strtol(portstr, NULL, 16);

      switch(portnum) { 
      case HIPRIOPORT:
	wh = write(fd_high, &c, 1);
	if(wh)
	  printf("hi prior\n");
	hi++;
	break;
      case LOPRIOPORT:	
	//	if(unlikely(lo < MAXIPI)) {
	wl = write(fd_low, &c, 1);
	if(wl)
	  printf("lo prio\n");
	//lo++;
	  //ipis++;
	//}
	break;
      default:
	break;
      }
    } while(1);

    close(fdr);
    if (munmap(a, MAP_SIZE) < 0) {
      perror("munmap");
      exit(-1);
    }
    pthread_exit(NULL);
}