void arp_test_harness() { struct finsFrame fins_frame, *fins_ptr; int task; fins_ptr = &fins_frame; init_arp_intface(host_MAC_addrs, host_IP_addrs); task = 1; while (task!=0){ PRINT_DEBUG("\nReceive from network a request arp `1' or reply arp `2' or\n generate request to network `3', `0' to exit\n"); scanf("%d", &task); if ((task==1)||(task==2)) fins_from_net(fins_ptr, task); else if (task==3) fins_from_stub(fins_ptr); if (task==1 ||task==2 ||task==3) arp_in(fins_ptr); } term_arp_intface(); }
process netin ( int32 iface /* Interface for this process */ ) { struct ifentry *ifptr; /* ptr to interface table entry */ struct netpacket *pkt; /* ptr to current packet */ /* Do forever: read packets from the network and process */ ifptr = &if_tab[iface]; while(1) { /* Obtain next packet arriving on an interface */ wait(ifptr->if_sem); pkt = ifptr->if_queue[ifptr->if_head++]; if (ifptr->if_head >= IF_QUEUESIZE) { ifptr->if_head = 0; } /* Store interface number in packet buffer */ pkt->net_iface = iface; /* Convert Ethernet Type to host order */ eth_ntoh(pkt); /* Demultiplex on Ethernet type */ switch (pkt->net_ethtype) { case ETH_ARP: /* Handle ARP */ arp_in(iface,(struct arppacket *)pkt); continue; case ETH_IP: /* Handle IP */ ip_in(pkt); continue; case ETH_IPv6: /* Handle IPv6 */ freebuf((char *)pkt); continue; default: /* Ignore all other incoming packets */ freebuf((char *)pkt); continue; } } }
process netin () { struct netpacket *pkt; /* Ptr to current packet */ int32 retval; /* Return value from read */ /* Do forever: read a packet from the network and process */ while(1) { /* Allocate a buffer */ pkt = (struct netpacket *)getbuf(netbufpool); /* Obtain next packet that arrives */ retval = read(ETHER0, (char *)pkt, PACKLEN); if(retval == SYSERR) { panic("Cannot read from Ethernet\n"); } /* Convert Ethernet Type to host order */ eth_ntoh(pkt); /* Demultiplex on Ethernet type */ switch (pkt->net_ethtype) { case ETH_ARP: /* Handle ARP */ arp_in((struct arppacket *)pkt); continue; case ETH_IP: /* Handle IP */ ip_in(pkt); continue; case ETH_IPv6: /* Handle IPv6 */ freebuf((char *)pkt); continue; default: /* Ignore all other incoming packets */ freebuf((char *)pkt); continue; } } }
static void ether_in(struct lcore_env *env, struct rte_mbuf** bufs, unsigned n_rx, uint8_t src_port) { uint32_t j; for(j = 0; j < n_rx; j++) { int res = 0; int eth_type; struct rte_mbuf* pkt = bufs[j]; rte_prefetch0(rte_pktmbuf_mtod(pkt, void *)); struct ether_hdr *eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *); eth_type = ntohs(eth->ether_type); //RTE_LOG(INFO, MARIO, "%s\n\tl2_len: %d\n\tl3_len:%d\n\tl4_len:%d\n", // __func__, pkt->l2_len, pkt->l3_len, pkt->l4_len); pkt->l2_len = ETHER_HDR_LEN; printf("eth_hdr_type : %x\n", eth->ether_type); printf("eth_type_arp : %x\n", ETHER_TYPE_ARP); switch (eth_type) { case ETHER_TYPE_ARP : { printf("arp recved\n"); print_eth(eth); res = arp_in(pkt); break; } case ETHER_TYPE_IPv4 : { printf("ip recved\n"); res = ip_in(pkt, src_port); //return value : port break; } case ETHER_TYPE_IPv6 : { ; break; } } // no need to send a packet. e.g. after receiving an arp_reply if(res < -1) continue; printf("send_packet\n"); send_packet(env, pkt, (res == -1) ? src_port : res); } }
int ether_in(struct rte_mbuf *mbuf) { struct ether_hdr *eth; eth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *); switch(ntohs(eth->ether_type)) { case ETHER_TYPE_ARP : logger(ARP, NORMAL, "seen arp packet\n"); //printf("arp packet\n"); arp_in(mbuf); break; case ETHER_TYPE_IPv4 : logger(IP, NORMAL, "seen ip packet\n"); //printf("IP packet\n"); ip_in(mbuf); break; default : rte_pktmbuf_free(mbuf); } rte_pktmbuf_free(mbuf); // don't free here, future work. }
process netin(void) { status retval; /* return value from function */ recv_packets = 0; /* initialize */ netbufpool = mkbufpool(PACKLEN, UDP_SLOTS * UDP_QSIZ + ICMP_SLOTS * ICMP_QSIZ + ICMP_OQSIZ + 1); if (netbufpool == SYSERR) { kprintf("Cannot allocate network buffer pool"); kill(getpid()); } /* Copy Ethernet address to global variable */ control(ETHER0, ETH_CTRL_GET_MAC, (int32)NetData.ethaddr, 0); /* Indicate that IP address, mask, and router are not yet valid */ NetData.ipvalid = FALSE; NetData.ipaddr = 0; NetData.addrmask = 0; NetData.routeraddr = 0; /* Initialize ARP cache */ arp_init(); /* Initialize UDP table */ udp_init(); currpkt = (struct netpacket *)getbuf(netbufpool); /* Do forever: read packets from the network and process */ //kprintf("[netin]: start to read packets\r\n"); while(1) { retval = read(ETHER0, (char *)currpkt, PACKLEN); if (retval == SYSERR) { panic("Ethernet read error"); } /* Convert Ethernet Type to host order */ eth_ntoh(currpkt); /* Demultiplex on Ethernet type */ switch (currpkt->net_ethtype) { case ETH_ARP: arp_in(); /* Handle an ARP packet */ continue; case ETH_IP: /* Convert IP packet to host order */ if (ipcksum(currpkt) != 0) { kprintf("checksum failed\n\r"); continue; } if (currpkt->net_ipvh != 0x45) { kprintf("version failed\n\r"); continue; } /* Convert IP packet to host byte order */ ip_ntoh(currpkt); if ( (currpkt->net_ipdst != IP_BCAST) && (NetData.ipvalid) && (currpkt->net_ipdst != NetData.ipaddr) ) { continue; } /* Demultiplex UDP and ignore others */ if (currpkt->net_ipproto == IP_UDP) { //kprintf("[netin]: UDP# %d\r\n", ++recv_packets); udp_in();/* Handle a UDP packet */ } continue; default: /* Ignore all other Ethernet types */ kprintf("\n"); continue; } } }
process netin(void) { status retval; /* return value from function */ int i=0; struct ipv4_packet* ippkt = NULL; /* * FIXME : This might now work */ netbufpool = mkbufpool(PACKLEN, UDP_SLOTS * UDP_QSIZ + ICMP_SLOTS * ICMP_QSIZ + ICMP_OQSIZ + 1); if (netbufpool == SYSERR) { kprintf("Cannot allocate network buffer pool"); kill(getpid()); } /* Copy Ethernet address to global variable */ control(ETHER0, ETH_CTRL_GET_MAC, (int32)NetData.ethaddr, 0); /* Indicate that IP address, mask, and router are not yet valid */ NetData.ipvalid = FALSE; NetData.ipaddr = 0; NetData.addrmask = 0; NetData.routeraddr = 0; kprintf("My Mac is : %06x\r\n", *(NetData.ethaddr+4)); /* Initialize ARP cache */ arp_init(); /* Initialize UDP table */ udp_init(); currpkt = (struct eth_packet *)getbuf(netbufpool); /* Do forever: read packets from the network and process */ while(1) { retval = read(ETHER0, (char *)currpkt, PACKLEN); if (retval == SYSERR) { panic("Ethernet read error"); } /* Convert Ethernet Type to host order */ eth_ntoh(currpkt); /* Demultiplex on Ethernet type */ switch (currpkt->net_ethtype) { case ETH_ARP: arp_in(); /* Handle an ARP packet */ continue; case ETH_IP: //kprintf("Ip packet received \r\n"); ippkt = (struct ipv4_packet *)(currpkt->net_ethdata); if (ipcksum(ippkt) != 0) { kprintf("checksum failed\n\r"); continue; } if (ippkt->net_ipvh != 0x45) { kprintf("version failed\n\r"); continue; } /* Convert IP packet to host order */ ip_ntoh(ippkt); if ( (ippkt->net_ipdst != IP_BCAST) && (NetData.ipvalid) && (ippkt->net_ipdst != NetData.ipaddr) ) { continue; } /* Demultiplex UDP and ignore others */ if (ippkt->net_ipproto == IP_UDP) { udp_in();/* Handle a UDP packet */ }else if (ippkt->net_ipproto == IP_ICMP){ kprintf("ICMP Packet \r\n"); icmp_in();/* Handle ICMP packet */ } continue; case 0x1000: #ifdef DEBUG kprintf("Received a simulator packet\r\n"); #endif wait(rpl_sim_write_sem); memcpy((char *)&sim_queue[i], currpkt->net_ethdata, sizeof(struct rpl_sim_packet)); #ifdef DEBUG kprintf("Netin working with iter [%d] message type : %d dest : %04x source : %04x\r\n",i, sim_queue[i].msg_type, *((uint32 *)(sim_queue[i].dest_node)), *((uint32 *)(sim_queue[i].src_node))); #endif signal(rpl_sim_read_sem); i = (i+1)%RPL_SIM_RECV_QUEUE_LEN; continue; default: /* Ignore all other Ethernet types */ continue; } } }