int ipaugenblick_create_client(ipaugenblick_update_cbk_t update_cbk)
{
    int ringset_idx;
    ipaugenblick_cmd_t *cmd;
    char ringname[1024];

    if(rte_ring_dequeue(free_clients_ring,(void **)&ringset_idx)) {
        syslog(LOG_ERR,"%s %d\n",__FILE__,__LINE__);
        return -1;
    }
    sprintf(ringname,"%s%d",FREE_CLIENTS_RING,ringset_idx);
    client_ring = rte_ring_lookup(ringname);
    if(!client_ring) {
	syslog(LOG_ERR,"%s %d\n",__FILE__,__LINE__);
        return -2;
    }
    cmd = ipaugenblick_get_free_command_buf();
    if(!cmd) {
       ipaugenblick_stats_cannot_allocate_cmd++;
       return -3;
    }

    cmd->cmd = IPAUGENBLICK_CONNECT_CLIENT;
    cmd->ringset_idx = ringset_idx;
    if(ipaugenblick_enqueue_command_buf(cmd)) {
       ipaugenblick_free_command_buf(cmd);
       return -3;
    }
    g_client_ringset_idx = (uint32_t)ringset_idx;
    ipaugenblick_update_cbk = update_cbk;
    return 0;
}
int ipaugenblick_open_socket(int family,int type,int parent)
{
	ipaugenblick_socket_t *ipaugenblick_socket;
	ipaugenblick_cmd_t *cmd; 

	if(rte_ring_dequeue(free_connections_ring,(void **)&ipaugenblick_socket)) {
        	syslog(LOG_ERR,"%s %d\n",__FILE__,__LINE__);
        	return -1;
    	}

    	/* allocate a ringset (cmd/tx/rx) here */
    	cmd = ipaugenblick_get_free_command_buf();
    	if(!cmd) {
        	ipaugenblick_stats_cannot_allocate_cmd++;
        	return -2;
    	}

    	cmd->cmd = IPAUGENBLICK_OPEN_SOCKET_COMMAND;
	cmd->ringset_idx = ipaugenblick_socket->connection_idx;
    	cmd->parent_idx = parent;
	cmd->u.open_sock.family = family;
	cmd->u.open_sock.type = type;
	cmd->u.open_sock.pid = getpid();
	if(ipaugenblick_enqueue_command_buf(cmd)) {
        	ipaugenblick_free_command_buf(cmd);
	        return -3;
    	}

    	local_socket_descriptors[ipaugenblick_socket->connection_idx].socket = ipaugenblick_socket;
	if(parent != -1)
		local_socket_descriptors[ipaugenblick_socket->connection_idx].select = parent;

    	return ipaugenblick_socket->connection_idx;
}
int ipaugenblick_read_updates(void)
{
	struct rte_mbuf *mbuf = NULL;
	unsigned char cmd = 0;
	
	if(rte_ring_dequeue(client_ring,(void **)&mbuf)) {
		syslog(LOG_ERR,"%s %d\n",__FILE__,__LINE__);
		return -1;
	}
	unsigned char *p = rte_pktmbuf_mtod(mbuf, unsigned char *);
	switch(*p) {
		case IPAUGENBLICK_NEW_IFACES:
		if(ipaugenblick_update_cbk) {
			cmd = 1;
			p++;
			ipaugenblick_update_cbk(cmd,p,rte_pktmbuf_data_len(mbuf) - 1);
		}
		break;
		case IPAUGENBLICK_NEW_ADDRESSES:
		if(ipaugenblick_update_cbk) {
			cmd = 3;
			p++;
			ipaugenblick_update_cbk(cmd,p,rte_pktmbuf_data_len(mbuf) - 1);
		}
		break;
		case IPAUGENBLICK_END_OF_RECORD:
		return 0;
	}
	return -1;
}
Exemple #4
0
int
CheckEtherOutRing(void)
{
   struct rte_mbuf **Msg, *mbuf;
   int mac_status = 0;

   int num = rte_ring_dequeue(ip_to_ether_ring_recv, (void **)&Msg);
   if(num < 0) {
      return 0;
   }
   mbuf = *Msg;
   printf("******Mbuf received = %p\n", mbuf);
   rte_mempool_put(buffer_message_pool, Msg);
   struct ipv4_hdr *hdr = (struct ipv4_hdr *)rte_pktmbuf_mtod (mbuf, struct ipv4_hdr*);
   unsigned char dest_mac[6];
   mac_status = get_mac(ntohl(hdr->dst_addr), dest_mac);
   if(mac_status) {
      printf("mac found sending packet out.\n");
      ether_out(dest_mac, NULL, ETHER_TYPE_IPv4, mbuf);
   }
   else {
      printf("mac not found pushing packet back to queue.\n");
      // put this mbuf bak in queue.
   }
   return 0;
}
Exemple #5
0
//the following is the module interface
struct ring_buf * getPacket(void *handle){
	struct ring_buf * ptr = NULL;
	IpImpl * impl = (IpImpl *)handle;
	rte_ring_dequeue(impl -> r, (void **)&ptr);
	if(ptr != NULL)
	printf("Ptr in getPacket: type:%d addr:%p.\n",ptr -> type, ptr -> ptr);
	return ptr;
}
int ipaugenblick_open_select(void)
{
    int ringset_idx;

    if(rte_ring_dequeue(selectors_ring,(void **)&ringset_idx)) {
        syslog(LOG_ERR,"%s %d\n",__FILE__,__LINE__);
        return -1;
    }
    return (int)ringset_idx;
}
int socket_read_nonblock(int ser_id, unsigned char *buffer)
{
   void *msg;
   struct tcb *ptcb = get_tcb_by_identifier(ser_id);
   while (rte_ring_dequeue(ptcb->socket_tcb_ring_recv, &msg) < 0){
      usleep(5);
      continue;
   }
//   printf("Received %s and len %d\n",(char *)msg, strlen(msg));
   memcpy(buffer, msg, strlen(msg));
   rte_mempool_put(buffer_message_pool, msg);
 
   return strlen(msg);//GetData(ser_id, buffer);
}
Exemple #8
0
int anscli_ring_recv(void *buff, int buff_len)
{
    void *msg;
    int wait_num = 0;
    while (wait_num < 10)
    {
        if (rte_ring_dequeue(anscli_ring_rx, &msg) < 0)
        {
            wait_num++;
            usleep(100000);  /* 100 ms */
            continue;
        }
        rte_memcpy(buff, msg, buff_len);
        rte_mempool_put(anscli_message_pool, msg);
        return ANSCLI_RECV_MSG;
    }

    return ANSCLI_NONRECV_MSG;
}
Exemple #9
0
int netdpcmd_ring_recv(void *buff, int buff_len)
{
    void *msg;
    int wait_num = 0; 
    while (wait_num < 100)
    {
        if (rte_ring_dequeue(netdpcmd_ring_rx, &msg) < 0)
        {
            wait_num++;
            usleep(200);
            continue;
        }
        rte_memcpy(buff, msg, buff_len);
        rte_mempool_put(netdpcmd_message_pool, msg);
        return NETDPCMD_RECV_MSG;
    }

    return NETDPCMD_NONRECV_MSG;
}
void send_loop(void)
{
	RTE_LOG(INFO, APP, "send_loop()\n");
	char pkt[PKT_SIZE] = {0};
	int nreceived;

	int retval = 0;
	(void) retval;
#ifdef CALC_CHECKSUM
	unsigned int kk = 0;
#endif
	srand(time(NULL));

	//Initializate packet contents
	int i;
	for(i = 0; i < PKT_SIZE; i++)
		pkt[i] = rand()%256;

#if ALLOC_METHOD == ALLOC_APP
	struct rte_mempool * packets_pool = rte_mempool_lookup("ovs_mp_1500_0_262144");
	//struct rte_mempool * packets_pool = rte_mempool_lookup("packets");

	//Create mempool
	//struct rte_mempool * packets_pool = rte_mempool_create(
	//	"packets",
	//	NUM_PKTS,
	//	MBUF_SIZE,
	//	CACHE_SIZE,					//This is the size of the mempool cache
	//	sizeof(struct rte_pktmbuf_pool_private),
	//	rte_pktmbuf_pool_init,
	//	NULL,
	//	rte_pktmbuf_init,
	//	NULL,
	//	rte_socket_id(),
	//	0 /*NO_FLAGS*/);


	if(packets_pool == NULL)
	{
		RTE_LOG(INFO, APP, "rte_errno: %s\n", rte_strerror(rte_errno));
		rte_exit(EXIT_FAILURE, "Cannot find memory pool\n");
	}

	RTE_LOG(INFO, APP, "There are %d free packets in the pool\n",
		rte_mempool_count(packets_pool));

#endif

#ifdef USE_BURST
	struct rte_mbuf * packets_array[BURST_SIZE] = {0};
	struct rte_mbuf * packets_array_rx[BURST_SIZE] = {0};
	int ntosend;
	int n;
	(void) n;

	/* prealloc packets */
	do
	{
		n = rte_mempool_get_bulk(packets_pool, (void **) packets_array, BURST_SIZE);
	} while(n != 0 && !stop);
	ntosend = BURST_SIZE;

#else
	struct rte_mbuf * mbuf;
	/* prealloc packet */
	do {
		mbuf = rte_pktmbuf_alloc(packets_pool);
	} while(mbuf == NULL);

#endif

	RTE_LOG(INFO, APP, "Starting sender loop\n");
	signal (SIGINT, crtl_c_handler);
	stop = 0;
	while(likely(!stop))
	{
		while(pause_);
#ifdef USE_BURST

	#if ALLOC_METHOD == ALLOC_OVS
		//Try to get BURS_SIZE free slots
		ntosend = rte_ring_dequeue_burst(alloc_q, (void **) packets_array, BURST_SIZE);
	#elif ALLOC_METHOD == ALLOC_APP
		//do
		//{
		//	n = rte_mempool_get_bulk(packets_pool, (void **) packets_array, BURST_SIZE);
		//} while(n != 0 && !stop);
		//ntosend = BURST_SIZE;
	#else
		#error "No implemented"
	#endif

		//Copy data to the buffers
		for(i = 0; i < ntosend; i++)
		{
			rte_memcpy(packets_array[i]->buf_addr, pkt, PKT_SIZE);
			//fill_packet(packets_array[i]->pkt.data);
			packets_array[i]->next = NULL;
			packets_array[i]->pkt_len = PKT_SIZE;
			packets_array[i]->data_len = PKT_SIZE;

		#ifdef CALC_CHECKSUM
			for(i = 0; i < ntosend; i++)
				for(kk = 0; kk < 8; kk++)
					checksum += ((uint64_t *)packets_array[i]->buf_addr)[kk];
		#endif
		}

		//Enqueue data (try until all the allocated packets are enqueue)
		i = 0;
		while(i < ntosend && !stop)
		{
			i += rte_ring_enqueue_burst(tx_ring, (void **) &packets_array[i], ntosend - i);

			/* also dequeue some packets */
			nreceived= rte_ring_dequeue_burst(rx_ring, (void **) packets_array_rx, BURST_SIZE);
			rx += nreceived; /* update statistics */
		}

#else	// [NO] USE_BURST
	#if ALLOC_METHOD  == ALLOC_OVS //Method 1
		//Read a buffer to be used as a buffer for a packet
		retval = rte_ring_dequeue(alloc_q, (void **)&mbuf);
		if(retval != 0)
		{
		#ifdef CALC_ALLOC_STATS
			//stats.alloc_fails++;
		#endif
			continue;
		}
	#elif ALLOC_METHOD  == ALLOC_APP //Method 2
		//mbuf = rte_pktmbuf_alloc(packets_pool);
		//if(mbuf == NULL)
		//{
		//#ifdef CALC_ALLOC_STATS
		//	stats.alloc_fails++;
		//#endif
		//	continue;
		//}
	#else
		#error "ALLOC_METHOD has a non valid value"
	#endif

	#if DELAY_CYCLES > 0
		//This loop increases mumber of packets per second (don't ask me why)
		unsigned long long j = 0;
		for(j = 0; j < DELAY_CYCLES; j++)
			asm("");
	#endif

		//Copy packet to the correct buffer
		rte_memcpy(mbuf->buf_addr, pkt, PKT_SIZE);
		//fill_packet(mbuf->pkt.data);
		//mbuf->pkt.next = NULL;
		//mbuf->pkt.pkt_len = PKT_SIZE;
		//mbuf->pkt.data_len = PKT_SIZE;
		(void) pkt;
		mbuf->next = NULL;
		mbuf->pkt_len = PKT_SIZE;
		mbuf->data_len = PKT_SIZE;

	#ifdef CALC_CHECKSUM
		for(kk = 0; kk < 8; kk++)
			checksum += ((uint64_t *)mbuf->buf_addr)[kk];
	#endif

		//this method avoids dropping packets:
		//Simple tries until the packet is inserted in the queue
		tryagain:
		retval = rte_ring_enqueue(tx_ring, (void *) mbuf);
		if(retval == -ENOBUFS && !stop)
		{
	#ifdef CALC_TX_TRIES
			//stats.tx_retries++;
	#endif
			goto tryagain;
		}

	#ifdef CALC_TX_STATS
		//stats.tx++;
	#endif

#endif //USE_BURST
	}

#ifdef CALC_CHECKSUM
	printf("Checksum was %" PRIu64 "\n", checksum);
#endif

}
int
check_socket_out_queue(void)
{
   Socket_Send_Msg *msg;
   uint32_t i;
   unsigned char message[1500];
   struct tcb *ptcb = NULL;
   struct tcb *temp = NULL;
   pthread_mutex_lock(&tcb_alloc_mutex);
   uint32_t TotalTcbs = Ntcb;
   pthread_mutex_unlock(&tcb_alloc_mutex);
    
   for(i=0; i<TotalTcbs; i++) {  // change it to hash type later
   //   if( i != 0)
      //printf("Checking tcb %d for sendingi %d %d\n", i, Ntcb, TotalTcbs);
      ptcb = tcbs[i];
      if(ptcb == NULL) {
         continue;
      }
      int num = rte_ring_dequeue(ptcb->tcb_socket_ring_recv, (void **)&msg);
      if(num < 0) {
         if(ptcb->need_ack_now) {
            logger(LOG_TCP, LOG_LEVEL_NORMAL, "sending immidiate ack for tcb %u\n", ptcb->identifier);
            ptcb->tcp_flags = TCP_FLAG_ACK;
            sendtcpdata(ptcb, NULL, 0);
            ptcb->need_ack_now = 0;
            // will use a saeparate api for ack later.
         }
         continue;
      }
      if(msg->m_Msg_Type == SOCKET_CLOSE) {
         temp = get_tcb_by_identifier(msg->m_Identifier);
         if(temp != ptcb) {
            printf ("Everything screewed at tcb'\n");
            exit(0);
      // put assert
         }
         ptcb->tcp_flags = TCP_FLAG_ACK | TCP_FLAG_FIN; // no problem in acking
         ptcb->need_ack_now = 1;
    //     sendfin(ptcb);
      }
      if(msg->m_Msg_Type == CONNECTION_OPEN) {
         temp = get_tcb_by_identifier(msg->m_Identifier);
         if(temp != ptcb) {
            printf ("Everything screewed at tcb'\n");
            exit(0);
      // put assert
         }
         sendsyn(ptcb);
         ptcb->state = SYN_SENT; 
      }
      if(msg->m_Msg_Type == SEND_DATA) {
         printf("****** Received %s and len %d and identifier %d\n",(char *)msg->m_Data, msg->m_Len, msg->m_Identifier);
         memcpy(message, msg->m_Data, msg->m_Len);
     
         temp = get_tcb_by_identifier(msg->m_Identifier);
         if(temp != ptcb) {
            printf ("Everything screewed at tcb'\n");
            exit(0);
      // put assert
         }
         ptcb->tcp_flags = TCP_FLAG_ACK; 
         sendtcpdata(ptcb, message, msg->m_Len);
      }
      rte_mempool_put(buffer_message_pool, msg);
   }
   return 0;
}