Ejemplo n.º 1
0
static rofl_result_t netfpga_init_port(switch_port_t* port){




	struct ifreq interface;
	netfpga_port_t* nport = (netfpga_port_t*)malloc(sizeof(*nport));

	
	char *useless;

	
	
		//fprintf(stderr, "device=  %s  \n",dev);
	useless = pcap_lookupdev(port->name); //test if device exist// gives char pointer, why not pcap_if_t?
	if (useless == NULL) {
		ROFL_ERR( "Couldn't find device: error= %s; no permission to listen on interface or other failure  \n", port->name);
		return ROFL_FAILURE;	
	}
	ROFL_DEBUG("Device :%s  found\n", port->name);

		
	char errbuf[PCAP_ERRBUF_SIZE];

	
	nport->pcap_fd = pcap_open_live(port->name, BUFSIZ, 1, 0, errbuf);//wait until the packet arrive, NO TIMEOUT
	if (nport->pcap_fd == NULL) {
		 ROFL_ERR( "Couldn't open device %s : %s\n",port->name, errbuf);
		 return ROFL_FAILURE;
	}

	nport->fd = pcap_get_selectable_fd(nport->pcap_fd);
	nport->test=25;	
	ROFL_DEBUG("pcap_open_live: socket opened \n ");
	



	ROFL_DEBUG("Ports.c creating socket over %s inerface\n", port->name);
	strncpy(interface.ifr_ifrn.ifrn_name, port->name, IFNAMSIZ/*&SWITCH_PORT_MAX_LEN_NAME*/);
	

	int flags;

	/* Set non-blocking mode. */
	flags = fcntl(nport->fd, F_GETFL, 0);
	if(fcntl(nport->fd, F_SETFL, flags | O_NONBLOCK) < 0) {
		return ROFL_FAILURE;
	}

	//Store in platform state and return
	port->platform_port_state = (platform_port_state_t*) nport;	

	return ROFL_SUCCESS;
} 
rofl_result_t netfpga_write_reg(netfpga_device_t *nfpga, uint32_t reg_id, uint32_t value){


	
	uint64_t reg;
	reg = ((uint64_t)reg_id << 32) + (uint64_t)value;

	if(ioctl(nfpga->fd, NETFPGA_IOCTL_CMD_WRITE_REG,reg) != 0){
		ROFL_ERR("ioctl failed on writing register fd: %d REG: 0x%x\n", nfpga->fd, reg_id);
		
		return ROFL_FAILURE;
	}

	

	return ROFL_SUCCESS;
}
rofl_result_t netfpga_read_reg(netfpga_device_t *nfpga, uint32_t reg_id, uint32_t *value){
	


	uint64_t v=(uint64_t)reg_id;

	//Calling ioctl
	if(ioctl(nfpga->fd, NETFPGA_IOCTL_CMD_READ_REG, &v) != 0){
		ROFL_ERR("ioctl failed on reading register fd: %d REG: 0x%x\n", nfpga->fd, reg_id);
		return ROFL_FAILURE;
	}

	//Put value and return	
	*value = (uint32_t)v;





	
	return ROFL_SUCCESS;
}