diff options
Diffstat (limited to 'src')
37 files changed, 5992 insertions, 2248 deletions
diff --git a/src/bp_gtest.cpp b/src/bp_gtest.cpp index a94c2d37..005801c4 100755 --- a/src/bp_gtest.cpp +++ b/src/bp_gtest.cpp @@ -31,6 +31,7 @@ limitations under the License. #include "msg_manager.h" #include <common/cgen_map.h> #include "platform_cfg.h" +#include "latency.h" int test_policer(){ CPolicer policer; @@ -623,7 +624,6 @@ TEST_F(basic, test_pcap_mode1) { EXPECT_EQ_UINT32(1, res?1:0)<< "pass"; } - /* check override the low IPG */ TEST_F(basic, test_pcap_mode2) { @@ -638,154 +638,191 @@ TEST_F(basic, test_pcap_mode2) { EXPECT_EQ_UINT32(1, res?1:0)<< "pass"; } +#define l_pkt_test_s_ip 0x01020304 +#define l_pkt_test_d_ip 0x05060708 - - - -TEST_F(basic, latency1) { - CLatencyPktInfo l; - l.Create(); - - CParserOption * po =&CGlobalInfo::m_options; - po->preview.setVMode(3); - po->preview.setFileWrite(true); - - uint8_t mac[]={0,0,0,1,0,0}; - (void)mac; - - CErfIF erf_vif; - erf_vif.set_review_mode(&CGlobalInfo::m_options.preview); - - erf_vif.open_file("exp/sctp.erf"); - - - mac[0]=0; - mac[1]=0; - mac[2]=0; - mac[3]=1; - mac[4]=0; - mac[5]=0; - - - l.set_ip(0x10000000,0x20000000,0x01000000); - - int i; - /* simulate 8 ports */ - for (i=0;i<8; i++) { - rte_mbuf_t * m=l.generate_pkt(i); - erf_vif.send_node(l.getNode()); - rte_pktmbuf_free(m); - } - - erf_vif.close_file(); - - - l.Delete(); - - CErfCmp cmp; - cmp.dump=1; - - bool res=true; - - if ( cmp.compare("exp/sctp.erf","exp/sctp-ex.erf") != true ) { - res=false; +CLatencyPktMode * +latency_pkt_mod_create(uint8_t l_pkt_mode) { + switch (l_pkt_mode) { + default: + return new CLatencyPktModeSCTP(l_pkt_mode); + break; + case L_PKT_SUBMODE_NO_REPLY: + case L_PKT_SUBMODE_REPLY: + case L_PKT_SUBMODE_0_SEQ: + return new CLatencyPktModeICMP(l_pkt_mode); + break; } - EXPECT_EQ_UINT32((uint32_t)res?1:0, (uint32_t)1)<< "pass"; } - - -TEST_F(basic, latency2) { - CLatencyPktInfo l; +rte_mbuf_t * +create_latency_test_pkt(uint8_t l_pkt_mode, uint16_t &pkt_size, uint8_t port_id, uint8_t pkt_num) { + rte_mbuf_t * m; + CLatencyPktMode *c_l_pkt_mode; CCPortLatency port0; - l.Create(); - port0.Create(0,0,l.get_payload_offset(),l.get_pkt_size(),0); - - - uint8_t mac[]={0,0,0,1,0,0}; - (void)mac; - - mac[0]=0; - mac[1]=0; - mac[2]=0; - mac[3]=1; - mac[4]=0; - mac[5]=0; - - - l.set_ip(0x01000000,0x02000000,0x01000000); - - - int i; - for (i=0; i<100; i++) { - rte_mbuf_t * m=l.generate_pkt(0); - rte_pktmbuf_mtod(m, uint8_t*); - //utl_DumpBuffer(stdout,p,l.get_pkt_size(),0); - - port0.update_packet(m); + CLatencyPktInfo info; + CLatencyManager mgr; - rte_pktmbuf_mtod(m, uint8_t*); - //utl_DumpBuffer(stdout,p,l.get_pkt_size(),0); - //printf("offset is : %d \n",l.get_payload_offset()); + c_l_pkt_mode = latency_pkt_mod_create(l_pkt_mode); - CRx_check_header * rx_p; - bool res=port0.check_packet(m,rx_p); - EXPECT_EQ_UINT32((uint32_t)res?1:0, (uint32_t)1)<< "pass"; - if (!res ) { - printf(" ERROR \n"); + mgr.c_l_pkt_mode = c_l_pkt_mode; + info.Create(c_l_pkt_mode); + port0.Create(&mgr, 0, info.get_payload_offset(), info.get_l4_offset(), info.get_pkt_size(), 0); + info.set_ip(l_pkt_test_s_ip ,l_pkt_test_d_ip, 0x01000000); + m=info.generate_pkt(0); + while (pkt_num > 0) { + pkt_num--; + port0.update_packet(m, port_id); + } + pkt_size = info.get_pkt_size(); + port0.Delete(); + info.Delete(); + delete c_l_pkt_mode; + + return m; +} + +bool +verify_latency_pkt(uint8_t *p, uint8_t proto, uint16_t icmp_seq, uint8_t icmp_type) { + EthernetHeader *eth = (EthernetHeader *)p; + IPHeader *ip = (IPHeader *)(p + 14); + uint8_t srcmac[]={0,0,0,1,0,0}; + uint8_t dstmac[]={0,0,0,1,0,0}; + latency_header * h; + + // eth + EXPECT_EQ_UINT32(eth->getNextProtocol(), 0x0800)<< "Failed ethernet next protocol check"; + EXPECT_EQ_UINT32(memcmp(p, srcmac, 6), 0)<< "Failed ethernet source MAC check"; + EXPECT_EQ_UINT32(memcmp(p, dstmac, 6), 0)<< "Failed ethernet dest MAC check"; + // IP + EXPECT_EQ_UINT32(ip->getSourceIp(), l_pkt_test_s_ip)<< "Failed IP src check"; + EXPECT_EQ_UINT32(ip->getDestIp(), l_pkt_test_d_ip)<< "Failed IP dst check"; + EXPECT_EQ_UINT32(ip->getProtocol(), proto)<< "Failed IP protocol check"; + EXPECT_EQ_UINT32(ip->isChecksumOK()?0:1, 0)<< "Failed IP checksum check"; + EXPECT_EQ_UINT32(ip->getTimeToLive(), 0xff)<< "Failed IP ttl check"; + EXPECT_EQ_UINT32(ip->getTotalLength(), 48)<< "Failed IP total length check"; + + // payload + h=(latency_header *)(p+42); + EXPECT_EQ_UINT32(h->magic, LATENCY_MAGIC)<< "Failed latency magic check"; + + if (proto == 0x84) + return true; + // ICMP + ICMPHeader *icmp = (ICMPHeader *)(p + 34); + EXPECT_EQ_UINT32(icmp->isCheckSumOk(28)?0:1, 0)<< "Failed ICMP checksum verification"; + EXPECT_EQ_UINT32(icmp->getSeqNum(), icmp_seq)<< "Failed ICMP sequence number check"; + EXPECT_EQ_UINT32(icmp->getType(), icmp_type)<< "Failed ICMP type check"; + EXPECT_EQ_UINT32(icmp->getCode(), 0)<< "Failed ICMP code check"; + EXPECT_EQ_UINT32(icmp->getId(), 0xaabb)<< "Failed ICMP ID check"; + + return true; +} + +bool +test_latency_pkt_rcv(rte_mbuf_t *m, uint8_t l_pkt_mode, uint8_t port_num, uint16_t num_pkt, bool exp_pkt_ok + , uint16_t exp_tx_seq, uint16_t exp_rx_seq) { + CLatencyPktMode *c_l_pkt_mode; + CCPortLatency port; + CLatencyPktInfo info; + CLatencyManager mgr; + CRx_check_header *rxc; + CGlobalInfo::m_options.m_l_pkt_mode = l_pkt_mode; + + c_l_pkt_mode = latency_pkt_mod_create(l_pkt_mode); + mgr.c_l_pkt_mode = c_l_pkt_mode; + info.Create(c_l_pkt_mode); + port.Create(&mgr, 0, info.get_payload_offset(), info.get_l4_offset(), info.get_pkt_size(), 0); + bool pkt_ok = port.check_packet(m, rxc); + + while (num_pkt > 0) { + num_pkt--; + if (port.can_send_packet(port_num)) { + port.update_packet(m, 0); } - rte_pktmbuf_free(m); } - port0.DumpCounters(stdout); - EXPECT_EQ_UINT32(port0.m_pkt_ok, (uint32_t)100)<< "pass"; - port0.Delete(); - l.Delete(); + EXPECT_EQ_UINT32(pkt_ok ?0:1, exp_pkt_ok?0:1)<< "Failed packet OK check"; + EXPECT_EQ_UINT32(port.get_icmp_tx_seq(), exp_tx_seq)<< "Failed tx check"; + EXPECT_EQ_UINT32(port.get_icmp_rx_seq(), exp_rx_seq)<< "Failed rx check"; + // if packet is bad, check_packet raise the error counter + EXPECT_EQ_UINT32(port.m_unsup_prot, exp_pkt_ok?0:1)<< "Failed unsupported packets count"; + + return true; } -TEST_F(basic, latency3) { - CLatencyPktInfo l; - CCPortLatency port0; - l.Create(); - port0.Create(0,0,l.get_payload_offset(),l.get_pkt_size(),0); +// Testing latency packet generation +TEST_F(basic, latency1) { + uint8_t *p; + rte_mbuf_t * m; + uint16_t pkt_size; + // SCTP packet + m = create_latency_test_pkt(0, pkt_size, 0, 1); + p=rte_pktmbuf_mtod(m, uint8_t*); + verify_latency_pkt(p, 0x84, 0, 0); + rte_pktmbuf_free(m); - uint8_t mac[]={0,0,0,1,0,0}; - (void)mac; + m = create_latency_test_pkt(L_PKT_SUBMODE_NO_REPLY, pkt_size, 1, 2); + p=rte_pktmbuf_mtod(m, uint8_t*); + verify_latency_pkt(p, 0x01, 2, 8); + rte_pktmbuf_free(m); + // ICMP reply mode client side + m = create_latency_test_pkt(L_PKT_SUBMODE_REPLY, pkt_size, 0, 3); + p = rte_pktmbuf_mtod(m, uint8_t*); + verify_latency_pkt(p, 0x01, 3, 8); + rte_pktmbuf_free(m); - mac[0]=0; - mac[1]=0; - mac[2]=0; - mac[3]=1; - mac[4]=0; - mac[5]=0; + // ICMP reply mode server side + m = create_latency_test_pkt(L_PKT_SUBMODE_REPLY, pkt_size, 1, 4); + p=rte_pktmbuf_mtod(m, uint8_t*); + verify_latency_pkt(p, 0x01, 4, 0); + rte_pktmbuf_free(m); + m = create_latency_test_pkt(L_PKT_SUBMODE_0_SEQ, pkt_size, 1, 5); + p=rte_pktmbuf_mtod(m, uint8_t*); + verify_latency_pkt(p, 0x01, 0, 8); + rte_pktmbuf_free(m); +} - l.set_ip(0x01000000,0x02000000,0x01000000); +// Testing latency packet receive path +TEST_F(basic, latency2) { + rte_mbuf_t *m; + uint16_t pkt_size; + uint8_t port_id = 0; + uint8_t pkt_num = 1; + uint8_t l_pkt_mode; - uint8_t *p; - rte_mbuf_t * m=l.generate_pkt(0); - port0.update_packet(m); - p=rte_pktmbuf_mtod(m, uint8_t*); - memset(p,0,l.get_pkt_size()); + l_pkt_mode = 0; + m = create_latency_test_pkt(l_pkt_mode, pkt_size, port_id, pkt_num); + test_latency_pkt_rcv(m, l_pkt_mode, 0, 2, true, 1, 0); - CRx_check_header * rx_p; - bool res=port0.check_packet(m,rx_p); - EXPECT_EQ_UINT32((uint32_t)res?0:1, (uint32_t)1)<< "pass"; - if (!res ) { - printf(" OK \n"); - } - rte_pktmbuf_free(m); - EXPECT_EQ_UINT32(port0.m_unsup_prot, (uint32_t)1)<< "pass"; + l_pkt_mode = L_PKT_SUBMODE_NO_REPLY; + m = create_latency_test_pkt(l_pkt_mode, pkt_size, port_id, pkt_num); + test_latency_pkt_rcv(m, l_pkt_mode, 1, 2, true, 3, 1); - port0.Delete(); - l.Delete(); -} + // reply mode server + l_pkt_mode = L_PKT_SUBMODE_REPLY; + m = create_latency_test_pkt(l_pkt_mode, pkt_size, port_id, pkt_num); + test_latency_pkt_rcv(m, l_pkt_mode, 1, 2, true, 2, 1); + // reply mode client + l_pkt_mode = L_PKT_SUBMODE_REPLY; + m = create_latency_test_pkt(l_pkt_mode, pkt_size, port_id, pkt_num); + test_latency_pkt_rcv(m, l_pkt_mode, 0, 2, true, 3, 1); + l_pkt_mode = L_PKT_SUBMODE_0_SEQ; + m = create_latency_test_pkt(l_pkt_mode, pkt_size, port_id, pkt_num); + test_latency_pkt_rcv(m, l_pkt_mode, 0, 2, true, 0, 0); + // bad packet + m = create_latency_test_pkt(l_pkt_mode, pkt_size, port_id, pkt_num); + EthernetHeader *eth = (EthernetHeader *)rte_pktmbuf_mtod(m, uint8_t*); + eth->setNextProtocol(0x5); + test_latency_pkt_rcv(m, l_pkt_mode, 0, 2, false, 1, 0); +} class CDummyLatencyHWBase : public CPortLatencyHWBase { public: @@ -835,21 +872,8 @@ public: uint8_t m_port_id; }; - - -TEST_F(basic, latency4) { - - uint8_t mac[]={0,0,0,1,0,0}; - (void)mac; - - mac[0]=0; - mac[1]=0; - mac[2]=0; - mac[3]=1; - mac[4]=0; - mac[5]=0; - - +// Testing latency statistics functions +TEST_F(basic, latency3) { CLatencyManager mg; CLatencyManagerCfg cfg; CDummyLatencyHWBase dports[MAX_LATENCY_PORTS]; @@ -882,7 +906,6 @@ TEST_F(basic, latency4) { mg.Delete(); } - TEST_F(basic, hist1) { CTimeHistogram hist1; @@ -902,8 +925,6 @@ TEST_F(basic, hist1) { hist1.Delete(); } - - TEST_F(basic, rtsp1) { CTestBasic t1; @@ -2020,7 +2041,7 @@ public: virtual int send_node(CGenNode * node); - virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, rte_mbuf_t *m){ + virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, uint8_t * p){ return (0); } diff --git a/src/bp_sim.cpp b/src/bp_sim.cpp index a61fbb8f..081553b5 100755 --- a/src/bp_sim.cpp +++ b/src/bp_sim.cpp @@ -20,6 +20,7 @@ limitations under the License. */ #include "bp_sim.h" +#include "latency.h" #include "utl_json.h" #include "utl_yaml.h" #include "msg_manager.h" @@ -1311,7 +1312,6 @@ bool CPacketIndication::ConvertPacketToIpv6InPlace(CCapPktRaw * pkt, return (true); } - void CPacketIndication::ProcessPacket(CPacketParser *parser, CCapPktRaw * pkt){ _ProcessPacket(parser,pkt); @@ -1320,8 +1320,6 @@ void CPacketIndication::ProcessPacket(CPacketParser *parser, } } - - /* process packet */ void CPacketIndication::_ProcessPacket(CPacketParser *parser, CCapPktRaw * pkt){ @@ -4657,13 +4655,24 @@ int CErfIFStl::send_node(CGenNode * _no_to_use){ if ( m_preview_mode->getFileWrite() ){ CGenNodeStateless * node_sl=(CGenNodeStateless *) _no_to_use; - + + pkt_dir_t dir=(pkt_dir_t)node_sl->get_mbuf_cache_dir(); + /* check that we have mbuf */ rte_mbuf_t * m=node_sl->get_cache_mbuf(); - assert( m ); - pkt_dir_t dir=(pkt_dir_t)node_sl->get_mbuf_cache_dir(); + if (m) { + /* cache packet */ + fill_raw_packet(m,_no_to_use,dir); + /* can't free the m, it is cached*/ + }else{ + + m=node_sl->alloc_node_with_vm(); + assert(m); + fill_raw_packet(m,_no_to_use,dir); + rte_pktmbuf_free(m); + + } - fill_raw_packet(m,_no_to_use,dir); BP_ASSERT(m_writer); bool res=m_writer->write_packet(m_raw); @@ -4719,900 +4728,10 @@ int CErfIF::send_node(CGenNode * node){ return (0); } - int CErfIF::flush_tx_queue(void){ return (0); } - - -const uint8_t sctp_pkt[]={ - - 0x00,0x04,0x96,0x08,0xe0,0x40, - 0x00,0x0e,0x2e,0x24,0x37,0x5f, - 0x08,0x00, - - 0x45,0x02,0x00,0x30, - 0x00,0x00,0x40,0x00, - 0x40,0x84,0xbd,0x04, - 0x9b,0xe6,0x18,0x9b, //sIP - 0xcb,0xff,0xfc,0xc2, //DIP - - 0x80,0x44,//SPORT - 0x00,0x50,//DPORT - - 0x00,0x00,0x00,0x00, //checksum - - 0x11,0x22,0x33,0x44, // magic - 0x00,0x00,0x00,0x00, //64 bit counter - 0x00,0x00,0x00,0x00, - 0x00,0x01,0xa0,0x00, //seq - 0x00,0x00,0x00,0x00, - -}; - -// 20+8+20` - -void CLatencyPktInfo::Create(){ - m_packet = new CCapPktRaw( sizeof(sctp_pkt) ); - m_packet->pkt_cnt=0; - m_packet->time_sec=0; - m_packet->time_nsec=0; - memcpy(m_packet->raw,sctp_pkt,sizeof(sctp_pkt)); - m_packet->pkt_len=sizeof(sctp_pkt); - - m_pkt_indication.m_packet =m_packet; - - m_pkt_indication.m_ether = (EthernetHeader *)m_packet->raw; - m_pkt_indication.l3.m_ipv4=(IPHeader *)(m_packet->raw+14); - m_pkt_indication.m_is_ipv6 = false; - m_pkt_indication.l4.m_udp=(UDPHeader *)m_packet->raw+14+20; - m_pkt_indication.m_payload=(uint8_t *)m_packet->raw+14+20+16; - m_pkt_indication.m_payload_len=0; - m_pkt_indication.m_packet_padding=4; - - - m_pkt_indication.m_ether_offset =0; - m_pkt_indication.m_ip_offset =14; - m_pkt_indication.m_udp_tcp_offset = 34; - m_pkt_indication.m_payload_offset = 34+8; - - CPacketDescriptor * lpd=&m_pkt_indication.m_desc; - lpd->Clear(); - lpd->SetInitSide(true); - lpd->SetSwapTuple(false); - lpd->SetIsValidPkt(true); - lpd->SetIsUdp(true); - lpd->SetIsLastPkt(true); - m_pkt_info.Create(&m_pkt_indication); - - memset(&m_dummy_node,0,sizeof(m_dummy_node)); - - m_dummy_node.set_socket_id( CGlobalInfo::m_socket.port_to_socket(0) ); - - m_dummy_node.m_time =0.1; - m_dummy_node.m_pkt_info = &m_pkt_info; - m_dummy_node.m_dest_ip = 0; - m_dummy_node.m_src_ip = 0; - m_dummy_node.m_src_port = 0x11; - m_dummy_node.m_flow_id =0; - m_dummy_node.m_flags =CGenNode::NODE_FLAGS_LATENCY; - -} - - -rte_mbuf_t * CLatencyPktInfo::generate_pkt(int port_id,uint32_t extern_ip){ - - bool is_client_to_serever=(port_id%2==0)?true:false; - - int dual_port_index=(port_id>>1); - uint32_t c=m_client_ip.v4; - uint32_t s=m_server_ip.v4; - if ( extern_ip ){ - c=extern_ip; - } - - if (!is_client_to_serever) { - /*swap */ - uint32_t t=c; - c=s; - s=t; - } - uint32_t mask=dual_port_index*m_dual_port_mask; - if ( extern_ip==0 ){ - c+=mask; - } - s+=mask; - m_dummy_node.m_src_ip = c; - m_dummy_node.m_dest_ip = s; - - rte_mbuf_t * m=m_pkt_info.generate_new_mbuf(&m_dummy_node); - return (m); - - -} - - -void CLatencyPktInfo::set_ip(uint32_t src, - uint32_t dst, - uint32_t dual_port_mask){ - - m_client_ip.v4=src; - m_server_ip.v4=dst; - m_dual_port_mask=dual_port_mask; - -} - - -void CLatencyPktInfo::Delete(){ - m_pkt_info.Delete(); - delete m_packet; -} - -void CCPortLatency::reset(){ - m_rx_seq =m_tx_seq; - m_pad = 0; - - m_tx_pkt_err=0; - m_tx_pkt_ok =0; - m_pkt_ok=0; - m_rx_check=0; - m_no_magic=0; - m_unsup_prot=0; - m_no_id=0; - m_seq_error=0; - m_length_error=0; - m_no_ipv4_option=0; - m_hist.Reset(); -} - - -static uint8_t nat_is_port_can_send(uint8_t port_id){ - uint8_t client_index = (port_id %2); - return (client_index ==0 ?1:0); -} - - -bool CCPortLatency::Create(CLatencyManager * parent, - uint8_t id, - uint16_t offset, - uint16_t pkt_size, - CCPortLatency * rx_port){ - m_parent = parent; - m_id = id; - m_tx_seq =0x12345678; - m_offset = offset; - m_pkt_size = pkt_size; - m_rx_port = rx_port; - m_nat_can_send = nat_is_port_can_send(m_id); - m_nat_learn = m_nat_can_send; - m_nat_external_ip=0; - - m_hist.Create(); - reset(); - return (true); -} - -void CCPortLatency::Delete(){ - m_hist.Delete(); -} - -void CCPortLatency::update_packet(rte_mbuf_t * m){ - uint8_t *p=rte_pktmbuf_mtod(m, uint8_t*); - /* update mac addr dest/src 12 bytes */ - memcpy(p,CGlobalInfo::m_options.get_dst_src_mac_addr(m_id),12); - - latency_header * h=(latency_header *)(p+m_offset); - h->magic = LATENCY_MAGIC | m_id ; - h->time_stamp = os_get_hr_tick_64(); - h->seq = m_tx_seq; - m_tx_seq++; -} - - -void CCPortLatency::DumpShortHeader(FILE *fd){ - - - fprintf(fd," if| tx_ok , rx_ok , rx ,error, average , max , Jitter , max window \n"); - fprintf(fd," | , , check, , latency(usec),latency (usec) ,(usec) , \n"); - fprintf(fd," ---------------------------------------------------------------------------------------------------------------- \n"); -} - - - -std::string CCPortLatency::get_field(std::string name,float f){ - char buff[200]; - sprintf(buff,"\"%s-%d\":%.1f,",name.c_str(),m_id,f); - return (std::string(buff)); -} - - -void CCPortLatency::dump_json_v2(std::string & json ){ - char buff[200]; - sprintf(buff,"\"port-%d\": {",m_id); - json+=std::string(buff); - m_hist.dump_json("hist",json); - dump_counters_json(json); - json+="},"; -} - -void CCPortLatency::dump_json(std::string & json ){ - json += get_field("avg",m_hist.get_average_latency() ); - json += get_field("max",m_hist.get_max_latency() ); - json += get_field("c-max",m_hist.get_max_latency_last_update() ); - json += get_field("error",(float)(m_unsup_prot+m_no_magic+m_no_id+m_seq_error+m_length_error) ); - json += get_field("jitter",(float)get_jitter_usec() ); -} - - -void CCPortLatency::DumpShort(FILE *fd){ - - m_hist.update(); - fprintf(fd,"%8lu,%8lu,%10lu,%4lu,", - m_tx_pkt_ok, - m_pkt_ok, - m_rx_check, - m_unsup_prot+m_no_magic+m_no_id+m_seq_error+m_length_error+m_no_ipv4_option+m_tx_pkt_err - ); - - fprintf(fd," %8.0f ,%8.0f,%8d ", - m_hist.get_average_latency(), - m_hist.get_max_latency(), - get_jitter_usec() - ); - fprintf(fd," | "); - m_hist.DumpWinMax(fd); - -} - -#define DPL_J(f) json+=add_json(#f,f); -#define DPL_J_LAST(f) json+=add_json(#f,f,true); - -void CCPortLatency::dump_counters_json(std::string & json ){ - - json+="\"stats\" : {"; - DPL_J(m_tx_pkt_ok); - DPL_J(m_tx_pkt_err); - DPL_J(m_pkt_ok); - DPL_J(m_unsup_prot); - DPL_J(m_no_magic); - DPL_J(m_no_id); - DPL_J(m_seq_error); - DPL_J(m_length_error); - DPL_J(m_no_ipv4_option); - json+=add_json("m_jitter",get_jitter_usec()); - /* must be last */ - DPL_J_LAST(m_rx_check); - json+="}"; - - -} - -void CCPortLatency::DumpCounters(FILE *fd){ - #define DP_A1(f) if (f) fprintf(fd," %-40s : %llu \n",#f, (unsigned long long)f) - - fprintf(fd," counter \n"); - fprintf(fd," -----------\n"); - - DP_A1(m_tx_pkt_err); - DP_A1(m_tx_pkt_ok); - DP_A1(m_pkt_ok); - DP_A1(m_unsup_prot); - DP_A1(m_no_magic); - DP_A1(m_no_id); - DP_A1(m_seq_error); - DP_A1(m_length_error); - DP_A1(m_rx_check); - DP_A1(m_no_ipv4_option); - - - fprintf(fd," -----------\n"); - m_hist.Dump(fd); - fprintf(fd," %-40s : %lu \n","jitter", (ulong)get_jitter_usec()); -} - -bool CCPortLatency::dump_packet(rte_mbuf_t * m){ - fprintf(stdout," %f.03 dump packet ..\n",now_sec()); - uint8_t *p=rte_pktmbuf_mtod(m, uint8_t*); - uint16_t pkt_size=rte_pktmbuf_pkt_len(m); - utl_DumpBuffer(stdout,p,pkt_size,0); - return (0); - - - - if (pkt_size < ( sizeof(CRx_check_header)+14+20) ) { - assert(0); - } - CRx_check_header * lp=(CRx_check_header *)(p+pkt_size-sizeof(CRx_check_header)); - - lp->dump(stdout); - - - uint16_t vlan_offset=0; - if ( unlikely( CGlobalInfo::m_options.preview.get_vlan_mode_enable() ) ){ - vlan_offset=4; - } - - (void)vlan_offset; - -// utl_DumpBuffer(stdout,p,pkt_size,0); - return (0); - -} - -bool CCPortLatency::check_rx_check(rte_mbuf_t * m){ - m_rx_check++; - return (true); -} - -bool CCPortLatency::do_learn(uint32_t external_ip){ - m_nat_learn=true; - m_nat_can_send=true; - m_nat_external_ip=external_ip; - return (true); -} - -bool CCPortLatency::check_packet(rte_mbuf_t * m,CRx_check_header * & rx_p){ - - CSimplePacketParser parser(m); - if ( !parser.Parse() ){ - m_unsup_prot++; // Unsupported protocol - return (false); - } - - uint16_t pkt_size=rte_pktmbuf_pkt_len(m); - /* check if CRC was extracted */ - if ( parser.getPktSize() == pkt_size-4) { - // CRC was not extracted by driver (VM E1000 driver issue) extract it - pkt_size=pkt_size-4; - } - - uint16_t vlan_offset=parser.m_vlan_offset; - uint8_t *p=rte_pktmbuf_mtod(m, uint8_t*); - - rx_p = (CRx_check_header *)0; - - if ( !parser.IsLatencyPkt() ){ - - #ifdef NAT_TRACE_ - printf(" %.3f RX : got packet !!! \n",now_sec() ); - #endif - - /* ipv6+rx-check */ - if ( parser.m_ipv6 ) { - /* if we have ipv6 packet */ - if (parser.m_protocol == RX_CHECK_V6_OPT_TYPE) { - if ( get_is_rx_check_mode() ){ - m_rx_check++; - rx_p=(CRx_check_header *)((uint8_t*)parser.m_ipv6 +IPv6Header::DefaultSize); - return (true); - } - - } - m_seq_error++; - return (false); - } - - uint8_t opt_len = parser.m_ipv4->getOptionLen(); - uint8_t *opt_ptr = parser.m_ipv4->getOption(); - /* Process IP option header(s) */ - while ( opt_len != 0 ) { - switch (*opt_ptr) { - case RX_CHECK_V4_OPT_TYPE: - /* rx-check option header */ - if ( ( !get_is_rx_check_mode() ) || - (opt_len < RX_CHECK_LEN) ) { - m_seq_error++; - return (false); - } - m_rx_check++; - rx_p=(CRx_check_header *)opt_ptr; - opt_len -= RX_CHECK_LEN; - opt_ptr += RX_CHECK_LEN; - break; - case CNatOption::noIPV4_OPTION: - /* NAT learn option header */ - CNatOption *lp; - if ( ( !CGlobalInfo::is_learn_mode() ) || - (opt_len < CNatOption::noOPTION_LEN) ) { - m_seq_error++; - return (false); - } - lp = (CNatOption *)opt_ptr; - if ( !lp->is_valid_ipv4_magic() ) { - m_no_ipv4_option++; - return (false); - } - m_parent->get_nat_manager()->handle_packet_ipv4(lp,parser.m_ipv4); - opt_len -= CNatOption::noOPTION_LEN; - opt_ptr += CNatOption::noOPTION_LEN; - break; - default: - m_seq_error++; - return (false); - } // End of switch - } // End of while - - return (true); - } // End of check for non-latency packet - - if ( CGlobalInfo::is_learn_mode() && (m_nat_learn ==false) ) { - do_learn(parser.m_ipv4->getSourceIp()); - } - - if ( (pkt_size-vlan_offset) != m_pkt_size ) { - m_length_error++; - return (false); - } - - latency_header * h=(latency_header *)(p+m_offset+vlan_offset); - - if ( (h->magic & 0xffffff00) != LATENCY_MAGIC ){ - m_no_magic++; - return (false); - } - - if ( h->seq != m_rx_seq ){ - m_seq_error++; - m_rx_seq =h->seq +1; - return (false); - }else{ - m_rx_seq++; - } - m_pkt_ok++; - uint64_t d = (os_get_hr_tick_64() - h->time_stamp ); - dsec_t ctime=ptime_convert_hr_dsec(d); - m_hist.Add(ctime); - m_jitter.calc(ctime); - return (true); -} - -void CLatencyManager::Delete(){ - m_pkt_gen.Delete(); - - if ( get_is_rx_check_mode() ) { - m_rx_check_manager.Delete(); - } - if ( CGlobalInfo::is_learn_mode() ){ - m_nat_check_manager.Delete(); - } - m_cpu_cp_u.Delete(); -} - -/* 0->1 - 1->0 - 2->3 - 3->2 -*/ -static uint8_t swap_port(uint8_t port_id){ - uint8_t offset= ((port_id>>1)<<1); - uint8_t client_index = (port_id %2); - return (offset + (client_index ^ 1)); -} - - - -bool CLatencyManager::Create(CLatencyManagerCfg * cfg){ - m_max_ports=cfg->m_max_ports; - assert (m_max_ports<=MAX_LATENCY_PORTS); - assert ((m_max_ports%2)==0); - m_port_mask =0xffffffff; - m_do_stop =false; - m_is_active =false; - m_pkt_gen.Create(); - int i; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - CCPortLatency * lpo=&m_ports[swap_port(i)].m_port; - - lp->m_io=cfg->m_ports[i]; - lp->m_port.Create(this, - i, - m_pkt_gen.get_payload_offset(), - m_pkt_gen.get_pkt_size(),lpo ); - } - m_cps= cfg->m_cps; - m_d_time =ptime_convert_dsec_hr((1.0/m_cps)); - m_delta_sec =(1.0/m_cps); - - - if ( get_is_rx_check_mode() ) { - assert(m_rx_check_manager.Create()); - m_rx_check_manager.m_cur_time= now_sec(); - } - - - m_pkt_gen.set_ip(cfg->m_client_ip.v4,cfg->m_server_ip.v4,cfg->m_dual_port_mask); - m_cpu_cp_u.Create(&m_cpu_dp_u); - if ( CGlobalInfo::is_learn_mode() ){ - m_nat_check_manager.Create(); - } - return (true); -} - - -void CLatencyManager::send_pkt_all_ports(){ - m_start_time = os_get_hr_tick_64(); - int i; - for (i=0; i<m_max_ports; i++) { - if ( m_port_mask & (1<<i) ){ - CLatencyManagerPerPort * lp=&m_ports[i]; - if (lp->m_port.can_send_packet() ){ - rte_mbuf_t * m=m_pkt_gen.generate_pkt(i,lp->m_port.external_nat_ip()); - lp->m_port.update_packet(m); - if ( lp->m_io->tx(m) == 0 ){ - lp->m_port.m_tx_pkt_ok++; - }else{ - lp->m_port.m_tx_pkt_err++; - } - - } - } - } -} - - -void CLatencyManager::wait_for_rx_dump(){ - rte_mbuf_t * rx_pkts[64]; - int i; - while ( true ) { - rte_pause(); - rte_pause(); - rte_pause(); - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - rte_mbuf_t * m; - uint16_t cnt_p = lp->m_io->rx_burst(rx_pkts, 64); - if (cnt_p) { - int j; - for (j=0; j<cnt_p; j++) { - m=rx_pkts[j] ; - lp->m_port.dump_packet( m); - rte_pktmbuf_free(m); - } - } /*cnt_p*/ - }/* for*/ - } -} - - -void CLatencyManager::handle_rx_pkt(CLatencyManagerPerPort * lp, - rte_mbuf_t * m){ - CRx_check_header *rxc; - lp->m_port.check_packet(m,rxc); - if ( unlikely(rxc!=NULL) ){ - m_rx_check_manager.handle_packet(rxc); - } - rte_pktmbuf_free(m); -} - -void CLatencyManager::handle_latecy_pkt_msg(uint8_t thread_id, - CGenNodeLatencyPktInfo * msg){ - - assert(msg->m_latency_offset==0xdead); - - uint8_t rx_port_index=(thread_id<<1)+(msg->m_dir&1); - assert( rx_port_index <m_max_ports ) ; - CLatencyManagerPerPort * lp=&m_ports[rx_port_index]; - handle_rx_pkt(lp,(rte_mbuf_t *)msg->m_pkt); -} - - -void CLatencyManager::run_rx_queue_msgs(uint8_t thread_id, - CNodeRing * r){ - - while ( true ) { - CGenNode * node; - if ( r->Dequeue(node)!=0 ){ - break; - } - assert(node); - - CGenNodeMsgBase * msg=(CGenNodeMsgBase *)node; - - uint8_t msg_type = msg->m_msg_type; - switch (msg_type ) { - case CGenNodeMsgBase::LATENCY_PKT: - handle_latecy_pkt_msg(thread_id,(CGenNodeLatencyPktInfo *) msg); - break; - default: - printf("ERROR latency-thread message type is not valid %d \n",msg_type); - assert(0); - } - - CGlobalInfo::free_node(node); - } -} - -void CLatencyManager::try_rx_queues(){ - - CMessagingManager * rx_dp = CMsgIns::Ins()->getRxDp(); - uint8_t threads=CMsgIns::Ins()->get_num_threads(); - int ti; - for (ti=0; ti<(int)threads; ti++) { - CNodeRing * r = rx_dp->getRingDpToCp(ti); - if ( !r->isEmpty() ){ - run_rx_queue_msgs((uint8_t)ti,r); - } - } -} - - -void CLatencyManager::try_rx(){ - rte_mbuf_t * rx_pkts[64]; - int i; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - rte_mbuf_t * m; - m_cpu_dp_u.start_work(); - /* try to read 64 packets clean up the queue */ - uint16_t cnt_p = lp->m_io->rx_burst(rx_pkts, 64); - if (cnt_p) { - int j; - for (j=0; j<cnt_p; j++) { - m=rx_pkts[j] ; - handle_rx_pkt(lp,m); - } - /* commit only if there was work to do ! */ - m_cpu_dp_u.commit(); - }/* if work */ - }// all ports -} - - -void CLatencyManager::reset(){ - - int i; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - lp->m_port.reset(); - } - -} - -void CLatencyManager::start(int iter){ - m_do_stop =false; - m_is_active =false; - int cnt=0; - - double n_time; - CGenNode * node = new CGenNode(); - node->m_type = CGenNode::FLOW_SYNC; /* general stuff */ - node->m_time = now_sec()+0.007; - m_p_queue.push(node); - - node = new CGenNode(); - node->m_type = CGenNode::FLOW_PKT; /* latency */ - node->m_time = now_sec(); /* 1/cps rate */ - m_p_queue.push(node); - bool do_try_rx_queue =CGlobalInfo::m_options.preview.get_vm_one_queue_enable()?true:false; - - - while ( !m_p_queue.empty() ) { - node = m_p_queue.top(); - n_time = node->m_time; - - /* wait for event */ - while ( true ) { - double dt = now_sec() - n_time ; - if (dt> (0.0)) { - break; - } - if (do_try_rx_queue){ - try_rx_queues(); - } - try_rx(); - rte_pause(); - } - - switch (node->m_type) { - case CGenNode::FLOW_SYNC: - if ( CGlobalInfo::is_learn_mode() ) { - m_nat_check_manager.handle_aging(); - } - - m_p_queue.pop(); - node->m_time += SYNC_TIME_OUT; - m_p_queue.push(node); - - break; - case CGenNode::FLOW_PKT: - m_cpu_dp_u.start_work(); - send_pkt_all_ports(); - m_p_queue.pop(); - node->m_time += m_delta_sec; - m_p_queue.push(node); - m_cpu_dp_u.commit(); - break; - } - - /* this will be called every sync which is 1msec */ - if ( m_do_stop ) { - break; - } - if ( iter>0 ){ - if ( ( cnt>iter) ){ - printf("stop due iter %d\n",iter); - break; - } - } - cnt++; - } - - /* free all nodes in the queue */ - while (!m_p_queue.empty()) { - node = m_p_queue.top(); - m_p_queue.pop(); - delete node; - } - - printf(" latency daemon has stopped\n"); - if ( get_is_rx_check_mode() ) { - m_rx_check_manager.tw_drain(); - } - -} - -void CLatencyManager::stop(){ - m_do_stop =true; -} - -bool CLatencyManager::is_active(){ - return (m_is_active); -} - - -double CLatencyManager::get_max_latency(){ - double l=0.0; - int i; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - if ( l <lp->m_port.m_hist.get_max_latency() ){ - l=lp->m_port.m_hist.get_max_latency(); - } - } - return (l); -} - -double CLatencyManager::get_avr_latency(){ - double l=0.0; - int i; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - if ( l <lp->m_port.m_hist.get_average_latency() ){ - l=lp->m_port.m_hist.get_average_latency(); - } - } - return (l); -} - -uint64_t CLatencyManager::get_total_pkt(){ - int i; - uint64_t t=0; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - t+=lp->m_port.m_tx_pkt_ok ; - } - return t; -} - -uint64_t CLatencyManager::get_total_bytes(){ - int i; - uint64_t t=0; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - t+=lp->m_port.m_tx_pkt_ok* (m_pkt_gen.get_pkt_size()+4); - } - return t; - -} - - -bool CLatencyManager::is_any_error(){ - int i; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - if ( lp->m_port.is_any_err() ){ - return (true); - } - } - return (false); -} - - -void CLatencyManager::dump_json(std::string & json ){ - json="{\"name\":\"trex-latecny\",\"type\":0,\"data\":{"; - int i; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - lp->m_port.dump_json(json); - } - - json+="\"unknown\":0}}" ; - -} - -void CLatencyManager::dump_json_v2(std::string & json ){ - json="{\"name\":\"trex-latecny-v2\",\"type\":0,\"data\":{"; - json+=add_json("cpu_util",m_cpu_cp_u.GetVal()); - - int i; - for (i=0; i<m_max_ports; i++) { - CLatencyManagerPerPort * lp=&m_ports[i]; - lp->m_port.dump_json_v2(json); - } - - json+="\"unknown\":0}}" ; - -} - -void CLatencyManager::DumpRxCheck(FILE *fd){ - if ( get_is_rx_check_mode() ) { - fprintf(fd," rx checker : \n"); - m_rx_check_manager.DumpShort(fd); - m_rx_check_manager.Dump(fd); - } -} - -void CLatencyManager::DumpShortRxCheck(FILE *fd){ - if ( get_is_rx_check_mode() ) { - m_rx_check_manager.DumpShort(fd); - } -} - -void CLatencyManager::rx_check_dump_json(std::string & json){ - if ( get_is_rx_check_mode() ) { - m_rx_check_manager.dump_json(json ); - } -} - -void CLatencyManager::update(){ - m_cpu_cp_u.Update() ; -} - -void CLatencyManager::DumpShort(FILE *fd){ - int i; - fprintf(fd," Cpu Utilization : %2.1f %% \n",m_cpu_cp_u.GetVal()); - CCPortLatency::DumpShortHeader(fd); - for (i=0; i<m_max_ports; i++) { - fprintf(fd," %d | ",i); - CLatencyManagerPerPort * lp=&m_ports[i]; - lp->m_port.DumpShort(fd); - fprintf(fd,"\n"); - } - - -} - -void CLatencyManager::Dump(FILE *fd){ - int i; - fprintf(fd," cpu : %2.1f %% \n",m_cpu_cp_u.GetVal()); - for (i=0; i<m_max_ports; i++) { - fprintf(fd," port %d \n",i); - fprintf(fd," -----------------\n"); - CLatencyManagerPerPort * lp=&m_ports[i]; - lp->m_port.DumpCounters(fd); - } -} - -void CLatencyManager::DumpRxCheckVerification(FILE *fd, - uint64_t total_tx_rx_check){ - if ( !get_is_rx_check_mode() ) { - fprintf(fd," rx_checker is disabled \n"); - return; - } - fprintf(fd," rx_check Tx : %llu \n", (unsigned long long)total_tx_rx_check); - fprintf(fd," rx_check Rx : %llu \n", (unsigned long long)m_rx_check_manager.getTotalRx() ); - fprintf(fd," rx_check verification :" ); - if (m_rx_check_manager.getTotalRx() == total_tx_rx_check) { - fprintf(fd," OK \n" ); - }else{ - fprintf(fd," FAIL \n" ); - } -} - - - void CTcpSeq::update(uint8_t *p, CFlowPktInfo *pkt_info, int16_t s_size){ TCPHeader *tcp= (TCPHeader *)(p+pkt_info->m_pkt_indication.getFastTcpOffset()); uint32_t seqnum, acknum; @@ -6865,12 +5984,14 @@ bool CSimplePacketParser::Parse(){ case EthernetHeader::Protocol::IP : // IPv4 packet ipv4=(IPHeader *)(p+14); + m_l4 = (uint8_t *)ipv4 + ipv4->getHeaderLength(); protocol = ipv4->getProtocol(); m_option_offset = 14 + IPV4_HDR_LEN; break; case EthernetHeader::Protocol::IPv6 : // IPv6 packet ipv6=(IPv6Header *)(p+14); + m_l4 = (uint8_t *)ipv6 + ipv6->getHeaderLength(); protocol = ipv6->getNextHdr(); m_option_offset = 14 +IPV6_HDR_LEN; break; @@ -6880,12 +6001,14 @@ bool CSimplePacketParser::Parse(){ case EthernetHeader::Protocol::IP: // IPv4 packet ipv4=(IPHeader *)(p+18); + m_l4 = (uint8_t *)ipv4 + ipv4->getHeaderLength(); protocol = ipv4->getProtocol(); m_option_offset = 18+ IPV4_HDR_LEN; break; case EthernetHeader::Protocol::IPv6 : // IPv6 packet ipv6=(IPv6Header *)(p+18); + m_l4 = (uint8_t *)ipv6 + ipv6->getHeaderLength(); protocol = ipv6->getNextHdr(); m_option_offset = 18 + IPV6_HDR_LEN; break; @@ -6929,7 +6052,3 @@ void CGenNodeBase::free_base(){ } - - - - diff --git a/src/bp_sim.h b/src/bp_sim.h index 0da7fb99..da8e8780 100755 --- a/src/bp_sim.h +++ b/src/bp_sim.h @@ -37,6 +37,7 @@ limitations under the License. #include <string> #include <common/Network/Packet/TcpHeader.h> #include <common/Network/Packet/UdpHeader.h> +#include <common/Network/Packet/IcmpHeader.h> #include <common/Network/Packet/IPHeader.h> #include <common/Network/Packet/IPv6Header.h> #include <common/Network/Packet/EthernetHeader.h> @@ -380,7 +381,7 @@ public: * * @return */ - virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, rte_mbuf_t *m)=0; + virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, uint8_t * p)=0; /** * translate a port_id to the correct dir on the core @@ -749,6 +750,7 @@ public: prefix=""; m_mac_splitter=0; m_run_mode = RUN_MODE_INVALID; + m_l_pkt_mode = 0; } @@ -771,7 +773,7 @@ public: uint16_t m_io_mode; //0,1,2 0 disable, 1- normal , 2 - short uint16_t m_run_flags; uint8_t m_mac_splitter; - uint8_t m_pad; + uint8_t m_l_pkt_mode; trex_run_mode_e m_run_mode; @@ -828,6 +830,9 @@ public: return ( (m_run_flags &RUN_FLAGS_RXCHECK_CONST_TS)?true:false ); } + inline uint8_t get_l_pkt_mode(){ + return (m_l_pkt_mode); + } void dump(FILE *fd); }; @@ -1237,7 +1242,8 @@ static inline int get_is_rx_check_mode(){ } static inline bool get_is_rx_filter_enable(){ - return ( ( get_is_rx_check_mode() || CGlobalInfo::is_learn_mode()) ?true:false ); + uint32_t latency_rate=CGlobalInfo::m_options.m_latency_rate; + return ( ( get_is_rx_check_mode() || CGlobalInfo::is_learn_mode() || latency_rate != 0) ?true:false ); } static inline uint16_t get_rx_check_hops() { return (CGlobalInfo::m_options.m_rx_check_hops); @@ -1799,7 +1805,7 @@ public: virtual int write_pkt(CCapPktRaw *pkt_raw); virtual int close_file(void); - virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, rte_mbuf_t *m){ + virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, uint8_t * p){ return (0); } @@ -1878,7 +1884,7 @@ public: return (0); } - virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, rte_mbuf_t *m){ + virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, uint8_t * p){ return (0); } @@ -2061,11 +2067,8 @@ inline bool CFlowKey::operator ==(const CFlowKey& rhs) const{ #define IS_VALID_S 1 #define IS_VALID_E 1 -#define IS_TCP_S 2 -#define IS_TCP_E 2 - -#define IS_UDP_S 3 -#define IS_UDP_E 3 +#define PROTO_S 3 +#define PROTO_E 2 #define IS_INIT_SIDE 4 @@ -2235,19 +2238,27 @@ public: } inline void SetIsTcp(bool is_valid){ - btSetMaskBit32(m_flags,IS_TCP_S,IS_TCP_E,is_valid?1:0); + btSetMaskBit32(m_flags,PROTO_S,PROTO_E,is_valid?1:0); } inline bool IsTcp(){ - return (btGetMaskBit32(m_flags,IS_TCP_S,IS_TCP_E) ? true:false); + return ((btGetMaskBit32(m_flags,PROTO_S,PROTO_E) == 1) ? true:false); } inline void SetIsUdp(bool is_valid){ - btSetMaskBit32(m_flags,IS_UDP_S,IS_UDP_E,is_valid?1:0); + btSetMaskBit32(m_flags,PROTO_S,PROTO_E,is_valid?2:0); } inline bool IsUdp(){ - return (btGetMaskBit32(m_flags,IS_UDP_S,IS_UDP_E) ? true:false); + return ((btGetMaskBit32(m_flags,PROTO_S,PROTO_E) == 2) ? true:false); + } + + inline void SetIsIcmp(bool is_valid){ + btSetMaskBit32(m_flags,PROTO_S,PROTO_E,is_valid?3:0); + } + + inline bool IsIcmp(){ + return ((btGetMaskBit32(m_flags,PROTO_S,PROTO_E) == 3) ? true:false); } inline void SetId(uint16_t _id){ @@ -2379,6 +2390,7 @@ public: union { TCPHeader * m_tcp; UDPHeader * m_udp; + ICMPHeader * m_icmp; } l4; uint8_t * m_payload; uint16_t m_payload_len; @@ -2902,7 +2914,11 @@ inline void CFlowPktInfo::update_pkt_info(char *p, m_udp->setDestPort(src_port); } }else{ - BP_ASSERT(0); +#ifdef _DEBUG + if (!m_pkt_indication.m_desc.IsIcmp()) { + BP_ASSERT(0); + } +#endif } } } @@ -3733,312 +3749,6 @@ inline void CFlowGeneratorRecPerThread::generate_flow(CNodeGenerator * gen, node); } - - -class CLatencyPktInfo { -public: - void Create(); - void Delete(); - void set_ip(uint32_t src, - uint32_t dst, - uint32_t dual_port_mask); - rte_mbuf_t * generate_pkt(int port_id,uint32_t extern_ip=0); - - CGenNode * getNode(){ - return (&m_dummy_node); - } - - uint16_t get_payload_offset(void){ - return ( m_pkt_indication.getFastPayloadOffset()); - } - - uint16_t get_pkt_size(void){ - return ( m_packet->pkt_len ); - } - -private: - ipaddr_t m_client_ip; - ipaddr_t m_server_ip; - uint32_t m_dual_port_mask; - - CGenNode m_dummy_node; - CFlowPktInfo m_pkt_info; - CPacketIndication m_pkt_indication; - CCapPktRaw * m_packet; -}; - - -#define LATENCY_MAGIC 0x12345600 - -struct latency_header { - - uint64_t time_stamp; - uint32_t magic; - uint32_t seq; - - uint8_t get_id(){ - return( magic & 0xff); - } -}; - - -class CSimplePacketParser { -public: - - CSimplePacketParser(rte_mbuf_t * m){ - m_m=m; - } - - bool Parse(); - uint8_t getTTl(); - uint16_t getPktSize(); - - - - inline bool IsLatencyPkt(){ - return ( (m_protocol ==0x84 )?true:false ); - } - - -public: - IPHeader * m_ipv4; - IPv6Header * m_ipv6; - uint8_t m_protocol; - uint16_t m_vlan_offset; - uint16_t m_option_offset; -private: - rte_mbuf_t * m_m ; -}; - - - -class CLatencyManager ; -// per port -class CCPortLatency { -public: - bool Create(CLatencyManager * parent, - uint8_t id, - uint16_t offset, - uint16_t pkt_size, - CCPortLatency * rx_port - ); - void Delete(); - void reset(); - bool can_send_packet(){ - if ( !CGlobalInfo::is_learn_mode() ) { - return(true); - } - return ( m_nat_can_send ); - } - uint32_t external_nat_ip(){ - return (m_nat_external_ip); - } - - void update_packet(rte_mbuf_t * m); - - bool do_learn(uint32_t external_ip); - - bool check_packet(rte_mbuf_t * m, - CRx_check_header * & rx_p); - bool check_rx_check(rte_mbuf_t * m); - - - bool dump_packet(rte_mbuf_t * m); - - void DumpCounters(FILE *fd); - void dump_counters_json(std::string & json ); - - void DumpShort(FILE *fd); - void dump_json(std::string & json ); - void dump_json_v2(std::string & json ); - - uint32_t get_jitter_usec(void){ - return ((uint32_t)(m_jitter.get_jitter()*1000000.0)); - } - - - static void DumpShortHeader(FILE *fd); - - bool is_any_err(){ - if ( (m_tx_pkt_ok == m_rx_port->m_pkt_ok ) && - - ((m_unsup_prot+ - m_no_magic+ - m_no_id+ - m_seq_error+ - m_length_error+m_no_ipv4_option+m_tx_pkt_err)==0) ) { - return (false); - } - return (true); - } - -private: - std::string get_field(std::string name,float f); - - - -private: - CLatencyManager * m_parent; - CCPortLatency * m_rx_port; /* corespond rx port */ - bool m_nat_learn; - bool m_nat_can_send; - uint32_t m_nat_external_ip; - - uint32_t m_tx_seq; - uint32_t m_rx_seq; - - uint8_t m_pad; - uint8_t m_id; - uint16_t m_offset; - - uint16_t m_pkt_size; - uint16_t pad1[3]; - -public: - uint64_t m_tx_pkt_ok; - uint64_t m_tx_pkt_err; - - uint64_t m_pkt_ok; - uint64_t m_unsup_prot; - uint64_t m_no_magic; - uint64_t m_no_id; - uint64_t m_seq_error; - uint64_t m_rx_check; - uint64_t m_no_ipv4_option; - - - uint64_t m_length_error; - CTimeHistogram m_hist; /* all window */ - CJitter m_jitter; -}; - - -class CPortLatencyHWBase { -public: - virtual int tx(rte_mbuf_t * m)=0; - virtual rte_mbuf_t * rx()=0; - virtual uint16_t rx_burst(struct rte_mbuf **rx_pkts, - uint16_t nb_pkts){ - return(0); - } -}; - - -class CLatencyManagerCfg { -public: - CLatencyManagerCfg (){ - m_max_ports=0; - m_cps=0.0; - m_client_ip.v4=0x10000000; - m_server_ip.v4=0x20000000; - m_dual_port_mask=0x01000000; - } - uint32_t m_max_ports; - double m_cps;// CPS - CPortLatencyHWBase * m_ports[MAX_LATENCY_PORTS]; - ipaddr_t m_client_ip; - ipaddr_t m_server_ip; - uint32_t m_dual_port_mask; - -}; - - - -class CLatencyManagerPerPort { -public: - CCPortLatency m_port; - CPortLatencyHWBase * m_io; - uint32_t m_flag; - -}; - - -class CLatencyManager { -public: - bool Create(CLatencyManagerCfg * cfg); - void Delete(); - -public: - void reset(); - void start(int iter); - void stop(); - bool is_active(); - - void set_ip(uint32_t client_ip, - uint32_t server_ip, - uint32_t mask_dual_port){ - m_pkt_gen.set_ip(client_ip,server_ip,mask_dual_port); - } - -public: - void Dump(FILE *fd); // dump all - void DumpShort(FILE *fd); // dump short histogram of latency - - void DumpRxCheck(FILE *fd); // dump all - void DumpShortRxCheck(FILE *fd); // dump short histogram of latency - void rx_check_dump_json(std::string & json); - uint16_t get_latency_header_offset(){ - return ( m_pkt_gen.get_payload_offset() ); - } - void update(); - void dump_json(std::string & json ); // dump to json - void dump_json_v2(std::string & json ); - - - - void DumpRxCheckVerification(FILE *fd,uint64_t total_tx_rx_check); - void set_mask(uint32_t mask){ - m_port_mask=mask; - } - - double get_max_latency(void); - double get_avr_latency(void); - bool is_any_error(); - uint64_t get_total_pkt(); - uint64_t get_total_bytes(); - CNatRxManager * get_nat_manager(){ - return ( &m_nat_check_manager ); - } - -private: - void send_pkt_all_ports(); - void try_rx(); - void try_rx_queues(); - void run_rx_queue_msgs(uint8_t thread_id, - CNodeRing * r); - void wait_for_rx_dump(); - void handle_rx_pkt(CLatencyManagerPerPort * lp, - rte_mbuf_t * m); - - -private: - /* messages handlers */ - void handle_latecy_pkt_msg(uint8_t thread_id, - CGenNodeLatencyPktInfo * msg); - - - -private: - pqueue_t m_p_queue; /* priorty queue */ - bool m_is_active; - CLatencyPktInfo m_pkt_gen; - CLatencyManagerPerPort m_ports[MAX_LATENCY_PORTS]; - uint64_t m_d_time; // calc tick betwen sending - double m_cps; - double m_delta_sec; - uint64_t m_start_time; // calc tick betwen sending - uint32_t m_port_mask; - uint32_t m_max_ports; - RxCheckManager m_rx_check_manager; - CNatRxManager m_nat_check_manager; - CCpuUtlDp m_cpu_dp_u; - CCpuUtlCp m_cpu_cp_u; - - volatile bool m_do_stop __rte_cache_aligned ; - -}; - - inline bool CGenNode::is_responder_pkt(){ return ( m_pkt_info->m_pkt_indication.m_desc.IsInitSide() ?false:true ); } diff --git a/src/common/Network/Packet/IcmpHeader.h b/src/common/Network/Packet/IcmpHeader.h new file mode 100644 index 00000000..99d89329 --- /dev/null +++ b/src/common/Network/Packet/IcmpHeader.h @@ -0,0 +1,89 @@ +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#ifndef _ICMP_HEADER_H_ +#define _ICMP_HEADER_H_ + +#include "PacketHeaderBase.h" +#include "IPHeader.h" + +class ICMPHeader +{ + +public: + ICMPHeader() + { + setCode(0); + setType(0); + setSeqNum(0xDEAD); + setId(0xBEEF); + setChecksum(0); + } + + ICMPHeader(uint8_t argType, + uint8_t argCode, + uint16_t argId, + uint16_t argSeqNum) + { + setType(argType); + setCode(argCode); + setId(argId); + setSeqNum(argSeqNum); + } + + + inline void setCode(uint8_t data); + inline uint8_t getCode(); + + inline void setType(uint8_t data); + inline uint8_t getType(); + + inline void setSeqNum(uint16_t data); + inline uint16_t getSeqNum(); + + inline void setId(uint16_t data); + inline uint16_t getId(); + + inline void setChecksum(uint16_t data); + inline uint16_t getChecksum(); + + inline void updateCheckSum(uint16_t len); + inline bool isCheckSumOk(uint16_t len); + inline uint16_t calcCheckSum(uint16_t len); + + +//////////////////////////////////////////////////////////////////////////////////////// +// Common Header Interface +//////////////////////////////////////////////////////////////////////////////////////// + +public: + inline uint8_t* getPointer (){return (uint8_t*)this;} + inline uint32_t getSize (){return 8;} + + void dump (FILE* fd); + +private: + uint8_t myType; + uint8_t myCode; + uint16_t myChecksum; + uint16_t myId; + uint16_t mySeqNum; +}; + + +#include "IcmpHeader.inl" + +#endif diff --git a/src/common/Network/Packet/IcmpHeader.inl b/src/common/Network/Packet/IcmpHeader.inl new file mode 100644 index 00000000..0e6806f8 --- /dev/null +++ b/src/common/Network/Packet/IcmpHeader.inl @@ -0,0 +1,87 @@ +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +inline void ICMPHeader::setCode(uint8_t argCode) +{ + myCode = argCode; +} + +inline uint8_t ICMPHeader::getCode() +{ + return myCode; +} + +inline void ICMPHeader::setType(uint8_t argType) +{ + myType = argType; +} + +inline uint8_t ICMPHeader::getType() +{ + return myType; +} + +inline void ICMPHeader::setSeqNum(uint16_t argSeqNum) +{ + mySeqNum = PKT_NTOHS(argSeqNum); +} + +inline uint16_t ICMPHeader::getSeqNum() +{ + return PKT_NTOHS(mySeqNum); +} + +inline void ICMPHeader::setId(uint16_t argId) +{ + myId = PKT_NTOHS(argId); +} + +inline uint16_t ICMPHeader::getId() +{ + return PKT_NTOHS(myId); +} + +inline void ICMPHeader::setChecksum(uint16_t argNewChecksum) +{ + myChecksum = PKT_NTOHS(argNewChecksum); +} + +inline uint16_t ICMPHeader::getChecksum() +{ + return PKT_NTOHS(myChecksum); +} + +inline void ICMPHeader::updateCheckSum(uint16_t len) +{ + setChecksum(0);// must be here + + myChecksum =calcCheckSum(len); +} + +inline bool ICMPHeader::isCheckSumOk(uint16_t len) +{ + uint16_t theChecksum= PKT_NTOHS(calcCheckSum(len)); + + return(theChecksum == 0); +} + +// len is in bytes. Including ICMP header + data. +inline uint16_t ICMPHeader::calcCheckSum(uint16_t len) +{ + uint16_t theChecksum = pkt_InetChecksum((uint8_t*)this, len); + + return(theChecksum); +} diff --git a/src/common/bitMan.h b/src/common/bitMan.h index ffa05598..8019b3f7 100755 --- a/src/common/bitMan.h +++ b/src/common/bitMan.h @@ -160,8 +160,8 @@ inline void btSetMaskBit32(unsigned int & a, btSetMaskBit<unsigned int>(a,startbit,stopbit,newVal); } -/* start > stop startbit = 10 , - stop = 8 +/* Notice: + startbit should be bigger (or equal) than stopbit count like big E diff --git a/src/dpdk_lib18/librte_ether/rte_eth_ctrl.h b/src/dpdk_lib18/librte_ether/rte_eth_ctrl.h index 642adb76..d9cdb379 100755 --- a/src/dpdk_lib18/librte_ether/rte_eth_ctrl.h +++ b/src/dpdk_lib18/librte_ether/rte_eth_ctrl.h @@ -202,6 +202,7 @@ enum rte_eth_flow_type { struct rte_eth_ipv4_flow { uint32_t src_ip; /**< IPv4 source address to match. */ uint32_t dst_ip; /**< IPv4 destination address to match. */ + uint8_t l4_proto; /* IPv4 protocol to match */ }; /** diff --git a/src/dpdk_lib18/librte_pmd_i40e/i40e_ethdev.c b/src/dpdk_lib18/librte_pmd_i40e/i40e_ethdev.c index 9c0db84c..b0e00464 100755 --- a/src/dpdk_lib18/librte_pmd_i40e/i40e_ethdev.c +++ b/src/dpdk_lib18/librte_pmd_i40e/i40e_ethdev.c @@ -355,6 +355,20 @@ static inline void i40e_flex_payload_reg_init(struct i40e_hw *hw) #define I40E_PRTQF_FD_INSET(_i, _j) (0x00250000 + ((_i) * 64 + (_j) * 32)) #define I40E_GLQF_FD_MSK(_i, _j) (0x00267200 + ((_i) * 4 + (_j) * 8)) +void dump_regs(struct i40e_hw *hw) +{ + int reg_nums[] = {31, 33, 34, 35, 41, 43}; + int i; + uint32_t reg; + + for (i =0; i < sizeof (reg_nums)/sizeof(int); i++) { + reg = I40E_READ_REG(hw,I40E_PRTQF_FD_INSET(reg_nums[i], 0)); + printf("I40E_PRTQF_FD_INSET(%d, 0): 0x%08x\n", reg_nums[i], reg); + reg = I40E_READ_REG(hw,I40E_PRTQF_FD_INSET(reg_nums[i], 1)); + printf("I40E_PRTQF_FD_INSET(%d, 1): 0x%08x\n", reg_nums[i], reg); + } +} + static inline void i40e_fillter_fields_reg_init(struct i40e_hw *hw) { uint32_t reg; @@ -403,6 +417,10 @@ static inline void i40e_fillter_fields_reg_init(struct i40e_hw *hw) //printf("I40E_PRTQF_FD_INSET(34, 1) = 0x%08x\n", reg); I40E_WRITE_REG(hw, I40E_PRTQF_FD_INSET(34, 1), 0x00040000); + // filter IP according to ttl and L4 protocol + I40E_WRITE_REG(hw, I40E_PRTQF_FD_INSET(35, 0), 0); + I40E_WRITE_REG(hw, I40E_PRTQF_FD_INSET(35, 1), 0x00040000); + reg = I40E_READ_REG(hw,I40E_PRTQF_FD_INSET(44, 0)); //printf("I40E_PRTQF_FD_INSET(44, 0) = 0x%08x\n", reg); I40E_WRITE_REG(hw, I40E_PRTQF_FD_INSET(44, 0), 0); @@ -420,8 +438,6 @@ static inline void i40e_fillter_fields_reg_init(struct i40e_hw *hw) I40E_WRITE_FLUSH(hw); } - - static int eth_i40e_dev_init(__rte_unused struct eth_driver *eth_drv, struct rte_eth_dev *dev) diff --git a/src/dpdk_lib18/librte_pmd_i40e/i40e_fdir.c b/src/dpdk_lib18/librte_pmd_i40e/i40e_fdir.c index 98df9357..4b209e18 100755 --- a/src/dpdk_lib18/librte_pmd_i40e/i40e_fdir.c +++ b/src/dpdk_lib18/librte_pmd_i40e/i40e_fdir.c @@ -727,7 +727,10 @@ i40e_fdir_fill_eth_ip_head(const struct rte_eth_fdir_input *fdir_input, */ ip->src_addr = fdir_input->flow.ip4_flow.dst_ip; ip->dst_addr = fdir_input->flow.ip4_flow.src_ip; - ip->next_proto_id = next_proto[fdir_input->flow_type]; + if (fdir_input->flow_type == RTE_ETH_FLOW_TYPE_IPV4_OTHER) { + ip->next_proto_id = fdir_input->flow.ip4_flow.l4_proto; + } else + ip->next_proto_id = next_proto[fdir_input->flow_type]; break; case RTE_ETH_FLOW_TYPE_UDPV6: case RTE_ETH_FLOW_TYPE_TCPV6: diff --git a/src/gtest/trex_stateless_gtest.cpp b/src/gtest/trex_stateless_gtest.cpp index 5b298023..1626ac25 100644 --- a/src/gtest/trex_stateless_gtest.cpp +++ b/src/gtest/trex_stateless_gtest.cpp @@ -30,7 +30,896 @@ limitations under the License. #include <trex_stateless_port.h> #include <trex_rpc_server_api.h> #include <iostream> +#include <vector> + + + +class CPcapLoader { +public: + CPcapLoader(); + ~CPcapLoader(); + + +public: + bool load_pcap_file(std::string file,int pkt_id=0); + void update_ip_src(uint32_t ip_addr); + void clone_packet_into_stream(TrexStream * stream); + void dump_packet(); + +public: + bool m_valid; + CCapPktRaw m_raw; + CPacketIndication m_pkt_indication; +}; + +CPcapLoader::~CPcapLoader(){ +} + +bool CPcapLoader::load_pcap_file(std::string cap_file,int pkt_id){ + m_valid=false; + CPacketParser parser; + + CCapReaderBase * lp=CCapReaderFactory::CreateReader((char *)cap_file.c_str(),0); + + if (lp == 0) { + printf(" ERROR file %s does not exist or not supported \n",(char *)cap_file.c_str()); + return false; + } + + int cnt=0; + bool found =false; + + + while ( true ) { + /* read packet */ + if ( lp->ReadPacket(&m_raw) ==false ){ + break; + } + if (cnt==pkt_id) { + found = true; + break; + } + cnt++; + } + if ( found ){ + if ( parser.ProcessPacket(&m_pkt_indication, &m_raw) ){ + m_valid = true; + } + } + + delete lp; + return (m_valid); +} + +void CPcapLoader::update_ip_src(uint32_t ip_addr){ + + if ( m_pkt_indication.l3.m_ipv4 ) { + m_pkt_indication.l3.m_ipv4->setSourceIp(ip_addr); + m_pkt_indication.l3.m_ipv4->updateCheckSum(); + } +} + +void CPcapLoader::clone_packet_into_stream(TrexStream * stream){ + + uint16_t pkt_size=m_raw.getTotalLen(); + + uint8_t *binary = new uint8_t[pkt_size]; + memcpy(binary,m_raw.raw,pkt_size); + stream->m_pkt.binary = binary; + stream->m_pkt.len = pkt_size; +} + + + + +CPcapLoader::CPcapLoader(){ + +} + +void CPcapLoader::dump_packet(){ + if (m_valid ) { + m_pkt_indication.Dump(stdout,1); + }else{ + fprintf(stdout," no packets were found \n"); + } +} + + + + +class basic_vm : public testing::Test { + protected: + virtual void SetUp() { + } + virtual void TearDown() { + } + public: +}; + + + + +TEST_F(basic_vm, pkt_size) { + + EXPECT_EQ(calc_writable_mbuf_size(36,62),62); + EXPECT_EQ(calc_writable_mbuf_size(63,62),62); + EXPECT_EQ(calc_writable_mbuf_size(45,65),65); + EXPECT_EQ(calc_writable_mbuf_size(66,65),65); + EXPECT_EQ(calc_writable_mbuf_size(62,128),128); + EXPECT_EQ(calc_writable_mbuf_size(62,252),61); + EXPECT_EQ(calc_writable_mbuf_size(121,252),120); + EXPECT_EQ(calc_writable_mbuf_size(253,252),252); + EXPECT_EQ(calc_writable_mbuf_size(250,252),252); + EXPECT_EQ(calc_writable_mbuf_size(184,252),183); +} + + +/* start/stop/stop back to back */ +TEST_F(basic_vm, vm0) { + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(20) ); + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",1, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,0,1,7 ) + ); + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var1",14, 0,true) + ); + + vm.Dump(stdout); +} + +TEST_F(basic_vm, vm1) { + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",1, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,0,1,7 ) + ); + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var1",26, 0,true) + ); + vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + vm.set_packet_size(128); + + vm.compile(); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + +} + +TEST_F(basic_vm, vm2) { + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",1, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,4,1,7 ) + ); + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var1",26, 0,true) + ); + //vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + vm.set_packet_size(128); + + vm.compile(); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + uint8_t test_udp_pkt[14+20+4+4]={ + 0x00,0x00,0x00,0x01,0x00,0x00, + 0x00,0x00,0x00,0x01,0x00,0x00, + 0x08,0x00, + + 0x45,0x00,0x00,0x81, /*14 */ + 0xaf,0x7e,0x00,0x00, /*18 */ + 0x12,0x11,0xd9,0x23, /*22 */ + 0x01,0x01,0x01,0x01, /*26 */ + 0x3d,0xad,0x72,0x1b, /*30 */ + + 0x11,0x11, + 0x11,0x11, + + 0x00,0x6d, + 0x00,0x00, + }; + + + + StreamDPVmInstructionsRunner runner; + + uint8_t ex[]={5, + 6, + 7, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 1, + 2, + 3}; + + int i; + for (i=0; i<20; i++) { + runner.run(program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + test_udp_pkt); + EXPECT_EQ(test_udp_pkt[26],ex[i]); + } + +} + + +TEST_F(basic_vm, vm3) { + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",4 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,4,1,7 ) + ); + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var1",26, 0,true) + ); + //vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + vm.set_packet_size(128); + + vm.compile(); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + #define PKT_TEST_SIZE (14+20+4+4) + uint8_t test_udp_pkt[PKT_TEST_SIZE]={ + 0x00,0x00,0x00,0x01,0x00,0x00, + 0x00,0x00,0x00,0x01,0x00,0x00, + 0x08,0x00, + + 0x45,0x00,0x00,0x81, /*14 */ + 0xaf,0x7e,0x00,0x00, /*18 */ + 0x12,0x11,0xd9,0x23, /*22 */ + 0x01,0x01,0x01,0x01, /*26 */ + 0x3d,0xad,0x72,0x1b, /*30 */ + + 0x11,0x11, + 0x11,0x11, + + 0x00,0x6d, + 0x00,0x00, + }; + + + + StreamDPVmInstructionsRunner runner; + + uint8_t ex[]={5, + 6, + 7, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 1, + 2, + 3}; + + int i; + for (i=0; i<20; i++) { + runner.run(program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + test_udp_pkt); + + fprintf(stdout," %d \n",i); + //utl_DumpBuffer(stdout,test_udp_pkt,PKT_TEST_SIZE,0); + /* big */ + EXPECT_EQ(test_udp_pkt[29],ex[i]); + EXPECT_EQ(test_udp_pkt[28],0); + EXPECT_EQ(test_udp_pkt[27],0); + EXPECT_EQ(test_udp_pkt[26],0); + } + +} + +TEST_F(basic_vm, vm4) { + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",4 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,4,1,7 ) + ); + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var1",26, 0,false) + ); + //vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + vm.set_packet_size(128); + + vm.compile(); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + #define PKT_TEST_SIZE (14+20+4+4) + uint8_t test_udp_pkt[PKT_TEST_SIZE]={ + 0x00,0x00,0x00,0x01,0x00,0x00, + 0x00,0x00,0x00,0x01,0x00,0x00, + 0x08,0x00, + + 0x45,0x00,0x00,0x81, /*14 */ + 0xaf,0x7e,0x00,0x00, /*18 */ + 0x12,0x11,0xd9,0x23, /*22 */ + 0x01,0x01,0x01,0x01, /*26 */ + 0x3d,0xad,0x72,0x1b, /*30 */ + + 0x11,0x11, + 0x11,0x11, + + 0x00,0x6d, + 0x00,0x00, + }; + + + + StreamDPVmInstructionsRunner runner; + + uint8_t ex[]={5, + 6, + 7, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 1, + 2, + 3}; + + int i; + for (i=0; i<20; i++) { + runner.run(program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + test_udp_pkt); + + fprintf(stdout," %d \n",i); + //utl_DumpBuffer(stdout,test_udp_pkt,PKT_TEST_SIZE,0); + /* not big */ + EXPECT_EQ(test_udp_pkt[29],0); + EXPECT_EQ(test_udp_pkt[28],0); + EXPECT_EQ(test_udp_pkt[27],0); + EXPECT_EQ(test_udp_pkt[26],ex[i]); + } + +} + + +/* two fields */ +TEST_F(basic_vm, vm5) { + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",4 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,4,1,7 ) + ); + + vm.add_instruction( new StreamVmInstructionFlowMan( "var2",1 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_DEC,25,23,27 ) ); + + /* src ip */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var1",26, 0,true) + ); + + /* change TOS */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var2",15, 0,true) + ); + + vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + vm.set_packet_size(128); + + vm.compile(); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + #define PKT_TEST_SIZE (14+20+4+4) + uint8_t test_udp_pkt[PKT_TEST_SIZE]={ + 0x00,0x00,0x00,0x01,0x00,0x00, + 0x00,0x00,0x00,0x01,0x00,0x00, + 0x08,0x00, + + 0x45,0x00,0x00,0x81, /*14 */ + 0xaf,0x7e,0x00,0x00, /*18 */ + 0x12,0x11,0xd9,0x23, /*22 */ + 0x01,0x01,0x01,0x01, /*26 */ + 0x3d,0xad,0x72,0x1b, /*30 */ + + 0x11,0x11, /*34 */ + 0x11,0x11, + + 0x00,0x6d, + 0x00,0x00, + }; + + + + StreamDPVmInstructionsRunner runner; + + uint8_t ex[]={5, + 6, + 7, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 1, + 2, + 3}; + + uint8_t ex_tos[]={0x18, + 0x17, + 0x1b, + 0x1a, + 0x19, + 0x18, + 0x17, + 0x1b, + 0x1a, + 0x19, + 0x18, + 0x17, + 0x1b, + 0x1a, + 0x19, + 0x18, + 0x17, + 0x1b, + 0x1a, + 0x19, + 0x18, + 0x17, + + 0x1b, + 0x1a, + 0x19, + 0x18, + 0x17, + + 0x1b, + 0x1a, + 0x19, + 0x18, + 0x17, + + 0x1b, + 0x1a, + 0x19, + 0x18, + 0x17, + }; + + int i; + for (i=0; i<20; i++) { + runner.run(program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + test_udp_pkt); + + fprintf(stdout," %d \n",i); + //utl_DumpBuffer(stdout,test_udp_pkt,PKT_TEST_SIZE,0); + /* not big */ + EXPECT_EQ(test_udp_pkt[29],ex[i]); + EXPECT_EQ(test_udp_pkt[28],0); + EXPECT_EQ(test_udp_pkt[27],0); + EXPECT_EQ(test_udp_pkt[26],0); + + /* check tos */ + EXPECT_EQ(test_udp_pkt[15],ex_tos[i]); + } +} + +/* -load file, write to file */ +TEST_F(basic_vm, vm6) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",4 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,0x10000001,0x10000001,0x100000fe) + ); + + vm.add_instruction( new StreamVmInstructionFlowMan( "var2",1 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_DEC,25,23,27 ) ); + + /* src ip */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var1",26, 0,true) + ); + + /* change TOS */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "var2",15, 0,true) + ); + + vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + vm.set_packet_size(128); + + vm.compile(); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm6.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + int i; + for (i=0; i<20; i++) { + runner.run(program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + //utl_DumpBuffer(stdout,pcap.m_raw.raw,pcap.m_raw.pkt_len,0); + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm6.pcap","exp/udp_64B_vm6-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + +/* test client command */ +TEST_F(basic_vm, vm7) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowClient( "cl1", + 0x10000001, + 0x10000004, + 1025, + 1027, + 100, + 0) ); + + /* src ip */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "cl1.ip",26, 0,true) + ); + + vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + /* src port */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "cl1.port",34, 0,true) + ); + + + vm.set_packet_size(128); + + vm.compile(); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm7.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + int i; + for (i=0; i<20; i++) { + runner.run(program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm7.pcap","exp/udp_64B_vm7-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + +TEST_F(basic_vm, vm8) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowClient( "cl1", + 0x10000001, + 0x10000006, + 1025, + 1027, + 4, + 0) ); + + /* src ip */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "cl1.ip",26, 0,true) + ); + + vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + /* src port */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "cl1.port",34, 0,true) + ); + + + vm.set_packet_size(128); + + vm.compile(); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm8.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + int i; + for (i=0; i<20; i++) { + runner.run(program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm8.pcap","exp/udp_64B_vm8-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + +static void vm_build_program_seq(StreamVm & vm, + uint16_t packet_size, + bool should_compile){ + + vm.add_instruction( new StreamVmInstructionFlowClient( "tuple_gen", + 0x10000001, + 0x10000006, + 1025, + 1027, + 20, + 0) ); + + /* src ip */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "tuple_gen.ip",26, 0,true) + ); + + vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + + /* src port */ + vm.add_instruction( new StreamVmInstructionWriteToPkt( "tuple_gen.port",34, 0,true) + ); + + + vm.set_packet_size(packet_size); + + if (should_compile) { + vm.compile(); + } +} + + +TEST_F(basic_vm, vm9) { + + + StreamVm vm; + + vm_build_program_seq(vm,128, true); + + printf(" max packet update %lu \n",(ulong)vm.get_max_packet_update_offset()); + + EXPECT_EQ(36,vm.get_max_packet_update_offset()); + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm9.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + int i; + for (i=0; i<30; i++) { + runner.run(program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm9.pcap","exp/udp_64B_vm9-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + + +/* test vmDP object */ +TEST_F(basic_vm, vm10) { + + StreamVm vm; + + vm_build_program_seq(vm,128, true); + + printf(" max packet update %lu \n",(ulong)vm.get_max_packet_update_offset()); + + EXPECT_EQ(36,vm.get_max_packet_update_offset()); + + StreamVmDp * lpDpVm =vm.cloneAsVmDp(); + + EXPECT_EQ(lpDpVm->get_bss_size(),vm.get_bss_size()); + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm9.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + int i; + for (i=0; i<30; i++) { + + runner.run(lpDpVm->get_program_size(), + lpDpVm->get_program(), + lpDpVm->get_bss(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + delete lpDpVm; + + bool res1=cmp.compare("exp/udp_64B_vm9.pcap","exp/udp_64B_vm9-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + + + +////////////////////////////////////////////////////// + + #define EXPECT_EQ_UINT32(a,b) EXPECT_EQ((uint32_t)(a),(uint32_t)(b)) @@ -274,96 +1163,6 @@ public: }; -class CPcapLoader { -public: - CPcapLoader(); - ~CPcapLoader(); - - -public: - bool load_pcap_file(std::string file,int pkt_id=0); - void update_ip_src(uint32_t ip_addr); - void clone_packet_into_stream(TrexStream * stream); - void dump_packet(); - -public: - bool m_valid; - CCapPktRaw m_raw; - CPacketIndication m_pkt_indication; -}; - -CPcapLoader::~CPcapLoader(){ -} - -bool CPcapLoader::load_pcap_file(std::string cap_file,int pkt_id){ - m_valid=false; - CPacketParser parser; - - CCapReaderBase * lp=CCapReaderFactory::CreateReader((char *)cap_file.c_str(),0); - - if (lp == 0) { - printf(" ERROR file %s does not exist or not supported \n",(char *)cap_file.c_str()); - return false; - } - - int cnt=0; - bool found =false; - - - while ( true ) { - /* read packet */ - if ( lp->ReadPacket(&m_raw) ==false ){ - break; - } - if (cnt==pkt_id) { - found = true; - break; - } - cnt++; - } - if ( found ){ - if ( parser.ProcessPacket(&m_pkt_indication, &m_raw) ){ - m_valid = true; - } - } - - delete lp; - return (m_valid); -} - -void CPcapLoader::update_ip_src(uint32_t ip_addr){ - - if ( m_pkt_indication.l3.m_ipv4 ) { - m_pkt_indication.l3.m_ipv4->setSourceIp(ip_addr); - m_pkt_indication.l3.m_ipv4->updateCheckSum(); - } -} - -void CPcapLoader::clone_packet_into_stream(TrexStream * stream){ - - uint16_t pkt_size=m_raw.getTotalLen(); - - uint8_t *binary = new uint8_t[pkt_size]; - memcpy(binary,m_raw.raw,pkt_size); - stream->m_pkt.binary = binary; - stream->m_pkt.len = pkt_size; -} - - - - -CPcapLoader::CPcapLoader(){ - -} - -void CPcapLoader::dump_packet(){ - if (m_valid ) { - m_pkt_indication.Dump(stdout,1); - }else{ - fprintf(stdout," no packets were found \n"); - } -} - TEST_F(basic_stl, load_pcap_file) { printf (" stateles %d \n",(int)sizeof(CGenNodeStateless)); @@ -435,11 +1234,9 @@ TEST_F(basic_stl, basic_pause_resume0) { // stream - clean - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpStartCmd = new TrexStatelessDpStart(port_id, 0, comp_obj.clone(), 10.0 /*sec */ ); + std::vector<TrexStreamsCompiledObj *> objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); t1.m_msg_queue.add_msg(lpStartCmd); @@ -499,14 +1296,9 @@ void CBBStartStopDelay2::call_after_init(CBasicStl * m_obj){ streams.push_back(stream1); // stream - clean - - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); - - assert(compile.compile(streams, comp_obj) ); - - - /* start with different event id */ - TrexStatelessDpStart * lpStartCmd = new TrexStatelessDpStart(m_port_id, 1, comp_obj.clone(), 10.0 /*sec */ ); + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 1, objs[0], 10.0 /*sec */ ); m_obj->m_msg_queue.add_command(m_core,lpStopCmd, 5.0); /* command in delay of 5 sec */ @@ -552,12 +1344,9 @@ TEST_F(basic_stl, single_pkt_bb_start_stop_delay2) { streams.push_back(stream1); // stream - clean - - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpStartCmd = new TrexStatelessDpStart(port_id, 0, comp_obj.clone(), 10.0 /*sec */ ); + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); t1.m_msg_queue.add_msg(lpStartCmd); @@ -633,12 +1422,9 @@ TEST_F(basic_stl, single_pkt_bb_start_stop_delay1) { streams.push_back(stream1); // stream - clean - - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpStartCmd = new TrexStatelessDpStart(port_id, 0, comp_obj.clone(), 10.0 /*sec */ ); + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); t1.m_msg_queue.add_msg(lpStartCmd); @@ -687,12 +1473,10 @@ TEST_F(basic_stl, single_pkt_bb_start_stop3) { streams.push_back(stream1); // stream - clean + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpStartCmd = new TrexStatelessDpStart(port_id, 0, comp_obj.clone(), 10.0 /*sec */ ); TrexStatelessDpStop * lpStopCmd = new TrexStatelessDpStop(port_id); TrexStatelessDpStop * lpStopCmd1 = new TrexStatelessDpStop(port_id); @@ -740,14 +1524,12 @@ TEST_F(basic_stl, single_pkt_bb_start_stop2) { streams.push_back(stream1); // stream - clean + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpStartCmd = new TrexStatelessDpStart(port_id, 0, comp_obj.clone(), 10.0 /*sec */ ); TrexStatelessDpStop * lpStopCmd = new TrexStatelessDpStop(port_id); - TrexStatelessDpStart * lpStartCmd1 = new TrexStatelessDpStart(port_id, 0, comp_obj.clone(), 10.0 /*sec */ ); + TrexStatelessDpStart * lpStartCmd1 = new TrexStatelessDpStart(port_id, 0, objs[0]->clone(), 10.0 /*sec */ ); t1.m_msg_queue.add_msg(lpStartCmd); @@ -795,12 +1577,10 @@ TEST_F(basic_stl, single_pkt_bb_start_stop) { streams.push_back(stream1); // stream - clean + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpStartCmd = new TrexStatelessDpStart(port_id, 0, comp_obj.clone(), 10.0 /*sec */ ); TrexStatelessDpStop * lpStopCmd = new TrexStatelessDpStop(port_id); @@ -880,14 +1660,13 @@ TEST_F(basic_stl, simple_prog4) { streams.push_back(stream2); - TrexStreamsCompiledObj comp_obj(0,1.0); - - EXPECT_TRUE(compile.compile(streams, comp_obj) ); + uint8_t port_id = 0; + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 0, objs[0], 20.0 /*sec */ ); - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(0, 0, comp_obj.clone(), 20.0 ); - - t1.m_msg = lpstart; + t1.m_msg = lpStartCmd; bool res=t1.init(); @@ -950,11 +1729,10 @@ TEST_F(basic_stl, simple_prog3) { streams.push_back(stream2); - TrexStreamsCompiledObj comp_obj(0,1.0); - - EXPECT_TRUE(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(0, 0, comp_obj.clone(), 50.0 ); + uint8_t port_id = 0; + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], 50.0 /*sec */ ); t1.m_msg = lpstart; @@ -1011,13 +1789,10 @@ TEST_F(basic_stl, simple_prog2) { pcap.clone_packet_into_stream(stream2); streams.push_back(stream2); - - TrexStreamsCompiledObj comp_obj(0,1.0); - - EXPECT_TRUE(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(0, 0, comp_obj.clone(), 10.0 ); - + uint8_t port_id = 0; + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); t1.m_msg = lpstart; @@ -1074,11 +1849,10 @@ TEST_F(basic_stl, simple_prog1) { streams.push_back(stream2); - TrexStreamsCompiledObj comp_obj(0,1.0); - - EXPECT_TRUE(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(0, 0, comp_obj.clone(), 10.0 ); + uint8_t port_id = 0; + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); t1.m_msg = lpstart; @@ -1119,12 +1893,10 @@ TEST_F(basic_stl, single_pkt_burst1) { streams.push_back(stream1); - TrexStreamsCompiledObj comp_obj(0,1.0); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(0, 0, comp_obj.clone(), 10.0 ); - + uint8_t port_id = 0; + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); t1.m_msg = lpstart; @@ -1170,11 +1942,9 @@ TEST_F(basic_stl, single_pkt) { // stream - clean - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(port_id, 0, comp_obj.clone(), 10.0 /*sec */ ); + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); t1.m_msg = lpstart; @@ -1226,24 +1996,117 @@ TEST_F(basic_stl, multi_pkt1) { streams.push_back(stream2); + // stream - clean + uint8_t port_id = 0; + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); + + t1.m_msg = lpstart; + + bool res=t1.init(); + + delete stream1 ; + delete stream2 ; + + EXPECT_EQ_UINT32(1, res?1:0)<< "pass"; +} + + +class CEnableVm { +public: + void run(bool full_packet,double duration ); +public: + std::string m_input_packet; //"cap2/udp_64B.pcap" + std::string m_out_file; //"exp/stl_vm_enable0"; +}; + +void CEnableVm::run(bool full_packet,double duration=10.0){ + + CBasicStl t1; + CParserOption * po =&CGlobalInfo::m_options; + po->preview.setVMode(7); + po->preview.setFileWrite(true); + po->out_file =m_out_file; + + TrexStreamsCompiler compile; + + uint8_t port_id=0; + + std::vector<TrexStream *> streams; + + TrexStream * stream1 = new TrexStream(TrexStream::stCONTINUOUS,0,0); + + stream1->set_pps(1.0); + + stream1->m_enabled = true; + stream1->m_self_start = true; + stream1->m_port_id= port_id; + + CPcapLoader pcap; + pcap.load_pcap_file(m_input_packet,0); + pcap.update_ip_src(0x10000001); + pcap.clone_packet_into_stream(stream1); + + uint16_t pkt_size=pcap.m_raw.pkt_len; + + vm_build_program_seq(stream1->m_vm,pkt_size, false); + #if 0 + if ( full_packet ){ + EXPECT_EQ(stream1->m_vm_prefix_size,pkt_size); + }else{ + EXPECT_EQ(stream1->m_vm_prefix_size,35); + } + #endif + + + streams.push_back(stream1); + // stream - clean - TrexStreamsCompiledObj comp_obj(0,1.0); + std::vector<TrexStreamsCompiledObj *> objs; - assert(compile.compile(streams, comp_obj) ); + assert(compile.compile(port_id,streams, objs) ); + + TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], duration /*sec */ ); - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(0, 0, comp_obj.clone(), 10 ); t1.m_msg = lpstart; bool res=t1.init(); delete stream1 ; - delete stream2 ; EXPECT_EQ_UINT32(1, res?1:0)<< "pass"; } +TEST_F(basic_stl, vm_enable0) { + + CEnableVm vm_test; + vm_test.m_out_file = "exp/stl_vm_enable0"; + vm_test.m_input_packet = "cap2/udp_64B.pcap"; + vm_test.run(true); +} + + +TEST_F(basic_stl, vm_enable1) { + + CEnableVm vm_test; + vm_test.m_out_file = "exp/stl_vm_enable1"; + vm_test.m_input_packet = "stl/udp_594B_no_crc.pcap"; + vm_test.run(false); +} + + + +TEST_F(basic_stl, vm_enable2) { + + CEnableVm vm_test; + vm_test.m_out_file = "exp/stl_vm_enable2"; + vm_test.m_input_packet = "cap2/udp_64B.pcap"; + vm_test.run(true,50.0); +} + @@ -1290,11 +2153,10 @@ TEST_F(basic_stl, multi_pkt2) { // stream - clean - TrexStreamsCompiledObj comp_obj(0,5.0); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(0, 0, comp_obj.clone(), 10 ); + uint8_t port_id = 0; + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs, 1, 5.0)); + TrexStatelessDpStart *lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], 10.0 /*sec */ ); t1.m_msg = lpstart; @@ -1336,11 +2198,10 @@ TEST_F(basic_stl, multi_burst1) { streams.push_back(stream1); - TrexStreamsCompiledObj comp_obj(0,1.0); - - assert(compile.compile(streams, comp_obj) ); - - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(0, 0, comp_obj.clone(), 40 ); + uint8_t port_id = 0; + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpstart = new TrexStatelessDpStart(port_id, 0, objs[0], 40.0 /*sec */ ); t1.m_msg = lpstart; @@ -1370,10 +2231,9 @@ TEST_F(basic_stl, compile_bad_1) { streams.push_back(stream1); - TrexStreamsCompiledObj comp_obj(0,1.0); - std::string err_msg; - EXPECT_FALSE(compile.compile(streams, comp_obj, &err_msg)); + std::vector<TrexStreamsCompiledObj *>objs; + EXPECT_FALSE(compile.compile(0, streams, objs, 1, 1, &err_msg)); delete stream1; @@ -1403,10 +2263,12 @@ TEST_F(basic_stl, compile_bad_2) { streams.push_back(stream1); streams.push_back(stream2); - TrexStreamsCompiledObj comp_obj(0,1.0); + uint8_t port_id = 0; std::string err_msg; - EXPECT_FALSE(compile.compile(streams, comp_obj, &err_msg)); + std::vector<TrexStreamsCompiledObj *>objs; + EXPECT_FALSE(compile.compile(port_id, streams, objs, 1, 1, &err_msg)); + delete stream1; delete stream2; @@ -1482,10 +2344,10 @@ TEST_F(basic_stl, compile_bad_3) { streams.push_back(stream); /* compile */ - TrexStreamsCompiledObj comp_obj(0,1.0); - std::string err_msg; - EXPECT_FALSE(compile.compile(streams, comp_obj, &err_msg)); + std::vector<TrexStreamsCompiledObj *>objs; + EXPECT_FALSE(compile.compile(0, streams, objs, 1, 1, &err_msg)); + for (auto stream : streams) { delete stream; @@ -1534,11 +2396,11 @@ TEST_F(basic_stl, compile_with_warnings) { /* compile */ - TrexStreamsCompiledObj comp_obj(0,1.0); - std::string err_msg; - EXPECT_TRUE(compile.compile(streams, comp_obj, &err_msg)); - + std::vector<TrexStreamsCompiledObj *>objs; + EXPECT_TRUE(compile.compile(0, streams, objs, 1, 1, &err_msg)); + delete objs[0]; + EXPECT_TRUE(compile.get_last_compile_warnings().size() == 1); for (auto stream : streams) { @@ -1573,20 +2435,22 @@ TEST_F(basic_stl, compile_good_stream_id_compres) { streams.push_back(stream1); streams.push_back(stream2); - TrexStreamsCompiledObj comp_obj(0,1.0); - + uint8_t port_id = 0; std::string err_msg; - EXPECT_TRUE(compile.compile(streams, comp_obj, &err_msg)); + std::vector<TrexStreamsCompiledObj *>objs; + EXPECT_TRUE(compile.compile(port_id, streams, objs, 1, 1, &err_msg)); printf(" %s \n",err_msg.c_str()); - comp_obj.Dump(stdout); + objs[0]->Dump(stdout); + + EXPECT_EQ_UINT32(objs[0]->get_objects()[0].m_stream->m_stream_id,0); + EXPECT_EQ_UINT32(objs[0]->get_objects()[0].m_stream->m_next_stream_id,1); - EXPECT_EQ_UINT32(comp_obj.get_objects()[0].m_stream->m_stream_id,0); - EXPECT_EQ_UINT32(comp_obj.get_objects()[0].m_stream->m_next_stream_id,1); + EXPECT_EQ_UINT32(objs[0]->get_objects()[1].m_stream->m_stream_id,1); + EXPECT_EQ_UINT32(objs[0]->get_objects()[1].m_stream->m_next_stream_id,0); - EXPECT_EQ_UINT32(comp_obj.get_objects()[1].m_stream->m_stream_id,1); - EXPECT_EQ_UINT32(comp_obj.get_objects()[1].m_stream->m_next_stream_id,0); + delete objs[0]; delete stream1; delete stream2; @@ -1648,14 +2512,12 @@ TEST_F(basic_stl, dp_stop_event) { // stream - clean - TrexStreamsCompiledObj comp_obj(port_id, 1.0 /*mul*/); + std::vector<TrexStreamsCompiledObj *>objs; + assert(compile.compile(port_id, streams, objs)); + TrexStatelessDpStart *lpStartCmd = new TrexStatelessDpStart(port_id, 17, objs[0], 10.0 /*sec */ ); - assert(compile.compile(streams, comp_obj) ); - TrexStatelessDpStart * lpstart = new TrexStatelessDpStart(port_id, 17, comp_obj.clone(), 10.0 /*sec */ ); - - - t1.m_msg = lpstart; + t1.m_msg = lpStartCmd; /* let me handle these */ DpToCpHandlerStopEvent handler(17); @@ -1713,13 +2575,15 @@ TEST_F(basic_stl, graph_generator1) { streams.push_back(stream); - const TrexStreamsGraphObj &obj = graph.generate(streams); - EXPECT_EQ(obj.get_max_bps(), 405120); - EXPECT_EQ(obj.get_max_pps(), 50); + const TrexStreamsGraphObj *obj = graph.generate(streams); + EXPECT_EQ(obj->get_max_bps(), 405120); + EXPECT_EQ(obj->get_max_pps(), 50); for (auto stream : streams) { delete stream; } + + delete obj; } @@ -1761,15 +2625,17 @@ TEST_F(basic_stl, graph_generator2) { streams.push_back(stream); - const TrexStreamsGraphObj &obj = graph.generate(streams); - EXPECT_EQ(obj.get_max_pps(), 1000.0); + const TrexStreamsGraphObj *obj = graph.generate(streams); + EXPECT_EQ(obj->get_max_pps(), 1000.0); - EXPECT_EQ(obj.get_max_bps(), (1000 * (128 + 4) * 8)); + EXPECT_EQ(obj->get_max_bps(), (1000 * (128 + 4) * 8)); for (auto stream : streams) { delete stream; } + + delete obj; } /* stress test */ diff --git a/src/latency.cpp b/src/latency.cpp new file mode 100644 index 00000000..02b54f75 --- /dev/null +++ b/src/latency.cpp @@ -0,0 +1,1019 @@ +/* + Hanoh Haim + Ido Barnea + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#include "latency.h" +#include "bp_sim.h" +#include "utl_json.h" +#include <common/basic_utils.h> + +const uint8_t sctp_pkt[]={ + + 0x00,0x04,0x96,0x08,0xe0,0x40, + 0x00,0x0e,0x2e,0x24,0x37,0x5f, + 0x08,0x00, + + 0x45,0x02,0x00,0x30, + 0x00,0x00,0x40,0x00, + 0xff,0x84,0xbd,0x04, + 0x9b,0xe6,0x18,0x9b, //sIP + 0xcb,0xff,0xfc,0xc2, //DIP + + 0x80,0x44,//SPORT + 0x00,0x50,//DPORT + + 0x00,0x00,0x00,0x00, //checksum + + 0x11,0x22,0x33,0x44, // magic + 0x00,0x00,0x00,0x00, //64 bit counter + 0x00,0x00,0x00,0x00, + 0x00,0x01,0xa0,0x00, //seq + 0x00,0x00,0x00,0x00, + +}; + +const uint8_t icmp_pkt[]={ + 0x00,0x04,0x96,0x08,0xe0,0x40, + 0x00,0x0e,0x2e,0x24,0x37,0x5f, + 0x08,0x00, + + 0x45,0x02,0x00,0x30, + 0x00,0x00,0x40,0x00, + 0xff,0x01,0xbd,0x04, + 0x9b,0xe6,0x18,0x9b, //SIP + 0xcb,0xff,0xfc,0xc2, //DIP + + 0x08, 0x00, + 0x01, 0x02, //checksum + 0xaa, 0xbb, // id + 0x00, 0x00, // Sequence number + + 0x11,0x22,0x33,0x44, // magic + 0x00,0x00,0x00,0x00, //64 bit counter + 0x00,0x00,0x00,0x00, + 0x00,0x01,0xa0,0x00, //seq + 0x00,0x00,0x00,0x00, + +}; + + +void CLatencyPktInfo::Create(class CLatencyPktMode *m_l_pkt_info){ + uint8_t pkt_size = m_l_pkt_info->getPacketLen(); + + m_packet = new CCapPktRaw( pkt_size); + m_packet->pkt_cnt=0; + m_packet->time_sec=0; + m_packet->time_nsec=0; + memcpy(m_packet->raw, m_l_pkt_info->getPacketData(), pkt_size); + m_packet->pkt_len=pkt_size; + + m_pkt_indication.m_packet =m_packet; + + m_pkt_indication.m_ether = (EthernetHeader *)m_packet->raw; + m_pkt_indication.l3.m_ipv4=(IPHeader *)(m_packet->raw+14); + m_pkt_indication.m_is_ipv6 = false; + m_pkt_indication.l4.m_icmp=(ICMPHeader *)m_packet->raw+14+20; + m_pkt_indication.m_payload=(uint8_t *)m_packet->raw+14+20+16; + m_pkt_indication.m_payload_len=0; + m_pkt_indication.m_packet_padding=4; + + + m_pkt_indication.m_ether_offset =0; + m_pkt_indication.m_ip_offset =14; + m_pkt_indication.m_udp_tcp_offset = 34; + m_pkt_indication.m_payload_offset = 34+8; + + CPacketDescriptor * lpd=&m_pkt_indication.m_desc; + lpd->Clear(); + lpd->SetInitSide(true); + lpd->SetSwapTuple(false); + lpd->SetIsValidPkt(true); + lpd->SetIsIcmp(true); + lpd->SetIsLastPkt(true); + m_pkt_info.Create(&m_pkt_indication); + + memset(&m_dummy_node,0,sizeof(m_dummy_node)); + + m_dummy_node.set_socket_id( CGlobalInfo::m_socket.port_to_socket(0) ); + + m_dummy_node.m_time =0.1; + m_dummy_node.m_pkt_info = &m_pkt_info; + m_dummy_node.m_dest_ip = 0; + m_dummy_node.m_src_ip = 0; + m_dummy_node.m_src_port = 0x11; + m_dummy_node.m_flow_id =0; + m_dummy_node.m_flags =CGenNode::NODE_FLAGS_LATENCY; + +} + +rte_mbuf_t * CLatencyPktInfo::generate_pkt(int port_id,uint32_t extern_ip){ + bool is_client_to_server=(port_id%2==0)?true:false; + + int dual_port_index=(port_id>>1); + uint32_t c=m_client_ip.v4; + uint32_t s=m_server_ip.v4; + if ( extern_ip ){ + c=extern_ip; + } + + if (!is_client_to_server) { + /*swap */ + uint32_t t=c; + c=s; + s=t; + } + uint32_t mask=dual_port_index*m_dual_port_mask; + if ( extern_ip==0 ){ + c+=mask; + } + s+=mask; + m_dummy_node.m_src_ip = c; + m_dummy_node.m_dest_ip = s; + + rte_mbuf_t * m=m_pkt_info.generate_new_mbuf(&m_dummy_node); + return (m); +} + +void CLatencyPktInfo::set_ip(uint32_t src, + uint32_t dst, + uint32_t dual_port_mask){ + m_client_ip.v4=src; + m_server_ip.v4=dst; + m_dual_port_mask=dual_port_mask; +} + +void CLatencyPktInfo::Delete(){ + m_pkt_info.Delete(); + delete m_packet; +} + +void CCPortLatency::reset(){ + m_rx_seq =m_tx_seq; + m_pad = 0; + m_tx_pkt_err=0; + m_tx_pkt_ok =0; + m_pkt_ok=0; + m_rx_check=0; + m_no_magic=0; + m_unsup_prot=0; + m_no_id=0; + m_seq_error=0; + m_length_error=0; + m_no_ipv4_option=0; + m_hist.Reset(); +} + +static uint8_t nat_is_port_can_send(uint8_t port_id){ + uint8_t client_index = (port_id %2); + return (client_index ==0 ?1:0); +} + +bool CCPortLatency::Create(CLatencyManager * parent, + uint8_t id, + uint16_t payload_offset, + uint16_t l4_offset, + uint16_t pkt_size, + CCPortLatency * rx_port){ + m_parent = parent; + m_id = id; + m_tx_seq =0x12345678; + m_icmp_tx_seq = 1; + m_icmp_rx_seq = 0; + m_l4_offset = l4_offset; + m_payload_offset = payload_offset; + m_pkt_size = pkt_size; + m_rx_port = rx_port; + m_nat_can_send = nat_is_port_can_send(m_id); + m_nat_learn = m_nat_can_send; + m_nat_external_ip=0; + + m_hist.Create(); + reset(); + return (true); +} + +void CCPortLatency::Delete(){ + m_hist.Delete(); +} + +void CCPortLatency::update_packet(rte_mbuf_t * m, int port_id){ + uint8_t *p=rte_pktmbuf_mtod(m, uint8_t*); + bool is_client_to_server=(port_id%2==0)?true:false; + + /* update mac addr dest/src 12 bytes */ + memcpy(p,CGlobalInfo::m_options.get_dst_src_mac_addr(m_id),12); + + latency_header * h=(latency_header *)(p+m_payload_offset); + h->magic = LATENCY_MAGIC | m_id ; + h->time_stamp = os_get_hr_tick_64(); + h->seq = m_tx_seq; + m_tx_seq++; + + CLatencyPktMode *c_l_pkt_mode = m_parent->c_l_pkt_mode; + c_l_pkt_mode->update_pkt(p + m_l4_offset, is_client_to_server, m_pkt_size - m_l4_offset, &m_icmp_tx_seq); +} + + +void CCPortLatency::DumpShortHeader(FILE *fd){ + + + fprintf(fd," if| tx_ok , rx_ok , rx ,error, average , max , Jitter , max window \n"); + fprintf(fd," | , , check, , latency(usec),latency (usec) ,(usec) , \n"); + fprintf(fd," ---------------------------------------------------------------------------------------------------------------- \n"); +} + + + +std::string CCPortLatency::get_field(std::string name,float f){ + char buff[200]; + sprintf(buff,"\"%s-%d\":%.1f,",name.c_str(),m_id,f); + return (std::string(buff)); +} + + +void CCPortLatency::dump_json_v2(std::string & json ){ + char buff[200]; + sprintf(buff,"\"port-%d\": {",m_id); + json+=std::string(buff); + m_hist.dump_json("hist",json); + dump_counters_json(json); + json+="},"; +} + +void CCPortLatency::dump_json(std::string & json ){ + json += get_field("avg",m_hist.get_average_latency() ); + json += get_field("max",m_hist.get_max_latency() ); + json += get_field("c-max",m_hist.get_max_latency_last_update() ); + json += get_field("error",(float)(m_unsup_prot+m_no_magic+m_no_id+m_seq_error+m_length_error) ); + json += get_field("jitter",(float)get_jitter_usec() ); +} + + +void CCPortLatency::DumpShort(FILE *fd){ + + m_hist.update(); + fprintf(fd,"%8lu,%8lu,%10lu,%4lu,", + m_tx_pkt_ok, + m_pkt_ok, + m_rx_check, + m_unsup_prot+m_no_magic+m_no_id+m_seq_error+m_length_error+m_no_ipv4_option+m_tx_pkt_err + ); + + fprintf(fd," %8.0f ,%8.0f,%8d ", + m_hist.get_average_latency(), + m_hist.get_max_latency(), + get_jitter_usec() + ); + fprintf(fd," | "); + m_hist.DumpWinMax(fd); + +} + +#define DPL_J(f) json+=add_json(#f,f); +#define DPL_J_LAST(f) json+=add_json(#f,f,true); + +void CCPortLatency::dump_counters_json(std::string & json ){ + + json+="\"stats\" : {"; + DPL_J(m_tx_pkt_ok); + DPL_J(m_tx_pkt_err); + DPL_J(m_pkt_ok); + DPL_J(m_unsup_prot); + DPL_J(m_no_magic); + DPL_J(m_no_id); + DPL_J(m_seq_error); + DPL_J(m_length_error); + DPL_J(m_no_ipv4_option); + json+=add_json("m_jitter",get_jitter_usec()); + /* must be last */ + DPL_J_LAST(m_rx_check); + json+="}"; + + +} + +void CCPortLatency::DumpCounters(FILE *fd){ + #define DP_A1(f) if (f) fprintf(fd," %-40s : %llu \n",#f, (unsigned long long)f) + + fprintf(fd," counter \n"); + fprintf(fd," -----------\n"); + + DP_A1(m_tx_pkt_err); + DP_A1(m_tx_pkt_ok); + DP_A1(m_pkt_ok); + DP_A1(m_unsup_prot); + DP_A1(m_no_magic); + DP_A1(m_no_id); + DP_A1(m_seq_error); + DP_A1(m_length_error); + DP_A1(m_rx_check); + DP_A1(m_no_ipv4_option); + + + fprintf(fd," -----------\n"); + m_hist.Dump(fd); + fprintf(fd," %-40s : %lu \n","jitter", (ulong)get_jitter_usec()); +} + +bool CCPortLatency::dump_packet(rte_mbuf_t * m){ + fprintf(stdout," %f.03 dump packet ..\n",now_sec()); + uint8_t *p=rte_pktmbuf_mtod(m, uint8_t*); + uint16_t pkt_size=rte_pktmbuf_pkt_len(m); + utl_DumpBuffer(stdout,p,pkt_size,0); + return (0); + + + + if (pkt_size < ( sizeof(CRx_check_header)+14+20) ) { + assert(0); + } + CRx_check_header * lp=(CRx_check_header *)(p+pkt_size-sizeof(CRx_check_header)); + + lp->dump(stdout); + + + uint16_t vlan_offset=0; + if ( unlikely( CGlobalInfo::m_options.preview.get_vlan_mode_enable() ) ){ + vlan_offset=4; + } + + (void)vlan_offset; + +// utl_DumpBuffer(stdout,p,pkt_size,0); + return (0); + +} + +bool CCPortLatency::check_rx_check(rte_mbuf_t * m) { + m_rx_check++; + return (true); +} + +bool CCPortLatency::do_learn(uint32_t external_ip) { + m_nat_learn=true; + m_nat_can_send=true; + m_nat_external_ip=external_ip; + return (true); +} + +bool CCPortLatency::check_packet(rte_mbuf_t * m,CRx_check_header * & rx_p) { + CSimplePacketParser parser(m); + if ( !parser.Parse() ) { + m_unsup_prot++; // Unsupported protocol + return (false); + } + CLatencyPktMode *c_l_pkt_mode = m_parent->c_l_pkt_mode; + uint16_t pkt_size=rte_pktmbuf_pkt_len(m); + + /* check if CRC was extracted */ + if ( parser.getPktSize() == pkt_size-4) { + // CRC was not extracted by driver (VM E1000 driver issue) extract it + pkt_size=pkt_size-4; + } + + uint16_t vlan_offset=parser.m_vlan_offset; + uint8_t *p=rte_pktmbuf_mtod(m, uint8_t*); + + rx_p = (CRx_check_header *)0; + + bool is_lateancy_pkt = c_l_pkt_mode->IsLatencyPkt(parser.m_ipv4) & parser.IsLatencyPkt(parser.m_l4 + c_l_pkt_mode->l4_header_len()); + + if ( ! is_lateancy_pkt) { + +#ifdef NAT_TRACE_ + printf(" %.3f RX : got packet !!! \n",now_sec() ); +#endif + + /* ipv6+rx-check */ + if ( parser.m_ipv6 ) { + /* if we have ipv6 packet */ + if (parser.m_protocol == RX_CHECK_V6_OPT_TYPE) { + if ( get_is_rx_check_mode() ){ + m_rx_check++; + rx_p=(CRx_check_header *)((uint8_t*)parser.m_ipv6 +IPv6Header::DefaultSize); + return (true); + } + + } + m_seq_error++; + return (false); + } + + uint8_t opt_len = parser.m_ipv4->getOptionLen(); + uint8_t *opt_ptr = parser.m_ipv4->getOption(); + /* Process IP option header(s) */ + while ( opt_len != 0 ) { + switch (*opt_ptr) { + case RX_CHECK_V4_OPT_TYPE: + /* rx-check option header */ + if ( ( !get_is_rx_check_mode() ) || + (opt_len < RX_CHECK_LEN) ) { + m_seq_error++; + return (false); + } + m_rx_check++; + rx_p=(CRx_check_header *)opt_ptr; + opt_len -= RX_CHECK_LEN; + opt_ptr += RX_CHECK_LEN; + break; + case CNatOption::noIPV4_OPTION: + /* NAT learn option header */ + CNatOption *lp; + if ( ( !CGlobalInfo::is_learn_mode() ) || + (opt_len < CNatOption::noOPTION_LEN) ) { + m_seq_error++; + return (false); + } + lp = (CNatOption *)opt_ptr; + if ( !lp->is_valid_ipv4_magic() ) { + m_no_ipv4_option++; + return (false); + } + m_parent->get_nat_manager()->handle_packet_ipv4(lp,parser.m_ipv4); + opt_len -= CNatOption::noOPTION_LEN; + opt_ptr += CNatOption::noOPTION_LEN; + break; + default: + m_seq_error++; + return (false); + } // End of switch + } // End of while + + return (true); + } // End of check for non-latency packet + if ( CGlobalInfo::is_learn_mode() && (m_nat_learn ==false) ) { + do_learn(parser.m_ipv4->getSourceIp()); + } + + if ( (pkt_size-vlan_offset) != m_pkt_size ) { + m_length_error++; + return (false); + } + c_l_pkt_mode->update_recv(p + m_l4_offset + vlan_offset, &m_icmp_rx_seq, &m_icmp_tx_seq); +#ifdef LATENCY_DEBUG + c_l_pkt_mode->rcv_debug_print(p + m_l4_offset + vlan_offset); +#endif + latency_header * h=(latency_header *)(p+m_payload_offset + vlan_offset); + if ( h->seq != m_rx_seq ){ + m_seq_error++; + m_rx_seq =h->seq +1; + return (false); + }else{ + m_rx_seq++; + } + m_pkt_ok++; + uint64_t d = (os_get_hr_tick_64() - h->time_stamp ); + dsec_t ctime=ptime_convert_hr_dsec(d); + m_hist.Add(ctime); + m_jitter.calc(ctime); + return (true); +} + +void CLatencyManager::Delete(){ + m_pkt_gen.Delete(); + + if ( get_is_rx_check_mode() ) { + m_rx_check_manager.Delete(); + } + if ( CGlobalInfo::is_learn_mode() ){ + m_nat_check_manager.Delete(); + } + m_cpu_cp_u.Delete(); +} + +/* 0->1 + 1->0 + 2->3 + 3->2 +*/ +static uint8_t swap_port(uint8_t port_id){ + uint8_t offset= ((port_id>>1)<<1); + uint8_t client_index = (port_id %2); + return (offset + (client_index ^ 1)); +} + + + +bool CLatencyManager::Create(CLatencyManagerCfg * cfg){ + switch (CGlobalInfo::m_options.get_l_pkt_mode()) { + default: + case 0: + c_l_pkt_mode = (CLatencyPktModeSCTP *) new CLatencyPktModeSCTP(CGlobalInfo::m_options.get_l_pkt_mode()); + break; + case 1: + case 2: + case 3: + c_l_pkt_mode = (CLatencyPktModeICMP *) new CLatencyPktModeICMP(CGlobalInfo::m_options.get_l_pkt_mode()); + break; + } + + m_max_ports=cfg->m_max_ports; + assert (m_max_ports<=MAX_LATENCY_PORTS); + assert ((m_max_ports%2)==0); + m_port_mask =0xffffffff; + m_do_stop =false; + m_is_active =false; + m_pkt_gen.Create(c_l_pkt_mode); + int i; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + CCPortLatency * lpo=&m_ports[swap_port(i)].m_port; + + lp->m_io=cfg->m_ports[i]; + lp->m_port.Create(this, + i, + m_pkt_gen.get_payload_offset(), + m_pkt_gen.get_l4_offset(), + m_pkt_gen.get_pkt_size(),lpo ); + } + m_cps= cfg->m_cps; + m_d_time =ptime_convert_dsec_hr((1.0/m_cps)); + m_delta_sec =(1.0/m_cps); + + + if ( get_is_rx_check_mode() ) { + assert(m_rx_check_manager.Create()); + m_rx_check_manager.m_cur_time= now_sec(); + } + + + m_pkt_gen.set_ip(cfg->m_client_ip.v4,cfg->m_server_ip.v4,cfg->m_dual_port_mask); + m_cpu_cp_u.Create(&m_cpu_dp_u); + if ( CGlobalInfo::is_learn_mode() ){ + m_nat_check_manager.Create(); + } + return (true); +} + + +void CLatencyManager::send_pkt_all_ports(){ + m_start_time = os_get_hr_tick_64(); + int i; + for (i=0; i<m_max_ports; i++) { + if ( m_port_mask & (1<<i) ){ + CLatencyManagerPerPort * lp=&m_ports[i]; + if (lp->m_port.can_send_packet(i%2) ){ + rte_mbuf_t * m=m_pkt_gen.generate_pkt(i,lp->m_port.external_nat_ip()); + lp->m_port.update_packet(m, i); + +#ifdef LATENCY_DEBUG + uint8_t *p = rte_pktmbuf_mtod(m, uint8_t*); + c_l_pkt_mode->send_debug_print(p + 34); +#endif + if ( lp->m_io->tx(m) == 0 ){ + lp->m_port.m_tx_pkt_ok++; + }else{ + lp->m_port.m_tx_pkt_err++; + } + + } + } + } +} + + +void CLatencyManager::wait_for_rx_dump(){ + rte_mbuf_t * rx_pkts[64]; + int i; + while ( true ) { + rte_pause(); + rte_pause(); + rte_pause(); + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + rte_mbuf_t * m; + uint16_t cnt_p = lp->m_io->rx_burst(rx_pkts, 64); + if (cnt_p) { + int j; + for (j=0; j<cnt_p; j++) { + m=rx_pkts[j] ; + lp->m_port.dump_packet( m); + rte_pktmbuf_free(m); + } + } /*cnt_p*/ + }/* for*/ + } +} + + +void CLatencyManager::handle_rx_pkt(CLatencyManagerPerPort * lp, + rte_mbuf_t * m){ + CRx_check_header *rxc = NULL; + + lp->m_port.check_packet(m,rxc); + if ( unlikely(rxc!=NULL) ){ + m_rx_check_manager.handle_packet(rxc); + } + + rte_pktmbuf_free(m); +} + +void CLatencyManager::handle_latency_pkt_msg(uint8_t thread_id, + CGenNodeLatencyPktInfo * msg){ + + assert(msg->m_latency_offset==0xdead); + + uint8_t rx_port_index=(thread_id<<1)+(msg->m_dir&1); + assert( rx_port_index <m_max_ports ) ; + CLatencyManagerPerPort * lp=&m_ports[rx_port_index]; + handle_rx_pkt(lp,(rte_mbuf_t *)msg->m_pkt); +} + + +void CLatencyManager::run_rx_queue_msgs(uint8_t thread_id, + CNodeRing * r){ + + while ( true ) { + CGenNode * node; + if ( r->Dequeue(node)!=0 ){ + break; + } + assert(node); + + CGenNodeMsgBase * msg=(CGenNodeMsgBase *)node; + + uint8_t msg_type = msg->m_msg_type; + switch (msg_type ) { + case CGenNodeMsgBase::LATENCY_PKT: + handle_latency_pkt_msg(thread_id,(CGenNodeLatencyPktInfo *) msg); + break; + default: + printf("ERROR latency-thread message type is not valid %d \n",msg_type); + assert(0); + } + + CGlobalInfo::free_node(node); + } +} + +void CLatencyManager::try_rx_queues(){ + + CMessagingManager * rx_dp = CMsgIns::Ins()->getRxDp(); + uint8_t threads=CMsgIns::Ins()->get_num_threads(); + int ti; + for (ti=0; ti<(int)threads; ti++) { + CNodeRing * r = rx_dp->getRingDpToCp(ti); + if ( !r->isEmpty() ){ + run_rx_queue_msgs((uint8_t)ti,r); + } + } +} + + +void CLatencyManager::try_rx(){ + rte_mbuf_t * rx_pkts[64]; + int i; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + rte_mbuf_t * m; + m_cpu_dp_u.start_work(); + /* try to read 64 packets clean up the queue */ + uint16_t cnt_p = lp->m_io->rx_burst(rx_pkts, 64); + if (cnt_p) { + int j; + for (j=0; j<cnt_p; j++) { + m=rx_pkts[j] ; + handle_rx_pkt(lp,m); + } + /* commit only if there was work to do ! */ + m_cpu_dp_u.commit(); + }/* if work */ + }// all ports +} + + +void CLatencyManager::reset(){ + + int i; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + lp->m_port.reset(); + } + +} + +void CLatencyManager::start(int iter){ + m_do_stop =false; + m_is_active =false; + int cnt=0; + + double n_time; + CGenNode * node = new CGenNode(); + node->m_type = CGenNode::FLOW_SYNC; /* general stuff */ + node->m_time = now_sec()+0.007; + m_p_queue.push(node); + + node = new CGenNode(); + node->m_type = CGenNode::FLOW_PKT; /* latency */ + node->m_time = now_sec(); /* 1/cps rate */ + m_p_queue.push(node); + bool do_try_rx_queue =CGlobalInfo::m_options.preview.get_vm_one_queue_enable()?true:false; + + + while ( !m_p_queue.empty() ) { + node = m_p_queue.top(); + n_time = node->m_time; + + /* wait for event */ + while ( true ) { + double dt = now_sec() - n_time ; + if (dt> (0.0)) { + break; + } + if (do_try_rx_queue){ + try_rx_queues(); + } + try_rx(); + rte_pause(); + } + + switch (node->m_type) { + case CGenNode::FLOW_SYNC: + if ( CGlobalInfo::is_learn_mode() ) { + m_nat_check_manager.handle_aging(); + } + + m_p_queue.pop(); + node->m_time += SYNC_TIME_OUT; + m_p_queue.push(node); + + break; + case CGenNode::FLOW_PKT: + m_cpu_dp_u.start_work(); + send_pkt_all_ports(); + m_p_queue.pop(); + node->m_time += m_delta_sec; + m_p_queue.push(node); + m_cpu_dp_u.commit(); + break; + } + + /* this will be called every sync which is 1msec */ + if ( m_do_stop ) { + break; + } + if ( iter>0 ){ + if ( ( cnt>iter) ){ + printf("stop due iter %d\n",iter); + break; + } + } + cnt++; + } + + /* free all nodes in the queue */ + while (!m_p_queue.empty()) { + node = m_p_queue.top(); + m_p_queue.pop(); + delete node; + } + + printf(" latency daemon has stopped\n"); + if ( get_is_rx_check_mode() ) { + m_rx_check_manager.tw_drain(); + } + +} + +void CLatencyManager::stop(){ + m_do_stop =true; +} + +bool CLatencyManager::is_active(){ + return (m_is_active); +} + + +double CLatencyManager::get_max_latency(){ + double l=0.0; + int i; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + if ( l <lp->m_port.m_hist.get_max_latency() ){ + l=lp->m_port.m_hist.get_max_latency(); + } + } + return (l); +} + +double CLatencyManager::get_avr_latency(){ + double l=0.0; + int i; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + if ( l <lp->m_port.m_hist.get_average_latency() ){ + l=lp->m_port.m_hist.get_average_latency(); + } + } + return (l); +} + +uint64_t CLatencyManager::get_total_pkt(){ + int i; + uint64_t t=0; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + t+=lp->m_port.m_tx_pkt_ok ; + } + return t; +} + +uint64_t CLatencyManager::get_total_bytes(){ + int i; + uint64_t t=0; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + t+=lp->m_port.m_tx_pkt_ok* (m_pkt_gen.get_pkt_size()+4); + } + return t; + +} + + +bool CLatencyManager::is_any_error(){ + int i; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + if ( lp->m_port.is_any_err() ){ + return (true); + } + } + return (false); +} + + +void CLatencyManager::dump_json(std::string & json ){ + json="{\"name\":\"trex-latecny\",\"type\":0,\"data\":{"; + int i; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + lp->m_port.dump_json(json); + } + + json+="\"unknown\":0}}" ; + +} + +void CLatencyManager::dump_json_v2(std::string & json ){ + json="{\"name\":\"trex-latecny-v2\",\"type\":0,\"data\":{"; + json+=add_json("cpu_util",m_cpu_cp_u.GetVal()); + + int i; + for (i=0; i<m_max_ports; i++) { + CLatencyManagerPerPort * lp=&m_ports[i]; + lp->m_port.dump_json_v2(json); + } + + json+="\"unknown\":0}}" ; + +} + +void CLatencyManager::DumpRxCheck(FILE *fd){ + if ( get_is_rx_check_mode() ) { + fprintf(fd," rx checker : \n"); + m_rx_check_manager.DumpShort(fd); + m_rx_check_manager.Dump(fd); + } +} + +void CLatencyManager::DumpShortRxCheck(FILE *fd){ + if ( get_is_rx_check_mode() ) { + m_rx_check_manager.DumpShort(fd); + } +} + +void CLatencyManager::rx_check_dump_json(std::string & json){ + if ( get_is_rx_check_mode() ) { + m_rx_check_manager.dump_json(json ); + } +} + +void CLatencyManager::update(){ + m_cpu_cp_u.Update() ; +} + +void CLatencyManager::DumpShort(FILE *fd){ + int i; + fprintf(fd," Cpu Utilization : %2.1f %% \n",m_cpu_cp_u.GetVal()); + CCPortLatency::DumpShortHeader(fd); + for (i=0; i<m_max_ports; i++) { + fprintf(fd," %d | ",i); + CLatencyManagerPerPort * lp=&m_ports[i]; + lp->m_port.DumpShort(fd); + fprintf(fd,"\n"); + } + + +} + +void CLatencyManager::Dump(FILE *fd){ + int i; + fprintf(fd," cpu : %2.1f %% \n",m_cpu_cp_u.GetVal()); + for (i=0; i<m_max_ports; i++) { + fprintf(fd," port %d \n",i); + fprintf(fd," -----------------\n"); + CLatencyManagerPerPort * lp=&m_ports[i]; + lp->m_port.DumpCounters(fd); + } +} + +void CLatencyManager::DumpRxCheckVerification(FILE *fd, + uint64_t total_tx_rx_check){ + if ( !get_is_rx_check_mode() ) { + fprintf(fd," rx_checker is disabled \n"); + return; + } + fprintf(fd," rx_check Tx : %llu \n", (unsigned long long)total_tx_rx_check); + fprintf(fd," rx_check Rx : %llu \n", (unsigned long long)m_rx_check_manager.getTotalRx() ); + fprintf(fd," rx_check verification :" ); + if (m_rx_check_manager.getTotalRx() == total_tx_rx_check) { + fprintf(fd," OK \n" ); + }else{ + fprintf(fd," FAIL \n" ); + } +} + +uint8_t CLatencyPktModeICMP::getPacketLen() {return sizeof(icmp_pkt);} +const uint8_t *CLatencyPktModeICMP::getPacketData() {return icmp_pkt;} +void CLatencyPktModeICMP::rcv_debug_print(uint8_t *pkt) { + ICMPHeader *m_icmp = (ICMPHeader *)pkt; + printf ("received latency ICMP packet code:%d seq:%x\n" + , m_icmp->getType(), m_icmp->getSeqNum()); +}; + +void CLatencyPktModeICMP::send_debug_print(uint8_t *pkt) { + ICMPHeader *m_icmp = (ICMPHeader *)pkt; + printf ("Sending latency ICMP packet code:%d seq:%d\n", m_icmp->getType(), m_icmp->getSeqNum()); +} + +void CLatencyPktModeICMP::update_pkt(uint8_t *pkt, bool is_client_to_server, uint16_t l4_len, uint16_t *tx_seq) { + ICMPHeader * m_icmp =(ICMPHeader *)(pkt); + + if (m_submode == L_PKT_SUBMODE_0_SEQ) { + m_icmp->setSeqNum(0); + } else { + m_icmp->setSeqNum(*tx_seq); + (*tx_seq)++; + } + + if ((!is_client_to_server) && (m_submode == L_PKT_SUBMODE_REPLY)) { + m_icmp->setType(0); // echo reply + } else { + m_icmp->setType(8); // echo request + } + // ICMP checksum is calculated on payload + ICMP header + m_icmp->updateCheckSum(l4_len); + +} + +bool CLatencyPktModeICMP::IsLatencyPkt(IPHeader *ip) { + if (!ip) + return false; + if (ip->getProtocol() != 0x1) + return false; + return true; +}; + +void CLatencyPktModeICMP::update_recv(uint8_t *pkt, uint16_t *r_seq, uint16_t *t_seq) { + ICMPHeader *m_icmp = (ICMPHeader *)(pkt); + *r_seq = m_icmp->getSeqNum(); + // handle wrap around, so can_send_packet will allow us to send + if (*r_seq == 0) + *t_seq = 0; +} + + +uint8_t CLatencyPktModeSCTP::getPacketLen() {return sizeof(sctp_pkt);} +const uint8_t *CLatencyPktModeSCTP::getPacketData() {return sctp_pkt;} +void CLatencyPktModeSCTP::rcv_debug_print(uint8_t *pkt) {printf("Received latency SCTP packet\n");} +void CLatencyPktModeSCTP::send_debug_print(uint8_t *pkt) {printf("Sending latency SCTP packet\n"); + // utl_DumpBuffer(stdout,pkt-20,28,0); +} +void CLatencyPktModeSCTP::update_pkt(uint8_t *pkt, bool is_client_to_server, uint16_t l4_len, uint16_t *tx_seq) {} +bool CLatencyPktModeSCTP::IsLatencyPkt(IPHeader *ip) { + if (!ip) { + return false; + } + if (ip->getProtocol() != 0x84) { + return false; + } + return true; +}; +void CLatencyPktModeSCTP::update_recv(uint8_t *pkt, uint16_t *r_seq, uint16_t *t_seq) {} diff --git a/src/latency.h b/src/latency.h new file mode 100644 index 00000000..59481a59 --- /dev/null +++ b/src/latency.h @@ -0,0 +1,386 @@ +#ifndef LATENCY_H +#define LATENCY_H +/* + Hanoh Haim + Ido Barnea + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#include <bp_sim.h> + +#define L_PKT_SUBMODE_NO_REPLY 1 +#define L_PKT_SUBMODE_REPLY 2 +#define L_PKT_SUBMODE_0_SEQ 3 + +class CLatencyPktInfo { +public: + void Create(class CLatencyPktMode *m_l_pkt_info); + void Delete(); + void set_ip(uint32_t src, + uint32_t dst, + uint32_t dual_port_mask); + rte_mbuf_t * generate_pkt(int port_id,uint32_t extern_ip=0); + + CGenNode * getNode(){ + return (&m_dummy_node); + } + + uint16_t get_payload_offset(void){ + return ( m_pkt_indication.getFastPayloadOffset()); + } + + uint16_t get_l4_offset(void){ + return ( m_pkt_indication.getFastTcpOffset()); + } + + uint16_t get_pkt_size(void){ + return ( m_packet->pkt_len ); + } + +private: + ipaddr_t m_client_ip; + ipaddr_t m_server_ip; + uint32_t m_dual_port_mask; + + CGenNode m_dummy_node; + CFlowPktInfo m_pkt_info; + CPacketIndication m_pkt_indication; + CCapPktRaw * m_packet; +}; + + +#define LATENCY_MAGIC 0x12345600 + +struct latency_header { + + uint64_t time_stamp; + uint32_t magic; + uint32_t seq; + + uint8_t get_id(){ + return( magic & 0xff); + } +}; + +class CSimplePacketParser { +public: + + CSimplePacketParser(rte_mbuf_t * m){ + m_m=m; + } + + bool Parse(); + uint8_t getTTl(); + uint16_t getPktSize(); + + inline bool IsLatencyPkt(uint8_t *p) { + latency_header * h=(latency_header *)(p); + if ( (h->magic & 0xffffff00) != LATENCY_MAGIC ){ + return (false); + } + + return true; + } + + +public: + IPHeader * m_ipv4; + IPv6Header * m_ipv6; + uint8_t m_protocol; + uint16_t m_vlan_offset; + uint16_t m_option_offset; + uint8_t * m_l4; +private: + rte_mbuf_t * m_m ; +}; + + + +class CLatencyManager ; + +// per port +class CCPortLatency { +public: + bool Create(CLatencyManager * parent, + uint8_t id, + uint16_t offset, + uint16_t l4_offset, + uint16_t pkt_size, + CCPortLatency * rx_port + ); + void Delete(); + void reset(); + bool can_send_packet(int direction){ + // in icmp_reply mode, can send response from server, only after we got the relevant request + // if we got request, we are sure to have NAT translation in place already. + if ((CGlobalInfo::m_options.m_l_pkt_mode == L_PKT_SUBMODE_REPLY) && (direction == 1)) { + if (m_icmp_tx_seq <= m_icmp_rx_seq) + return(true); + else + return(false); + } + + if ( !CGlobalInfo::is_learn_mode() ) { + return(true); + } + return ( m_nat_can_send ); + } + uint32_t external_nat_ip(){ + return (m_nat_external_ip); + } + + void update_packet(rte_mbuf_t * m, int port_id); + + bool do_learn(uint32_t external_ip); + + bool check_packet(rte_mbuf_t * m, + CRx_check_header * & rx_p); + bool check_rx_check(rte_mbuf_t * m); + + + bool dump_packet(rte_mbuf_t * m); + + void DumpCounters(FILE *fd); + void dump_counters_json(std::string & json ); + + void DumpShort(FILE *fd); + void dump_json(std::string & json ); + void dump_json_v2(std::string & json ); + + uint32_t get_jitter_usec(void){ + return ((uint32_t)(m_jitter.get_jitter()*1000000.0)); + } + + + static void DumpShortHeader(FILE *fd); + + bool is_any_err(){ + if ( (m_tx_pkt_ok == m_rx_port->m_pkt_ok ) && + + ((m_unsup_prot+ + m_no_magic+ + m_no_id+ + m_seq_error+ + m_length_error+m_no_ipv4_option+m_tx_pkt_err)==0) ) { + return (false); + } + return (true); + } + + uint16_t get_icmp_tx_seq() {return m_icmp_tx_seq;} + uint16_t get_icmp_rx_seq() {return m_icmp_rx_seq;} + +private: + std::string get_field(std::string name,float f); + + +private: + CLatencyManager * m_parent; + CCPortLatency * m_rx_port; /* corespond rx port */ + bool m_nat_learn; + bool m_nat_can_send; + uint32_t m_nat_external_ip; + + uint32_t m_tx_seq; + uint32_t m_rx_seq; + + uint8_t m_pad; + uint8_t m_id; + uint16_t m_payload_offset; + uint16_t m_l4_offset; + + uint16_t m_pkt_size; + // following two variables are for the latency ICMP reply mode. + // if we want to pass through firewall, we want to send reply only after we got request with same seq num + // ICMP seq num of next packet we will transmit + uint16_t m_icmp_tx_seq; + // ICMP seq num of last request we got + uint16_t m_icmp_rx_seq; + uint16_t pad1[1]; + +public: + uint64_t m_tx_pkt_ok; + uint64_t m_tx_pkt_err; + + uint64_t m_pkt_ok; + uint64_t m_unsup_prot; + uint64_t m_no_magic; + uint64_t m_no_id; + uint64_t m_seq_error; + uint64_t m_rx_check; + uint64_t m_no_ipv4_option; + + + uint64_t m_length_error; + CTimeHistogram m_hist; /* all window */ + CJitter m_jitter; +}; + + +class CPortLatencyHWBase { +public: + virtual int tx(rte_mbuf_t * m)=0; + virtual rte_mbuf_t * rx()=0; + virtual uint16_t rx_burst(struct rte_mbuf **rx_pkts, + uint16_t nb_pkts){ + return(0); + } +}; + + +class CLatencyManagerCfg { +public: + CLatencyManagerCfg (){ + m_max_ports=0; + m_cps=0.0; + m_client_ip.v4=0x10000000; + m_server_ip.v4=0x20000000; + m_dual_port_mask=0x01000000; + } + uint32_t m_max_ports; + double m_cps;// CPS + CPortLatencyHWBase * m_ports[MAX_LATENCY_PORTS]; + ipaddr_t m_client_ip; + ipaddr_t m_server_ip; + uint32_t m_dual_port_mask; + +}; + + +class CLatencyManagerPerPort { +public: + CCPortLatency m_port; + CPortLatencyHWBase * m_io; + uint32_t m_flag; + +}; + + +class CLatencyPktMode { + public: + uint8_t m_submode; + CLatencyPktMode(uint8_t submode) {m_submode = submode;} + virtual uint8_t getPacketLen() = 0; + virtual const uint8_t *getPacketData() = 0; + virtual void rcv_debug_print(uint8_t *pkt) = 0; + virtual void send_debug_print(uint8_t *pkt) = 0; + virtual void update_pkt(uint8_t *pkt, bool is_client_to_server, uint16_t l4_len, uint16_t *tx_seq) = 0; + virtual bool IsLatencyPkt(IPHeader *ip) = 0; + uint8_t l4_header_len() {return 8;} + virtual void update_recv(uint8_t *pkt, uint16_t *r_seq, uint16_t *t_seq) = 0; + virtual uint8_t getProtocol() = 0; + virtual ~CLatencyPktMode() {} +}; + +class CLatencyPktModeICMP: public CLatencyPktMode { + public: + CLatencyPktModeICMP(uint8_t submode) : CLatencyPktMode(submode) {} + uint8_t getPacketLen(); + const uint8_t *getPacketData(); + void rcv_debug_print(uint8_t *); + void send_debug_print(uint8_t *); + void update_pkt(uint8_t *pkt, bool is_client_to_server, uint16_t l4_len, uint16_t *tx_seq); + bool IsLatencyPkt(IPHeader *ip); + void update_recv(uint8_t *pkt, uint16_t *r_seq, uint16_t *t_seq); + uint8_t getProtocol() {return 0x1;} +}; + +class CLatencyPktModeSCTP: public CLatencyPktMode { + public: + CLatencyPktModeSCTP(uint8_t submode) : CLatencyPktMode(submode) {} + uint8_t getPacketLen(); + const uint8_t *getPacketData(); + void rcv_debug_print(uint8_t *); + void send_debug_print(uint8_t *); + void update_pkt(uint8_t *pkt, bool is_client_to_server, uint16_t l4_len, uint16_t *tx_seq); + bool IsLatencyPkt(IPHeader *ip); + void update_recv(uint8_t *pkt, uint16_t *r_seq, uint16_t *t_seq); + uint8_t getProtocol() {return 0x84;} +}; + +class CLatencyManager { +public: + bool Create(CLatencyManagerCfg * cfg); + void Delete(); + void reset(); + void start(int iter); + void stop(); + bool is_active(); + void set_ip(uint32_t client_ip, + uint32_t server_ip, + uint32_t mask_dual_port){ + m_pkt_gen.set_ip(client_ip,server_ip,mask_dual_port); + } + void Dump(FILE *fd); // dump all + void DumpShort(FILE *fd); // dump short histogram of latency + + void DumpRxCheck(FILE *fd); // dump all + void DumpShortRxCheck(FILE *fd); // dump short histogram of latency + void rx_check_dump_json(std::string & json); + uint16_t get_latency_header_offset(){ + return ( m_pkt_gen.get_payload_offset() ); + } + void update(); + void dump_json(std::string & json ); // dump to json + void dump_json_v2(std::string & json ); + void DumpRxCheckVerification(FILE *fd,uint64_t total_tx_rx_check); + void set_mask(uint32_t mask){ + m_port_mask=mask; + } + double get_max_latency(void); + double get_avr_latency(void); + bool is_any_error(); + uint64_t get_total_pkt(); + uint64_t get_total_bytes(); + CNatRxManager * get_nat_manager(){ + return ( &m_nat_check_manager ); + } + CLatencyPktMode *c_l_pkt_mode; + +private: + void send_pkt_all_ports(); + void try_rx(); + void try_rx_queues(); + void run_rx_queue_msgs(uint8_t thread_id, + CNodeRing * r); + void wait_for_rx_dump(); + void handle_rx_pkt(CLatencyManagerPerPort * lp, + rte_mbuf_t * m); + /* messages handlers */ + void handle_latency_pkt_msg(uint8_t thread_id, + CGenNodeLatencyPktInfo * msg); + + pqueue_t m_p_queue; /* priorty queue */ + bool m_is_active; + CLatencyPktInfo m_pkt_gen; + CLatencyManagerPerPort m_ports[MAX_LATENCY_PORTS]; + uint64_t m_d_time; // calc tick betwen sending + double m_cps; + double m_delta_sec; + uint64_t m_start_time; // calc tick betwen sending + uint32_t m_port_mask; + uint32_t m_max_ports; + RxCheckManager m_rx_check_manager; + CNatRxManager m_nat_check_manager; + CCpuUtlDp m_cpu_dp_u; + CCpuUtlCp m_cpu_cp_u; + volatile bool m_do_stop __rte_cache_aligned ; +}; + +#endif + diff --git a/src/main_dpdk.cpp b/src/main_dpdk.cpp index 3e5418b9..8c9eb914 100755 --- a/src/main_dpdk.cpp +++ b/src/main_dpdk.cpp @@ -51,6 +51,7 @@ limitations under the License. #include <rte_mbuf.h> #include <rte_random.h> #include "bp_sim.h" +#include "latency.h" #include "os_time.h" #include <common/arg/SimpleGlob.h> #include <common/arg/SimpleOpt.h> @@ -77,6 +78,7 @@ extern "C" { #include "utl_term_io.h" #include "msg_manager.h" #include "platform_cfg.h" +#include "latency.h" #include <internal_api/trex_platform_api.h> @@ -118,13 +120,10 @@ static inline int get_is_latency_thread_enable(){ return (CGlobalInfo::m_options.is_latency_enabled() ?1:0); } - - struct port_cfg_t; class CPhyEthIF; class CPhyEthIFStats ; - class CTRexExtendedDriverBase { public: @@ -436,12 +435,6 @@ static inline int get_min_sample_rate(void){ return ( get_ex_drv()->get_min_sample_rate()); } - - - - - - #define MAX_DPDK_ARGS 40 static CPlatformYamlInfo global_platform_cfg_info; static int global_dpdk_args_num ; @@ -450,9 +443,6 @@ static char global_cores_str[100]; static char global_prefix_str[100]; static char global_loglevel_str[20]; - - - // cores =0==1,1*2,2,3,4,5,6 // An enum for all the option types enum { OPT_HELP, @@ -487,6 +477,7 @@ enum { OPT_HELP, OPT_IPV6, OPT_LEARN, OPT_LEARN_VERIFY, + OPT_L_PKT_MODE, OPT_NO_FLOW_CONTROL, OPT_RX_CHECK_HOPS, OPT_MAC_FILE, @@ -499,9 +490,6 @@ enum { OPT_HELP, }; - - - /* these are the argument types: SO_NONE -- no argument needed SO_REQ_SEP -- single required argument @@ -549,6 +537,7 @@ static CSimpleOpt::SOption parser_options[] = { OPT_IPV6, "--ipv6", SO_NONE }, { OPT_LEARN, "--learn", SO_NONE }, { OPT_LEARN_VERIFY, "--learn-verify", SO_NONE }, + { OPT_L_PKT_MODE, "--l-pkt-mode", SO_REQ_SEP }, { OPT_NO_FLOW_CONTROL, "--no-flow-control", SO_NONE }, { OPT_VLAN, "--vlan", SO_NONE }, { OPT_MAC_FILE, "--mac", SO_REQ_SEP }, @@ -622,7 +611,11 @@ static int usage(){ printf(" --learn : Work in NAT environments, learn the dynamic NAT translation and ALG \n"); printf(" --learn-verify : Learn the translation, but intended for verification of the mechanism in cases that NAT does not exist \n"); printf(" \n"); - + printf(" --l-pkt-mode [0-3] : Set mode for sending latency packets.\n"); + printf(" 0 (default) send SCTP packets \n"); + printf(" 1 Send ICMP request packets \n"); + printf(" 2 Send ICMP requests from client side, and response from server side (for working with firewall) \n"); + printf(" 3 Send ICMP requests with sequence ID 0 from both sides \n"); printf(" -v [1-3] : verbose mode ( works only on the debug image ! ) \n"); printf(" 1 show only stats \n"); printf(" 2 run preview do not write to file \n"); @@ -685,7 +678,7 @@ static int usage(){ printf(" limitations under the License. \n"); printf(" \n"); printf(" Open Source Components / Libraries \n"); - printf(" DPDK (BSD) \n"); + printf(" DPDK (BSD) \n"); printf(" YAML-CPP (BSD) \n"); printf(" JSONCPP (MIT) \n"); printf(" \n"); @@ -696,6 +689,7 @@ static int usage(){ printf(" User : %s \n",VERSION_USER); printf(" Date : %s , %s \n",get_build_date(),get_build_time()); printf(" Uuid : %s \n",VERSION_UIID); + printf(" Git SHA : %s \n",VERSION_GIT_SHA); return (0); } @@ -782,6 +776,11 @@ static int parse_options(int argc, char *argv[], CParserOption* po, bool first_t po->preview.set_lean_and_verify_mode_enable(true); break; + case OPT_L_PKT_MODE : + sscanf(args.OptionArg(),"%d", &tmp_data); + po->m_l_pkt_mode=(uint8_t)tmp_data; + break; + case OPT_REAL_TIME : printf(" warning -r is deprecated, real time is not needed any more , it is the default \n"); po->preview.setRealTime(true); @@ -1448,11 +1447,11 @@ void CPhyEthIF::configure(uint16_t nb_rx_queue, /* -rx-queue 0 - default- all traffic no SCTP +rx-queue 0 - default- all traffic not goint to queue 1 will be drop as queue is disable -rx-queue 1 - SCTP traffic will go to here +rx-queue 1 - Latency measurement packets will go here pci_reg_write(IXGBE_L34T_IMIR(0),(1<<21)); @@ -1875,7 +1874,7 @@ public: bool process_rx_pkt(pkt_dir_t dir,rte_mbuf_t * m); - virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, rte_mbuf_t *m); + virtual int update_mac_addr_from_global_cfg(pkt_dir_t dir, uint8_t * p); virtual pkt_dir_t port_id_to_dir(uint8_t port_id); @@ -1918,8 +1917,6 @@ public: virtual int send_node(CGenNode * node); }; - - bool CCoreEthIF::Create(uint8_t core_id, uint16_t tx_client_queue_id, CPhyEthIF * tx_client_port, @@ -1939,54 +1936,6 @@ bool CCoreEthIF::Create(uint8_t core_id, return (true); } -bool CCoreEthIF::process_rx_pkt(pkt_dir_t dir, - rte_mbuf_t * m){ - - CSimplePacketParser parser(m); - if ( !parser.Parse() ){ - return false; - } - bool send=false; - if ( parser.IsLatencyPkt() ){ - send=true; - - }else{ - if ( get_is_rx_filter_enable() ){ - uint8_t max_ttl = 0xff - get_rx_check_hops(); - uint8_t pkt_ttl = parser.getTTl(); - if ( (pkt_ttl==max_ttl) || (pkt_ttl==(max_ttl-1) ) ) { - send=true; - } - } - } - - - if (send) { - CGenNodeLatencyPktInfo * node=(CGenNodeLatencyPktInfo * )CGlobalInfo::create_node(); - if ( node ) { - node->m_msg_type = CGenNodeMsgBase::LATENCY_PKT; - node->m_dir = dir; - node->m_latency_offset = 0xdead; - node->m_pkt = m; - if ( m_ring_to_rx->Enqueue((CGenNode*)node)==0 ){ - }else{ - CGlobalInfo::free_node((CGenNode *)node); - send=false; - } - - #ifdef LATENCY_QUEUE_TRACE_ - printf("rx to cp --\n"); - rte_pktmbuf_dump(stdout,m, rte_pktmbuf_pkt_len(m)); - #endif - }else{ - send=false; - } - } - return (send); -} - - - void CCoreEthIF::flush_rx_queue(void){ pkt_dir_t dir ; bool is_latency=get_is_latency_thread_enable(); @@ -2020,7 +1969,6 @@ void CCoreEthIF::flush_rx_queue(void){ } } - int CCoreEthIF::flush_tx_queue(void){ /* flush both sides */ pkt_dir_t dir ; @@ -2040,7 +1988,6 @@ int CCoreEthIF::flush_tx_queue(void){ return (0); } - void CCoreEthIF::GetCoreCounters(CVirtualIFPerSideStats *stats){ stats->Clear(); pkt_dir_t dir ; @@ -2130,6 +2077,8 @@ int CCoreEthIF::send_pkt(CCorePerPort * lp_port, CVirtualIFPerSideStats * lp_stats ){ + //rte_pktmbuf_dump(stdout,m, rte_pktmbuf_pkt_len(m)); + lp_stats->m_tx_pkt +=1; lp_stats->m_tx_bytes += (rte_pktmbuf_pkt_len(m)+4); @@ -2186,12 +2135,18 @@ int CCoreEthIFStateless::send_node(CGenNode * no){ /* check that we have mbuf */ rte_mbuf_t * m=node_sl->get_cache_mbuf(); - assert( m ); pkt_dir_t dir=(pkt_dir_t)node_sl->get_mbuf_cache_dir(); CCorePerPort * lp_port=&m_ports[dir]; CVirtualIFPerSideStats * lp_stats = &m_stats[dir]; - rte_pktmbuf_refcnt_update(m,1); + if (m) { + /* cache case */ + rte_pktmbuf_refcnt_update(m,1); + }else{ + m=node_sl->alloc_node_with_vm(); + assert(m); + } send_pkt(lp_port,m,lp_stats); + return (0); }; @@ -2290,14 +2245,12 @@ int CCoreEthIF::send_node(CGenNode * node){ } -int CCoreEthIF::update_mac_addr_from_global_cfg(pkt_dir_t dir, - rte_mbuf_t *m){ - assert(m); +int CCoreEthIF::update_mac_addr_from_global_cfg(pkt_dir_t dir, uint8_t * p){ + assert(p); assert(dir<2); + CCorePerPort * lp_port=&m_ports[dir]; - uint8_t *p=rte_pktmbuf_mtod(m, uint8_t*); uint8_t p_id=lp_port->m_port->get_port_id(); - memcpy(p,CGlobalInfo::m_options.get_dst_src_mac_addr(p_id),12); return (0); } @@ -2854,7 +2807,7 @@ private: int create_pkt(uint8_t *pkt,int pkt_size); int create_udp_pkt(); - int create_sctp_pkt(); + int create_icmp_pkt(); @@ -2972,7 +2925,6 @@ int CGlobalTRex::test_send(){ int i; //set_promisc_all(true); - //create_sctp_pkt(); create_udp_pkt(); CRx_check_header rx_check_header; @@ -2995,18 +2947,7 @@ int CGlobalTRex::test_send(){ //test_send_pkts(m_latency_tx_queue_id,1,2); //test_send_pkts(m_latency_tx_queue_id,1,3); test_send_pkts(0,1,0); - test_send_pkts(0,1,1); - //test_send_pkts(2,1,0); - - - //test_send_pkts(0,1,1); - //test_send_pkts(0,1,2); - //test_send_pkts(0,1,3); - - /*test_send_pkts(2,1,0); - test_send_pkts(2,1,1); - test_send_pkts(2,1,2); - test_send_pkts(2,1,3);*/ + test_send_pkts(0,2,1); /*delay(1000); fprintf(stdout," --------------------------------\n"); @@ -3091,22 +3032,21 @@ const uint8_t udp_pkt[]={ }; -const uint8_t sctp_pkt1[]={ - +const uint8_t icmp_pkt1[]={ 0x00,0x00,0x00,0x01,0x00,0x00, 0x00,0x00,0x00,0x01,0x00,0x00, 0x08,0x00, 0x45,0x02,0x00,0x30, 0x00,0x00,0x40,0x00, - 0x40,0x84,0xbd,0x04, - 0x01,0x01,0x01,0x01, //sIP - 0x02,0x02,0x02,0x02, //DIP - - 0x80,0x44,//SPORT - 0x00,0x50,//DPORT + 0xaa,0x01,0xbd,0x04, + 0x9b,0xe6,0x18,0x9b, //SIP + 0xcb,0xff,0xfc,0xc2, //DIP - 0x00,0x00,0x00,0x00, //checksum + 0x08, 0x00, + 0x01, 0x02, //checksum + 0xaa, 0xbb, // id + 0x00, 0x00, // Sequence number 0x11,0x22,0x33,0x44, // magic 0x00,0x00,0x00,0x00, //64 bit counter @@ -3114,8 +3054,7 @@ const uint8_t sctp_pkt1[]={ 0x00,0x01,0xa0,0x00, //seq 0x00,0x00,0x00,0x00, -}; - +}; @@ -3144,8 +3083,8 @@ int CGlobalTRex::create_udp_pkt(){ return (create_pkt((uint8_t*)udp_pkt,sizeof(udp_pkt))); } -int CGlobalTRex::create_sctp_pkt(){ - return (create_pkt((uint8_t*)sctp_pkt1,sizeof(sctp_pkt1))); +int CGlobalTRex::create_icmp_pkt(){ + return (create_pkt((uint8_t*)icmp_pkt1,sizeof(icmp_pkt1))); } @@ -3392,7 +3331,7 @@ int CGlobalTRex::ixgbe_start(void){ m_cores_to_dual_ports+1, &m_port_cfg.m_port_conf); - /* the latency queue for SCTP */ + /* the latency queue for latency measurement packets */ m_latency_tx_queue_id= m_cores_to_dual_ports; socket_id_t socket_id = CGlobalInfo::m_socket.port_to_socket((port_id_t)i); @@ -3409,7 +3348,7 @@ int CGlobalTRex::ixgbe_start(void){ /* set the filter queue */ _if->set_rx_queue(1); - /* sctp ring is 1 */ + /* latency measurement ring is 1 */ _if->rx_queue_setup(1, RTE_TEST_RX_LATENCY_DESC_DEFAULT, socket_id, @@ -4110,13 +4049,15 @@ int CGlobalTRex::run_in_master(){ break; } - m_mg.rx_check_dump_json(json ); - m_zmq_publisher.publish_json(json); - }/* ex checked */ } + if ( get_is_rx_check_mode() ) { + m_mg.rx_check_dump_json(json ); + m_zmq_publisher.publish_json(json); + } + /* backward compatible */ m_mg.dump_json(json ); m_zmq_publisher.publish_json(json); @@ -4351,6 +4292,54 @@ int CGlobalTRex::start_send_master(){ static CGlobalTRex g_trex; +bool CCoreEthIF::process_rx_pkt(pkt_dir_t dir, + rte_mbuf_t * m){ + + CSimplePacketParser parser(m); + if ( !parser.Parse() ){ + return false; + } + bool send=false; + CLatencyPktMode *c_l_pkt_mode = g_trex.m_mg.c_l_pkt_mode; + bool is_lateancy_pkt = c_l_pkt_mode->IsLatencyPkt(parser.m_ipv4) & parser.IsLatencyPkt(parser.m_l4 + c_l_pkt_mode->l4_header_len()); + + if (is_lateancy_pkt){ + send=true; + }else{ + if ( get_is_rx_filter_enable() ){ + uint8_t max_ttl = 0xff - get_rx_check_hops(); + uint8_t pkt_ttl = parser.getTTl(); + if ( (pkt_ttl==max_ttl) || (pkt_ttl==(max_ttl-1) ) ) { + send=true; + } + } + } + + + if (send) { + CGenNodeLatencyPktInfo * node=(CGenNodeLatencyPktInfo * )CGlobalInfo::create_node(); + if ( node ) { + node->m_msg_type = CGenNodeMsgBase::LATENCY_PKT; + node->m_dir = dir; + node->m_latency_offset = 0xdead; + node->m_pkt = m; + if ( m_ring_to_rx->Enqueue((CGenNode*)node)==0 ){ + }else{ + CGlobalInfo::free_node((CGenNode *)node); + send=false; + } + + #ifdef LATENCY_QUEUE_TRACE_ + printf("rx to cp --\n"); + rte_pktmbuf_dump(stdout,m, rte_pktmbuf_pkt_len(m)); + #endif + }else{ + send=false; + } + } + return (send); +} + TrexStateless * get_stateless_obj() { return g_trex.m_trex_stateless; @@ -4476,8 +4465,7 @@ int update_global_info_from_platform_file(){ CGlobalInfo::m_memory_cfg.set(cg->m_memory,mul); CGlobalInfo::m_memory_cfg.set_number_of_dp_cors( - CGlobalInfo::m_options.get_number_of_dp_cores_needed() ); - + CGlobalInfo::m_options.get_number_of_dp_cores_needed() ); return (0); } @@ -4736,15 +4724,19 @@ int CTRexExtendedDriverBase1G::wait_for_stable_link(){ } int CTRexExtendedDriverBase1G::configure_drop_queue(CPhyEthIF * _if){ + uint8_t protocol; + if (CGlobalInfo::m_options.m_l_pkt_mode == 0) { + protocol = IPPROTO_SCTP; + } else { + protocol = IPPROTO_ICMP; + } + _if->pci_reg_write( E1000_RXDCTL(0) , 0); /* enable filter to pass packet to rx queue 1 */ - _if->pci_reg_write( E1000_IMIR(0), 0x00020000); - _if->pci_reg_write( E1000_IMIREXT(0), 0x00081000); - - _if->pci_reg_write( E1000_TTQF(0), 0x00000084 /* protocol */ + _if->pci_reg_write( E1000_TTQF(0), protocol | 0x00008100 /* enable */ | 0xE0010000 /* RX queue is 1 */ ); @@ -4772,11 +4764,13 @@ int CTRexExtendedDriverBase1G::configure_rx_filter_rules(CPhyEthIF * _if){ */ int i; // IPv4: bytes being compared are {TTL, Protocol} - uint16_t ff_rules_v4[4]={ + uint16_t ff_rules_v4[6]={ (uint16_t)(0xFF06 - v4_hops), (uint16_t)(0xFE11 - v4_hops), (uint16_t)(0xFF11 - v4_hops), (uint16_t)(0xFE06 - v4_hops), + (uint16_t)(0xFF01 - v4_hops), + (uint16_t)(0xFE01 - v4_hops), } ; // IPv6: bytes being compared are {NextHdr, HopLimit} uint16_t ff_rules_v6[2]={ @@ -4917,24 +4911,30 @@ int CTRexExtendedDriverBase10G::configure_rx_filter_rules(CPhyEthIF * _if){ // IPv4: bytes being compared are {TTL, Protocol} - uint16_t ff_rules_v4[4]={ + uint16_t ff_rules_v4[6]={ (uint16_t)(0xFF11 - v4_hops), (uint16_t)(0xFE11 - v4_hops), (uint16_t)(0xFF06 - v4_hops), (uint16_t)(0xFE06 - v4_hops), + (uint16_t)(0xFF01 - v4_hops), + (uint16_t)(0xFE01 - v4_hops), } ; // IPv6: bytes being compared are {NextHdr, HopLimit} - uint16_t ff_rules_v6[4]={ + uint16_t ff_rules_v6[6]={ + (uint16_t)(0x3CFF - hops), + (uint16_t)(0x3CFE - hops), (uint16_t)(0x3CFF - hops), (uint16_t)(0x3CFE - hops), (uint16_t)(0x3CFF - hops), (uint16_t)(0x3CFE - hops), } ; - const rte_l4type ff_rules_type[4]={ + const rte_l4type ff_rules_type[6]={ RTE_FDIR_L4TYPE_UDP, RTE_FDIR_L4TYPE_UDP, RTE_FDIR_L4TYPE_TCP, - RTE_FDIR_L4TYPE_TCP + RTE_FDIR_L4TYPE_TCP, + RTE_FDIR_L4TYPE_NONE, + RTE_FDIR_L4TYPE_NONE } ; uint16_t *ff_rules; @@ -4974,8 +4974,7 @@ int CTRexExtendedDriverBase10G::configure_rx_filter_rules(CPhyEthIF * _if){ } int CTRexExtendedDriverBase10G::configure_drop_queue(CPhyEthIF * _if){ - - /* enable rule 0 SCTP -> queue 1 for latency */ + /* enable rule 0 SCTP -> queue 1 for latency */ /* 1<<21 means that queue 1 is for SCTP */ _if->pci_reg_write(IXGBE_L34T_IMIR(0),(1<<21)); @@ -4985,7 +4984,6 @@ int CTRexExtendedDriverBase10G::configure_drop_queue(CPhyEthIF * _if){ ((0x0f)<<IXGBE_FTQF_5TUPLE_MASK_SHIFT)|IXGBE_FTQF_QUEUE_ENABLE); /* disable queue zero - default all traffic will go to here and will be dropped */ - _if->pci_reg_write( IXGBE_RXDCTL(0) , 0); return (0); } @@ -5046,7 +5044,7 @@ void CTRexExtendedDriverBase40G::update_configuration(port_cfg_t * cfg){ } - +/* Add rule to send packets with protocol 'type', and ttl 'ttl' to rx queue 1 */ void CTRexExtendedDriverBase40G::add_rules(CPhyEthIF * _if, enum rte_eth_flow_type type, uint8_t ttl){ @@ -5071,7 +5069,11 @@ void CTRexExtendedDriverBase40G::add_rules(CPhyEthIF * _if, filter.input.flow_type = type; filter.input.ttl=ttl; - /* any SCTP move to queue number 1 */ + if (type == RTE_ETH_FLOW_TYPE_IPV4_OTHER) { + filter.input.flow.ip4_flow.l4_proto = IPPROTO_ICMP; // In this case we want filter for icmp packets + } + + /* We want to place latency packets in queue 1 */ ret=rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR, RTE_ETH_FILTER_ADD, (void*)&filter); @@ -5100,7 +5102,9 @@ int CTRexExtendedDriverBase40G::configure_rx_filter_rules(CPhyEthIF * _if){ int CTRexExtendedDriverBase40G::configure_drop_queue(CPhyEthIF * _if){ - add_rules(_if,RTE_ETH_FLOW_TYPE_SCTPV4,0); + /* Configure queue for latency packets */ + add_rules(_if,RTE_ETH_FLOW_TYPE_IPV4_OTHER,255); + add_rules(_if,RTE_ETH_FLOW_TYPE_SCTPV4,255); return (0); } diff --git a/src/platform_cfg.cpp b/src/platform_cfg.cpp index 547cc3ad..ca42aa31 100755 --- a/src/platform_cfg.cpp +++ b/src/platform_cfg.cpp @@ -35,8 +35,8 @@ void CPlatformMemoryYamlInfo::reset(){ m_mbuf[MBUF_64] = m_mbuf[MBUF_64]*2; m_mbuf[MBUF_2048] = CONST_NB_MBUF_2_10G/2; - m_mbuf[TRAFFIC_MBUF_64] = m_mbuf[MBUF_64]*2; - m_mbuf[TRAFFIC_MBUF_2048] = CONST_NB_MBUF_2_10G*4; + m_mbuf[TRAFFIC_MBUF_64] = m_mbuf[MBUF_64] * 4; + m_mbuf[TRAFFIC_MBUF_2048] = CONST_NB_MBUF_2_10G * 8; m_mbuf[MBUF_DP_FLOWS] = (1024*1024/2); m_mbuf[MBUF_GLOBAL_FLOWS] =(10*1024/2); diff --git a/src/publisher/trex_publisher.h b/src/publisher/trex_publisher.h index 8d1be064..bd4392f7 100644 --- a/src/publisher/trex_publisher.h +++ b/src/publisher/trex_publisher.h @@ -39,10 +39,16 @@ public: void publish_json(const std::string &s); enum event_type_e { - EVENT_PORT_STARTED = 0, - EVENT_PORT_STOPPED = 1, - EVENT_SERVER_STOPPED = 2, - EVENT_PORT_FINISHED_TX = 3, + EVENT_PORT_STARTED = 0, + EVENT_PORT_STOPPED = 1, + EVENT_PORT_PAUSED = 2, + EVENT_PORT_RESUMED = 3, + EVENT_PORT_FINISHED_TX = 4, + EVENT_PORT_FORCE_ACQUIRED = 5, + + EVENT_SERVER_STOPPED = 100, + + }; void publish_event(event_type_e type, const Json::Value &data = Json::nullValue); diff --git a/src/rpc-server/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/commands/trex_rpc_cmd_general.cpp index 9570aae7..a701f6db 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_general.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_general.cpp @@ -43,7 +43,7 @@ using namespace std; trex_rpc_cmd_rc_e TrexRpcCmdPing::_run(const Json::Value ¶ms, Json::Value &result) { - result["result"] = "ACK"; + result["result"] = Json::objectValue; return (TREX_RPC_CMD_OK); } @@ -198,10 +198,6 @@ TrexRpcCmdGetSysInfo::_run(const Json::Value ¶ms, Json::Value &result) { } - section["ports"][i]["owner"] = port->get_owner(); - - section["ports"][i]["status"] = port->get_state_as_string(); - } return (TREX_RPC_CMD_OK); @@ -224,7 +220,7 @@ TrexRpcCmdGetOwner::_run(const Json::Value ¶ms, Json::Value &result) { uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - section["owner"] = port->get_owner(); + section["owner"] = port->get_owner().get_name(); return (TREX_RPC_CMD_OK); } @@ -238,19 +234,20 @@ TrexRpcCmdAcquire::_run(const Json::Value ¶ms, Json::Value &result) { uint8_t port_id = parse_port(params, result); - const string &new_owner = parse_string(params, "user", result); + const string &new_owner = parse_string(params, "user", result); bool force = parse_bool(params, "force", result); + uint32_t session_id = parse_uint32(params, "session_id", result); /* if not free and not you and not force - fail */ TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); try { - port->acquire(new_owner, force); + port->acquire(new_owner, session_id, force); } catch (const TrexRpcException &ex) { generate_execute_err(result, ex.what()); } - result["result"] = port->get_owner_handler(); + result["result"] = port->get_owner().get_handler(); return (TREX_RPC_CMD_OK); } @@ -272,7 +269,7 @@ TrexRpcCmdRelease::_run(const Json::Value ¶ms, Json::Value &result) { generate_execute_err(result, ex.what()); } - result["result"] = "ACK"; + result["result"] = Json::objectValue; return (TREX_RPC_CMD_OK); } @@ -288,8 +285,6 @@ TrexRpcCmdGetPortStats::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - result["result"]["status"] = port->get_state_as_string(); - try { port->encode_stats(result["result"]); } catch (const TrexRpcException &ex) { @@ -300,41 +295,25 @@ TrexRpcCmdGetPortStats::_run(const Json::Value ¶ms, Json::Value &result) { } /** - * request the server a sync about a specific user + * fetch the port status + * + * @author imarom (09-Dec-15) + * + * @param params + * @param result * + * @return trex_rpc_cmd_rc_e */ trex_rpc_cmd_rc_e -TrexRpcCmdSyncUser::_run(const Json::Value ¶ms, Json::Value &result) { - - const string &user = parse_string(params, "user", result); - bool sync_streams = parse_bool(params, "sync_streams", result); - - result["result"] = Json::arrayValue; - - for (auto port : get_stateless_obj()->get_port_list()) { - if (port->get_owner() == user) { - - Json::Value owned_port; +TrexRpcCmdGetPortStatus::_run(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_port(params, result); - owned_port["port_id"] = port->get_port_id(); - owned_port["handler"] = port->get_owner_handler(); - owned_port["state"] = port->get_state_as_string(); - - /* if sync streams was asked - sync all the streams */ - if (sync_streams) { - owned_port["streams"] = Json::arrayValue; + TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - std::vector <TrexStream *> streams; - port->get_object_list(streams); + result["result"]["owner"] = (port->get_owner().is_free() ? "" : port->get_owner().get_name()); + result["result"]["state"] = port->get_state_as_string(); - for (auto stream : streams) { - owned_port["streams"].append(stream->get_stream_json()); - } - } - - result["result"].append(owned_port); - } - } return (TREX_RPC_CMD_OK); } + diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp index 96224d4e..d8f7e772 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -23,27 +23,12 @@ limitations under the License. #include <trex_stream.h> #include <trex_stateless.h> #include <trex_stateless_port.h> +#include <trex_streams_compiler.h> #include <iostream> using namespace std; -/** - * simple parser of string to number - * only difference is that it enforces whole number - * and not partial - * - */ -static uint64_t str2num(const string &str) { - size_t index; - - uint64_t num = std::stoull(str, &index, 0); - if (index != str.size()) { - throw invalid_argument("could not parse string to number"); - } - - return (num); -} /*************************** * add new stream @@ -52,7 +37,8 @@ static uint64_t str2num(const string &str) { trex_rpc_cmd_rc_e TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_int(params, "port_id", result); + uint8_t port_id = parse_port(params, result); + uint32_t stream_id = parse_int(params, "stream_id", result); const Json::Value §ion = parse_object(params, "stream", result); @@ -122,7 +108,7 @@ TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { generate_execute_err(result, ex.what()); } - result["result"] = "ACK"; + result["result"] = Json::objectValue; return (TREX_RPC_CMD_OK); } @@ -190,6 +176,30 @@ TrexRpcCmdAddStream::parse_vm_instr_checksum(const Json::Value &inst, TrexStream } void +TrexRpcCmdAddStream::parse_vm_instr_tuple_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result){ + + + std::string flow_var_name = parse_string(inst, "name", result); + + uint32_t ip_min = parse_uint32(inst, "ip_min", result); + uint32_t ip_max = parse_uint32(inst, "ip_max", result); + uint16_t port_min = parse_uint16(inst, "port_min", result); + uint16_t port_max = parse_uint16(inst, "port_max", result); + uint32_t limit_flows = parse_uint32(inst, "limit_flows", result); + uint16_t flags = parse_uint16(inst, "flags", result); + + stream->m_vm.add_instruction(new StreamVmInstructionFlowClient(flow_var_name, + ip_min, + ip_max, + port_min, + port_max, + limit_flows, + flags + )); +} + + +void TrexRpcCmdAddStream::parse_vm_instr_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result) { std::string flow_var_name = parse_string(inst, "name", result); @@ -211,31 +221,9 @@ TrexRpcCmdAddStream::parse_vm_instr_flow_var(const Json::Value &inst, TrexStream throw TrexRpcException("internal error"); } - std::string init_value_str = parse_string(inst, "init_value", result); - std::string min_value_str = parse_string(inst, "min_value", result); - std::string max_value_str = parse_string(inst, "max_value", result); - - uint64_t init_value = 0; - uint64_t min_value = 0; - uint64_t max_value = 0; - - try { - init_value = str2num(init_value_str); - } catch (invalid_argument) { - generate_parse_err(result, "failed to parse 'init_value' as a number"); - } - - try { - min_value = str2num(min_value_str); - } catch (invalid_argument) { - generate_parse_err(result, "failed to parse 'min_value' as a number"); - } - - try { - max_value = str2num(max_value_str); - } catch (invalid_argument) { - generate_parse_err(result, "failed to parse 'max_value' as a number"); - } + uint64_t init_value = parse_uint64(inst, "init_value", result); + uint64_t min_value = parse_uint64(inst, "min_value", result); + uint64_t max_value = parse_uint64(inst, "max_value", result); stream->m_vm.add_instruction(new StreamVmInstructionFlowMan(flow_var_name, flow_var_size, @@ -263,9 +251,9 @@ void TrexRpcCmdAddStream::parse_vm(const Json::Value &vm, TrexStream *stream, Json::Value &result) { /* array of VM instructions on vm */ for (int i = 0; i < vm.size(); i++) { - const Json::Value & inst = vm[i]; + const Json::Value & inst = parse_object(vm, i, result); - auto vm_types = {"fix_checksum_ipv4", "flow_var", "write_flow_var"}; + auto vm_types = {"fix_checksum_ipv4", "flow_var", "write_flow_var","tuple_flow_var"}; std::string vm_type = parse_choice(inst, "type", vm_types, result); // checksum instruction @@ -278,6 +266,8 @@ TrexRpcCmdAddStream::parse_vm(const Json::Value &vm, TrexStream *stream, Json::V } else if (vm_type == "write_flow_var") { parse_vm_instr_write_flow_var(inst, stream, result); + } else if (vm_type == "tuple_flow_var") { + parse_vm_instr_tuple_flow_var(inst, stream, result); } else { /* internal error */ throw TrexRpcException("internal error"); @@ -296,15 +286,7 @@ TrexRpcCmdAddStream::validate_stream(const TrexStream *stream, Json::Value &resu generate_execute_err(result, ss.str()); } - /* port id should be between 0 and count - 1 */ - if (stream->m_port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - delete stream; - generate_execute_err(result, ss.str()); - } - - /* add the stream to the port's stream table */ + /* add the stream to the port's stream table */ TrexStatelessPort * port = get_stateless_obj()->get_port_by_id(stream->m_port_id); /* does such a stream exists ? */ @@ -323,17 +305,11 @@ TrexRpcCmdAddStream::validate_stream(const TrexStream *stream, Json::Value &resu **************************/ trex_rpc_cmd_rc_e TrexRpcCmdRemoveStream::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - uint32_t stream_id = parse_int(params, "stream_id", result); - - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + + uint32_t stream_id = parse_int(params, "stream_id", result); TrexStream *stream = port->get_stream_by_id(stream_id); if (!stream) { @@ -350,7 +326,7 @@ TrexRpcCmdRemoveStream::_run(const Json::Value ¶ms, Json::Value &result) { delete stream; - result["result"] = "ACK"; + result["result"] = Json::objectValue; return (TREX_RPC_CMD_OK); } @@ -362,14 +338,8 @@ TrexRpcCmdRemoveStream::_run(const Json::Value ¶ms, Json::Value &result) { **************************/ trex_rpc_cmd_rc_e TrexRpcCmdRemoveAllStreams::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); try { @@ -379,7 +349,7 @@ TrexRpcCmdRemoveAllStreams::_run(const Json::Value ¶ms, Json::Value &result) } - result["result"] = "ACK"; + result["result"] = Json::objectValue; return (TREX_RPC_CMD_OK); } @@ -393,27 +363,20 @@ trex_rpc_cmd_rc_e TrexRpcCmdGetStreamList::_run(const Json::Value ¶ms, Json::Value &result) { std::vector<uint32_t> stream_list; - uint8_t port_id = parse_byte(params, "port_id", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } - - TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + uint8_t port_id = parse_port(params, result); + TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - port->get_id_list(stream_list); + port->get_id_list(stream_list); - Json::Value json_list = Json::arrayValue; + Json::Value json_list = Json::arrayValue; - for (auto stream_id : stream_list) { - json_list.append(stream_id); - } + for (auto stream_id : stream_list) { + json_list.append(stream_id); + } - result["result"] = json_list; + result["result"] = json_list; - return (TREX_RPC_CMD_OK); + return (TREX_RPC_CMD_OK); } /*************************** @@ -423,18 +386,13 @@ TrexRpcCmdGetStreamList::_run(const Json::Value ¶ms, Json::Value &result) { **************************/ trex_rpc_cmd_rc_e TrexRpcCmdGetStream::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - bool get_pkt = parse_bool(params, "get_pkt", result); - uint32_t stream_id = parse_int(params, "stream_id", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + bool get_pkt = parse_bool(params, "get_pkt", result); + uint32_t stream_id = parse_int(params, "stream_id", result); + TrexStream *stream = port->get_stream_by_id(stream_id); if (!stream) { @@ -462,49 +420,32 @@ TrexRpcCmdGetStream::_run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e TrexRpcCmdStartTraffic::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - double duration = parse_double(params, "duration", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } - + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + double duration = parse_double(params, "duration", result); - const Json::Value &mul = parse_object(params, "mul", result); - - std::string mul_type = parse_string(mul, "type", result); - double max_rate = parse_double(mul, "max", result); - - - double m = 0; - - /* dispatch according to type of multiplier */ - if (mul_type == "raw") { - m = max_rate; - - } else if (mul_type == "max_bps") { - m = port->calculate_m_from_bps(max_rate); + /* multiplier */ + const Json::Value &mul_obj = parse_object(params, "mul", result); - } else if (mul_type == "max_pps") { - m = port->calculate_m_from_pps(max_rate); + std::string type = parse_choice(mul_obj, "type", TrexPortMultiplier::g_types, result); + std::string op = parse_string(mul_obj, "op", result); + double value = parse_double(mul_obj, "value", result); - } else { - generate_parse_err(result, "multiplier type can be either 'raw', 'max_bps' or 'max_pps'"); + if (op != "abs") { + generate_parse_err(result, "start message can only specify absolute speed rate"); } + TrexPortMultiplier mul(type, op, value); try { - port->start_traffic(m, duration); + port->start_traffic(mul, duration); } catch (const TrexRpcException &ex) { generate_execute_err(result, ex.what()); } - result["result"] = "ACK"; + result["result"]["multiplier"] = port->get_multiplier(); return (TREX_RPC_CMD_OK); } @@ -515,14 +456,8 @@ TrexRpcCmdStartTraffic::_run(const Json::Value ¶ms, Json::Value &result) { **************************/ trex_rpc_cmd_rc_e TrexRpcCmdStopTraffic::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); try { @@ -531,7 +466,7 @@ TrexRpcCmdStopTraffic::_run(const Json::Value ¶ms, Json::Value &result) { generate_execute_err(result, ex.what()); } - result["result"] = "ACK"; + result["result"] = Json::objectValue; return (TREX_RPC_CMD_OK); } @@ -542,17 +477,12 @@ TrexRpcCmdStopTraffic::_run(const Json::Value ¶ms, Json::Value &result) { **************************/ trex_rpc_cmd_rc_e TrexRpcCmdGetAllStreams::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - bool get_pkt = parse_bool(params, "get_pkt", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } - + + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + bool get_pkt = parse_bool(params, "get_pkt", result); + std::vector <TrexStream *> streams; port->get_object_list(streams); @@ -584,14 +514,7 @@ TrexRpcCmdGetAllStreams::_run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e TrexRpcCmdPauseTraffic::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } - + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); try { @@ -600,7 +523,7 @@ TrexRpcCmdPauseTraffic::_run(const Json::Value ¶ms, Json::Value &result) { generate_execute_err(result, ex.what()); } - result["result"] = "ACK"; + result["result"] = Json::objectValue; return (TREX_RPC_CMD_OK); } @@ -612,14 +535,7 @@ TrexRpcCmdPauseTraffic::_run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e TrexRpcCmdResumeTraffic::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } - + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); try { @@ -628,7 +544,7 @@ TrexRpcCmdResumeTraffic::_run(const Json::Value ¶ms, Json::Value &result) { generate_execute_err(result, ex.what()); } - result["result"] = "ACK"; + result["result"] = Json::objectValue; return (TREX_RPC_CMD_OK); } @@ -640,46 +556,80 @@ TrexRpcCmdResumeTraffic::_run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e TrexRpcCmdUpdateTraffic::_run(const Json::Value ¶ms, Json::Value &result) { - uint8_t port_id = parse_byte(params, "port_id", result); - - if (port_id >= get_stateless_obj()->get_port_count()) { - std::stringstream ss; - ss << "invalid port id - should be between 0 and " << (int)get_stateless_obj()->get_port_count() - 1; - generate_execute_err(result, ss.str()); - } - + uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); /* multiplier */ - const Json::Value &mul = parse_object(params, "mul", result); - std::string mul_type = parse_string(mul, "type", result); - double max_rate = parse_double(mul, "max", result); + const Json::Value &mul_obj = parse_object(params, "mul", result); - double m = 0; + std::string type = parse_choice(mul_obj, "type", TrexPortMultiplier::g_types, result); + std::string op = parse_choice(mul_obj, "op", TrexPortMultiplier::g_ops, result); + double value = parse_double(mul_obj, "value", result); - /* dispatch according to type of multiplier */ - if (mul_type == "raw") { - m = max_rate; + TrexPortMultiplier mul(type, op, value); - } else if (mul_type == "max_bps") { - m = port->calculate_m_from_bps(max_rate); - - } else if (mul_type == "max_pps") { - m = port->calculate_m_from_pps(max_rate); - - } else { - generate_parse_err(result, "multiplier type can be either 'raw', 'max_bps' or 'max_pps'"); - } try { - port->update_traffic(m); + port->update_traffic(mul); } catch (const TrexRpcException &ex) { generate_execute_err(result, ex.what()); } - result["result"] = "ACK"; + result["result"]["multiplier"] = port->get_multiplier(); return (TREX_RPC_CMD_OK); } +/*************************** + * validate + * + * checks that the port + * attached streams are + * valid as a program + **************************/ +trex_rpc_cmd_rc_e +TrexRpcCmdValidate::_run(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_port(params, result); + TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); + + const TrexStreamsGraphObj *graph = NULL; + + try { + graph = port->validate(); + } + catch (const TrexException &ex) { + generate_execute_err(result, ex.what()); + } + + + result["result"]["rate"]["max_bps"] = graph->get_max_bps(); + result["result"]["rate"]["max_pps"] = graph->get_max_pps(); + result["result"]["rate"]["max_line_util"] = graph->get_max_bps() / port->get_port_speed_bps(); + + result["result"]["graph"]["expected_duration"] = graph->get_duration(); + result["result"]["graph"]["events_count"] = (int)graph->get_events().size(); + + result["result"]["graph"]["events"] = Json::arrayValue; + Json::Value &events_json = result["result"]["graph"]["events"]; + + int index = 0; + for (const auto &ev : graph->get_events()) { + Json::Value ev_json; + + ev_json["time_usec"] = ev.time; + ev_json["diff_bps"] = ev.diff_bps; + ev_json["diff_pps"] = ev.diff_pps; + ev_json["stream_id"] = ev.stream_id; + + events_json.append(ev_json); + + index++; + if (index >= 100) { + break; + } + } + + + return (TREX_RPC_CMD_OK); +} diff --git a/src/rpc-server/commands/trex_rpc_cmd_test.cpp b/src/rpc-server/commands/trex_rpc_cmd_test.cpp index 3cdddd31..ad4d3bb1 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_test.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_test.cpp @@ -21,6 +21,7 @@ limitations under the License. #include "trex_rpc_cmds.h" #include <iostream> #include <sstream> +#include <json/json.h> using namespace std; @@ -31,7 +32,7 @@ using namespace std; trex_rpc_cmd_rc_e TrexRpcCmdTestAdd::_run(const Json::Value ¶ms, Json::Value &result) { - result["result"] = parse_int(params, "x", result) + parse_int(params, "y", result); + result["result"] = Json::Value::UInt64(parse_uint64(params, "x", result) + parse_uint64(params, "y", result)); return (TREX_RPC_CMD_OK); } @@ -44,7 +45,7 @@ TrexRpcCmdTestAdd::_run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e TrexRpcCmdTestSub::_run(const Json::Value ¶ms, Json::Value &result) { - result["result"] = parse_int(params, "x", result) - parse_int(params, "y", result); + result["result"] = Json::Value::UInt64(parse_uint64(params, "x", result) - parse_uint64(params, "y", result)); return (TREX_RPC_CMD_OK); } diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h index b4f37e3b..f4651d7b 100644 --- a/src/rpc-server/commands/trex_rpc_cmds.h +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -70,14 +70,15 @@ void get_hostname(std::string &hostname); * ownership */ TREX_RPC_CMD_DEFINE(TrexRpcCmdGetOwner, "get_owner", 1, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdAcquire, "acquire", 3, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdAcquire, "acquire", 4, false); TREX_RPC_CMD_DEFINE(TrexRpcCmdRelease, "release", 1, true); /** * port commands */ -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetPortStats, "get_port_stats", 1, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetPortStats, "get_port_stats", 1, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetPortStatus, "get_port_status", 1, false); /** @@ -94,14 +95,15 @@ void validate_stream(const TrexStream *stream, Json::Value &result); void parse_vm(const Json::Value &vm, TrexStream *stream, Json::Value &result); void parse_vm_instr_checksum(const Json::Value &inst, TrexStream *stream, Json::Value &result); void parse_vm_instr_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result); +void parse_vm_instr_tuple_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result); void parse_vm_instr_write_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result); ); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStreamList, "get_stream_list", 1, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetAllStreams, "get_all_streams", 2, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStreamList, "get_stream_list", 1, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetAllStreams, "get_all_streams", 2, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 3, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 3, false); @@ -112,7 +114,6 @@ TREX_RPC_CMD_DEFINE(TrexRpcCmdResumeTraffic, "resume_traffic", 1, true); TREX_RPC_CMD_DEFINE(TrexRpcCmdUpdateTraffic, "update_traffic", 2, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdSyncUser, "sync_user", 2, false); - +TREX_RPC_CMD_DEFINE(TrexRpcCmdValidate, "validate", 2, false); #endif /* __TREX_RPC_CMD_H__ */ diff --git a/src/rpc-server/trex_rpc_cmd.cpp b/src/rpc-server/trex_rpc_cmd.cpp index af0db3f4..b5dd121c 100644 --- a/src/rpc-server/trex_rpc_cmd.cpp +++ b/src/rpc-server/trex_rpc_cmd.cpp @@ -63,8 +63,12 @@ TrexRpcCommand::verify_ownership(const Json::Value ¶ms, Json::Value &result) TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - if (!port->verify_owner_handler(handler)) { - generate_execute_err(result, "invalid handler provided. please pass the handler given when calling 'acquire' or take ownership"); + if (port->get_owner().is_free()) { + generate_execute_err(result, "please acquire the port before modifying port state"); + } + + if (!port->get_owner().verify(handler)) { + generate_execute_err(result, "port is not owned by you or your current executing session"); } } @@ -92,6 +96,10 @@ TrexRpcCommand::type_to_str(field_type_e type) { return "byte"; case FIELD_TYPE_UINT16: return "uint16"; + case FIELD_TYPE_UINT32: + return "uint32"; + case FIELD_TYPE_UINT64: + return "uint64"; case FIELD_TYPE_BOOL: return "bool"; case FIELD_TYPE_INT: @@ -161,6 +169,30 @@ TrexRpcCommand::parse_uint16(const Json::Value &parent, int index, Json::Value & return parent[index].asUInt(); } +uint32_t +TrexRpcCommand::parse_uint32(const Json::Value &parent, const std::string &name, Json::Value &result) { + check_field_type(parent, name, FIELD_TYPE_UINT32, result); + return parent[name].asUInt(); +} + +uint32_t +TrexRpcCommand::parse_uint32(const Json::Value &parent, int index, Json::Value &result) { + check_field_type(parent, index, FIELD_TYPE_UINT32, result); + return parent[index].asUInt(); +} + +uint64_t +TrexRpcCommand::parse_uint64(const Json::Value &parent, const std::string &name, Json::Value &result) { + check_field_type(parent, name, FIELD_TYPE_UINT64, result); + return parent[name].asUInt64(); +} + +uint64_t +TrexRpcCommand::parse_uint64(const Json::Value &parent, int index, Json::Value &result) { + check_field_type(parent, index, FIELD_TYPE_UINT64, result); + return parent[index].asUInt64(); +} + int TrexRpcCommand::parse_int(const Json::Value &parent, const std::string &name, Json::Value &result) { check_field_type(parent, name, FIELD_TYPE_INT, result); @@ -185,12 +217,24 @@ TrexRpcCommand::parse_string(const Json::Value &parent, const std::string &name, return parent[name].asString(); } +/** + * object version + */ const Json::Value & TrexRpcCommand::parse_object(const Json::Value &parent, const std::string &name, Json::Value &result) { check_field_type(parent, name, FIELD_TYPE_OBJ, result); return parent[name]; } +/** + * index version + */ +const Json::Value & +TrexRpcCommand::parse_object(const Json::Value &parent, int index, Json::Value &result) { + check_field_type(parent, index, FIELD_TYPE_OBJ, result); + return parent[index]; +} + const Json::Value & TrexRpcCommand::parse_array(const Json::Value &parent, const std::string &name, Json::Value &result) { check_field_type(parent, name, FIELD_TYPE_ARRAY, result); @@ -250,6 +294,18 @@ TrexRpcCommand::check_field_type_common(const Json::Value &field, const std::str } break; + case FIELD_TYPE_UINT32: + if ( (!field.isUInt()) || (field.asUInt() > 0xFFFFFFFF)) { + rc = false; + } + break; + + case FIELD_TYPE_UINT64: + if (!field.isInt64()) { + rc = false; + } + break; + case FIELD_TYPE_BOOL: if (!field.isBool()) { rc = false; diff --git a/src/rpc-server/trex_rpc_cmd_api.h b/src/rpc-server/trex_rpc_cmd_api.h index 3c718eaa..7cbdf4ff 100644 --- a/src/rpc-server/trex_rpc_cmd_api.h +++ b/src/rpc-server/trex_rpc_cmd_api.h @@ -99,6 +99,8 @@ protected: enum field_type_e { FIELD_TYPE_BYTE, FIELD_TYPE_UINT16, + FIELD_TYPE_UINT32, + FIELD_TYPE_UINT64, FIELD_TYPE_INT, FIELD_TYPE_DOUBLE, FIELD_TYPE_BOOL, @@ -136,6 +138,8 @@ protected: */ uint8_t parse_byte(const Json::Value &parent, const std::string &name, Json::Value &result); uint16_t parse_uint16(const Json::Value &parent, const std::string &name, Json::Value &result); + uint32_t parse_uint32(const Json::Value &parent, const std::string &name, Json::Value &result); + uint64_t parse_uint64(const Json::Value &parent, const std::string &name, Json::Value &result); int parse_int(const Json::Value &parent, const std::string &name, Json::Value &result); double parse_double(const Json::Value &parent, const std::string &name, Json::Value &result); bool parse_bool(const Json::Value &parent, const std::string &name, Json::Value &result); @@ -145,6 +149,8 @@ protected: uint8_t parse_byte(const Json::Value &parent, int index, Json::Value &result); uint16_t parse_uint16(const Json::Value &parent, int index, Json::Value &result); + uint32_t parse_uint32(const Json::Value &parent, int index, Json::Value &result); + uint64_t parse_uint64(const Json::Value &parent, int index, Json::Value &result); int parse_int(const Json::Value &parent, int index, Json::Value &result); double parse_double(const Json::Value &parent, int index, Json::Value &result); bool parse_bool(const Json::Value &parent, int index, Json::Value &result); @@ -159,7 +165,7 @@ protected: * parse a field from choices * */ - template<typename T> T parse_choice(const Json::Value ¶ms, const std::string &name, std::initializer_list<T> choices, Json::Value &result) { + template<typename T> T parse_choice(const Json::Value ¶ms, const std::string &name, const std::initializer_list<T> choices, Json::Value &result) { const Json::Value &field = params[name]; if (field == Json::Value::null) { diff --git a/src/rpc-server/trex_rpc_cmds_table.cpp b/src/rpc-server/trex_rpc_cmds_table.cpp index a65bbccf..82c723b7 100644 --- a/src/rpc-server/trex_rpc_cmds_table.cpp +++ b/src/rpc-server/trex_rpc_cmds_table.cpp @@ -41,8 +41,8 @@ TrexRpcCommandsTable::TrexRpcCommandsTable() { register_command(new TrexRpcCmdAcquire()); register_command(new TrexRpcCmdRelease()); register_command(new TrexRpcCmdGetPortStats()); - - register_command(new TrexRpcCmdSyncUser()); + register_command(new TrexRpcCmdGetPortStatus()); + /* stream commands */ register_command(new TrexRpcCmdAddStream()); @@ -57,8 +57,11 @@ TrexRpcCommandsTable::TrexRpcCommandsTable() { register_command(new TrexRpcCmdPauseTraffic()); register_command(new TrexRpcCmdResumeTraffic()); register_command(new TrexRpcCmdUpdateTraffic()); + + register_command(new TrexRpcCmdValidate()); } + TrexRpcCommandsTable::~TrexRpcCommandsTable() { for (auto cmd : m_rpc_cmd_table) { delete cmd.second; diff --git a/src/rpc-server/trex_rpc_req_resp_server.cpp b/src/rpc-server/trex_rpc_req_resp_server.cpp index 9147f75d..eb7825ac 100644 --- a/src/rpc-server/trex_rpc_req_resp_server.cpp +++ b/src/rpc-server/trex_rpc_req_resp_server.cpp @@ -26,6 +26,7 @@ limitations under the License. #include <unistd.h> #include <sstream> #include <iostream> +#include <assert.h> #include <zmq.h> #include <json/json.h> @@ -70,28 +71,14 @@ void TrexRpcServerReqRes::_rpc_thread_cb() { /* server main loop */ while (m_is_running) { - int msg_size = zmq_recv (m_socket, m_msg_buffer, sizeof(m_msg_buffer), 0); - - /* msg_size of -1 is an error - decode it */ - if (msg_size == -1) { - /* normal shutdown and zmq_term was called */ - if (errno == ETERM) { - break; - } else { - throw TrexRpcException("Unhandled error of zmq_recv"); - } - } + std::string request; - if (msg_size >= sizeof(m_msg_buffer)) { - std::stringstream ss; - ss << "RPC request of '" << msg_size << "' exceeds maximum message size which is '" << sizeof(m_msg_buffer) << "'"; - handle_server_error(ss.str()); - continue; + /* get the next request */ + bool rc = fetch_one_request(request); + if (!rc) { + break; } - /* transform it to a string */ - std::string request((const char *)m_msg_buffer, msg_size); - verbose_json("Server Received: ", TrexJsonRpcV2Parser::pretty_json_str(request)); handle_request(request); @@ -101,6 +88,35 @@ void TrexRpcServerReqRes::_rpc_thread_cb() { zmq_close(m_socket); } +bool +TrexRpcServerReqRes::fetch_one_request(std::string &msg) { + + zmq_msg_t zmq_msg; + int rc; + + rc = zmq_msg_init(&zmq_msg); + assert(rc == 0); + + rc = zmq_msg_recv (&zmq_msg, m_socket, 0); + + if (rc == -1) { + zmq_msg_close(&zmq_msg); + /* normal shutdown and zmq_term was called */ + if (errno == ETERM) { + return false; + } else { + throw TrexRpcException("Unhandled error of zmq_recv"); + } + } + + const char *data = (const char *)zmq_msg_data(&zmq_msg); + size_t len = zmq_msg_size(&zmq_msg); + msg.append(data, len); + + zmq_msg_close(&zmq_msg); + return true; +} + /** * stops the ZMQ based RPC server * diff --git a/src/rpc-server/trex_rpc_req_resp_server.h b/src/rpc-server/trex_rpc_req_resp_server.h index bc38c0ef..2876206c 100644 --- a/src/rpc-server/trex_rpc_req_resp_server.h +++ b/src/rpc-server/trex_rpc_req_resp_server.h @@ -39,14 +39,12 @@ protected: void _stop_rpc_thread(); private: - + bool fetch_one_request(std::string &msg); void handle_request(const std::string &request); void handle_server_error(const std::string &specific_err); - static const int RPC_MAX_MSG_SIZE = (200 * 1024); void *m_context; void *m_socket; - uint8_t m_msg_buffer[RPC_MAX_MSG_SIZE]; }; diff --git a/src/stateless/cp/trex_stateless_port.cpp b/src/stateless/cp/trex_stateless_port.cpp index 8e18a5bf..3a64f8c5 100644 --- a/src/stateless/cp/trex_stateless_port.cpp +++ b/src/stateless/cp/trex_stateless_port.cpp @@ -57,7 +57,6 @@ TrexStatelessPort::TrexStatelessPort(uint8_t port_id, const TrexPlatformApi *api m_port_id = port_id; m_port_state = PORT_STATE_IDLE; - clear_owner(); /* get the platform specific data */ api->get_interface_info(port_id, m_driver_name, m_speed); @@ -85,18 +84,40 @@ TrexStatelessPort::TrexStatelessPort(uint8_t port_id, const TrexPlatformApi *api * @param force */ void -TrexStatelessPort::acquire(const std::string &user, bool force) { - if ( (!is_free_to_aquire()) && (get_owner() != user) && (!force)) { - throw TrexRpcException("port is already taken by '" + get_owner() + "'"); +TrexStatelessPort::acquire(const std::string &user, uint32_t session_id, bool force) { + + /* if port is free - just take it */ + if (get_owner().is_free()) { + get_owner().own(user); + return; + } + + if (force) { + get_owner().own(user); + + /* inform the other client of the steal... */ + Json::Value data; + + data["port_id"] = m_port_id; + data["who"] = user; + data["session_id"] = session_id; + + get_stateless_obj()->get_publisher()->publish_event(TrexPublisher::EVENT_PORT_FORCE_ACQUIRED, data); + + } else { + /* not same user or session id and not force - report error */ + if (get_owner().get_name() == user) { + throw TrexRpcException("port is already owned by another session of '" + user + "'"); + } else { + throw TrexRpcException("port is already taken by '" + get_owner().get_name() + "'"); + } } - set_owner(user); } void TrexStatelessPort::release(void) { - verify_state( ~(PORT_STATE_TX | PORT_STATE_PAUSE) ); - clear_owner(); + get_owner().release(); } /** @@ -104,71 +125,71 @@ TrexStatelessPort::release(void) { * */ void -TrexStatelessPort::start_traffic(double mul, double duration) { +TrexStatelessPort::start_traffic(const TrexPortMultiplier &mul, double duration) { /* command allowed only on state stream */ verify_state(PORT_STATE_STREAMS); + /* just making sure no leftovers... */ + delete_streams_graph(); + + /* on start - we can only provide absolute values */ + assert(mul.m_op == TrexPortMultiplier::OP_ABS); + + double factor = calculate_effective_factor(mul); + /* fetch all the streams from the table */ vector<TrexStream *> streams; get_object_list(streams); - /* split it per core */ - double per_core_mul = mul / m_cores_id_list.size(); /* compiler it */ - TrexStreamsCompiler compiler; - TrexStreamsCompiledObj *compiled_obj = new TrexStreamsCompiledObj(m_port_id, per_core_mul); + std::vector<TrexStreamsCompiledObj *> compiled_objs; + std::string fail_msg; - bool rc = compiler.compile(streams, *compiled_obj); + TrexStreamsCompiler compiler; + bool rc = compiler.compile(m_port_id, + streams, + compiled_objs, + get_dp_core_count(), + factor, + &fail_msg); if (!rc) { - throw TrexRpcException("Failed to compile streams"); + throw TrexRpcException(fail_msg); } /* generate a message to all the relevant DP cores to start transmitting */ int event_id = m_dp_events.generate_event_id(); + /* mark that DP event of stoppped is possible */ m_dp_events.wait_for_event(TrexDpPortEvent::EVENT_STOP, event_id); - TrexStatelessCpToDpMsgBase *start_msg = new TrexStatelessDpStart(m_port_id, event_id, compiled_obj, duration); - - m_last_all_streams_continues = compiled_obj->get_all_streams_continues(); - m_last_duration =duration; + /* update object status */ + m_factor = factor; + m_last_all_streams_continues = compiled_objs[0]->get_all_streams_continues(); + m_last_duration = duration; change_state(PORT_STATE_TX); - send_message_to_dp(start_msg); + + /* update the DP - messages will be freed by the DP */ + int index = 0; + for (auto core_id : m_cores_id_list) { + + TrexStatelessCpToDpMsgBase *start_msg = new TrexStatelessDpStart(m_port_id, event_id, compiled_objs[index], duration); + send_message_to_dp(core_id, start_msg); + + index++; + } + /* update subscribers */ Json::Value data; data["port_id"] = m_port_id; get_stateless_obj()->get_publisher()->publish_event(TrexPublisher::EVENT_PORT_STARTED, data); + } -double -TrexStatelessPort::calculate_m_from_bps(double max_bps) { - /* fetch all the streams from the table */ - vector<TrexStream *> streams; - get_object_list(streams); - - TrexStreamsGraph graph; - const TrexStreamsGraphObj &obj = graph.generate(streams); - - return (max_bps / obj.get_max_bps()); -} - -double -TrexStatelessPort::calculate_m_from_pps(double max_pps) { - /* fetch all the streams from the table */ - vector<TrexStream *> streams; - get_object_list(streams); - - TrexStreamsGraph graph; - const TrexStreamsGraphObj &obj = graph.generate(streams); - - return (max_pps / obj.get_max_pps()); -} - /** * stop traffic on port * @@ -180,17 +201,20 @@ void TrexStatelessPort::stop_traffic(void) { if (!( (m_port_state == PORT_STATE_TX) - || (m_port_state ==PORT_STATE_PAUSE) )) { + || (m_port_state == PORT_STATE_PAUSE) )) { return; } + /* delete any previous graphs */ + delete_streams_graph(); + /* mask out the DP stop event */ m_dp_events.disable(TrexDpPortEvent::EVENT_STOP); /* generate a message to all the relevant DP cores to start transmitting */ TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpStop(m_port_id); - send_message_to_dp(stop_msg); + send_message_to_all_dp(stop_msg); change_state(PORT_STATE_STREAMS); @@ -213,11 +237,15 @@ TrexStatelessPort::pause_traffic(void) { throw TrexRpcException(" pause is supported when duration is not enable is start command "); } - TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpPause(m_port_id); + TrexStatelessCpToDpMsgBase *pause_msg = new TrexStatelessDpPause(m_port_id); - send_message_to_dp(stop_msg); + send_message_to_all_dp(pause_msg); change_state(PORT_STATE_PAUSE); + + Json::Value data; + data["port_id"] = m_port_id; + get_stateless_obj()->get_publisher()->publish_event(TrexPublisher::EVENT_PORT_PAUSED, data); } void @@ -226,23 +254,54 @@ TrexStatelessPort::resume_traffic(void) { verify_state(PORT_STATE_PAUSE); /* generate a message to all the relevant DP cores to start transmitting */ - TrexStatelessCpToDpMsgBase *stop_msg = new TrexStatelessDpResume(m_port_id); + TrexStatelessCpToDpMsgBase *resume_msg = new TrexStatelessDpResume(m_port_id); - send_message_to_dp(stop_msg); + send_message_to_all_dp(resume_msg); change_state(PORT_STATE_TX); + + + Json::Value data; + data["port_id"] = m_port_id; + get_stateless_obj()->get_publisher()->publish_event(TrexPublisher::EVENT_PORT_RESUMED, data); } void -TrexStatelessPort::update_traffic(double mul) { +TrexStatelessPort::update_traffic(const TrexPortMultiplier &mul) { + + double factor; verify_state(PORT_STATE_TX | PORT_STATE_PAUSE); /* generate a message to all the relevant DP cores to start transmitting */ - double per_core_mul = mul / m_cores_id_list.size(); - TrexStatelessCpToDpMsgBase *update_msg = new TrexStatelessDpUpdate(m_port_id, per_core_mul); + double new_factor = calculate_effective_factor(mul); + + switch (mul.m_op) { + case TrexPortMultiplier::OP_ABS: + factor = new_factor / m_factor; + break; + + case TrexPortMultiplier::OP_ADD: + factor = (m_factor + new_factor) / m_factor; + break; + + case TrexPortMultiplier::OP_SUB: + factor = (m_factor - new_factor) / m_factor; + if (factor <= 0) { + throw TrexRpcException("Update request will lower traffic to less than zero"); + } + break; + + default: + assert(0); + break; + } + + TrexStatelessCpToDpMsgBase *update_msg = new TrexStatelessDpUpdate(m_port_id, factor); - send_message_to_dp(update_msg); + send_message_to_all_dp(update_msg); + + m_factor *= factor; } @@ -295,27 +354,6 @@ TrexStatelessPort::change_state(port_state_e new_state) { m_port_state = new_state; } -/** - * generate a random connection handler - * - */ -std::string -TrexStatelessPort::generate_handler() { - std::stringstream ss; - - static const char alphanum[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - - /* generate 8 bytes of random handler */ - for (int i = 0; i < 8; ++i) { - ss << alphanum[rand() % (sizeof(alphanum) - 1)]; - } - - return (ss.str()); -} - void TrexStatelessPort::encode_stats(Json::Value &port) { @@ -341,15 +379,22 @@ TrexStatelessPort::encode_stats(Json::Value &port) { } void -TrexStatelessPort::send_message_to_dp(TrexStatelessCpToDpMsgBase *msg) { +TrexStatelessPort::send_message_to_all_dp(TrexStatelessCpToDpMsgBase *msg) { for (auto core_id : m_cores_id_list) { - - /* send the message to the core */ - CNodeRing *ring = CMsgIns::Ins()->getCpDp()->getRingCpToDp(core_id); - ring->Enqueue((CGenNode *)msg->clone()); + send_message_to_dp(core_id, msg->clone()); } + /* original was not sent - delete it */ + delete msg; +} + +void +TrexStatelessPort::send_message_to_dp(uint8_t core_id, TrexStatelessCpToDpMsgBase *msg) { + + /* send the message to the core */ + CNodeRing *ring = CMsgIns::Ins()->getCpDp()->getRingCpToDp(core_id); + ring->Enqueue((CGenNode *)msg); } /** @@ -376,3 +421,201 @@ TrexStatelessPort::on_dp_event_occured(TrexDpPortEvent::event_e event_type) { } } + +uint64_t +TrexStatelessPort::get_port_speed_bps() const { + switch (m_speed) { + case TrexPlatformApi::SPEED_1G: + return (1LLU * 1000 * 1000 * 1000); + + case TrexPlatformApi::SPEED_10G: + return (10LLU * 1000 * 1000 * 1000); + + case TrexPlatformApi::SPEED_40G: + return (40LLU * 1000 * 1000 * 1000); + + default: + return 0; + } +} + +double +TrexStatelessPort::calculate_effective_factor(const TrexPortMultiplier &mul) { + + /* for a simple factor request */ + if (mul.m_type == TrexPortMultiplier::MUL_FACTOR) { + return (mul.m_value); + } + + /* we now need the graph - generate it if we don't have it (happens once) */ + if (!m_graph_obj) { + generate_streams_graph(); + } + + switch (mul.m_type) { + case TrexPortMultiplier::MUL_BPS: + return (mul.m_value / m_graph_obj->get_max_bps()); + + case TrexPortMultiplier::MUL_PPS: + return (mul.m_value / m_graph_obj->get_max_pps()); + + case TrexPortMultiplier::MUL_PERCENTAGE: + /* if abs percentage is from the line speed - otherwise its from the current speed */ + + if (mul.m_op == TrexPortMultiplier::OP_ABS) { + double required = (mul.m_value / 100.0) * get_port_speed_bps(); + return (required / m_graph_obj->get_max_bps()); + } else { + return (m_factor * (mul.m_value / 100.0)); + } + + default: + assert(0); + } + +} + + +void +TrexStatelessPort::generate_streams_graph() { + + /* dispose of the old one */ + if (m_graph_obj) { + delete_streams_graph(); + } + + /* fetch all the streams from the table */ + vector<TrexStream *> streams; + get_object_list(streams); + + TrexStreamsGraph graph; + m_graph_obj = graph.generate(streams); +} + +void +TrexStatelessPort::delete_streams_graph() { + if (m_graph_obj) { + delete m_graph_obj; + m_graph_obj = NULL; + } +} + + + +/*************************** + * port multiplier + * + **************************/ +const std::initializer_list<std::string> TrexPortMultiplier::g_types = {"raw", "bps", "pps", "percentage"}; +const std::initializer_list<std::string> TrexPortMultiplier::g_ops = {"abs", "add", "sub"}; + +TrexPortMultiplier:: +TrexPortMultiplier(const std::string &type_str, const std::string &op_str, double value) { + mul_type_e type; + mul_op_e op; + + if (type_str == "raw") { + type = MUL_FACTOR; + + } else if (type_str == "bps") { + type = MUL_BPS; + + } else if (type_str == "pps") { + type = MUL_PPS; + + } else if (type_str == "percentage") { + type = MUL_PERCENTAGE; + } else { + throw TrexException("bad type str: " + type_str); + } + + if (op_str == "abs") { + op = OP_ABS; + + } else if (op_str == "add") { + op = OP_ADD; + + } else if (op_str == "sub") { + op = OP_SUB; + + } else { + throw TrexException("bad op str: " + op_str); + } + + m_type = type; + m_op = op; + m_value = value; + +} + +const TrexStreamsGraphObj * +TrexStatelessPort::validate(void) { + + /* first compile the graph */ + + vector<TrexStream *> streams; + get_object_list(streams); + + if (streams.size() == 0) { + throw TrexException("no streams attached to port"); + } + + TrexStreamsCompiler compiler; + std::vector<TrexStreamsCompiledObj *> compiled_objs; + + std::string fail_msg; + bool rc = compiler.compile(m_port_id, + streams, + compiled_objs, + get_dp_core_count(), + 1.0, + &fail_msg); + if (!rc) { + throw TrexException(fail_msg); + } + + for (auto obj : compiled_objs) { + delete obj; + } + + /* now create a stream graph */ + if (!m_graph_obj) { + generate_streams_graph(); + } + + return m_graph_obj; +} + + +/************* Trex Port Owner **************/ + +TrexPortOwner::TrexPortOwner() { + m_is_free = true; + + /* for handlers random generation */ + srand(time(NULL)); +} + +/** + * generate a random connection handler + * + */ +std::string +TrexPortOwner::generate_handler() { + std::stringstream ss; + + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + + /* generate 8 bytes of random handler */ + for (int i = 0; i < 8; ++i) { + ss << alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + return (ss.str()); +} + +const std::string TrexPortOwner::g_unowned_name = "<FREE>"; +const std::string TrexPortOwner::g_unowned_handler = ""; diff --git a/src/stateless/cp/trex_stateless_port.h b/src/stateless/cp/trex_stateless_port.h index b061a414..a529d38f 100644 --- a/src/stateless/cp/trex_stateless_port.h +++ b/src/stateless/cp/trex_stateless_port.h @@ -26,6 +26,79 @@ limitations under the License. #include <internal_api/trex_platform_api.h> class TrexStatelessCpToDpMsgBase; +class TrexStreamsGraphObj; +class TrexPortMultiplier; + +/** + * TRex port owner can perform + * write commands + * while port is owned - others can + * do read only commands + * + */ +class TrexPortOwner { +public: + + TrexPortOwner(); + + /** + * is port free to acquire + */ + bool is_free() { + return m_is_free; + } + + void release() { + m_is_free = true; + m_owner_name = ""; + m_handler = ""; + } + + bool is_owned_by(const std::string &user) { + return ( !m_is_free && (m_owner_name == user) ); + } + + void own(const std::string &owner_name) { + + /* save user data */ + m_owner_name = owner_name; + + /* internal data */ + m_handler = generate_handler(); + m_is_free = false; + } + + bool verify(const std::string &handler) { + return ( (!m_is_free) && (m_handler == handler) ); + } + + const std::string &get_name() { + return (!m_is_free ? m_owner_name : g_unowned_name); + } + + const std::string &get_handler() { + return (!m_is_free ? m_handler : g_unowned_handler); + } + + +private: + std::string generate_handler(); + + /* is this port owned by someone ? */ + bool m_is_free; + + /* user provided info */ + std::string m_owner_name; + + /* handler genereated internally */ + std::string m_handler; + + + /* just references defaults... */ + static const std::string g_unowned_name; + static const std::string g_unowned_handler; +}; + /** * describes a stateless port @@ -58,13 +131,14 @@ public: RC_ERR_FAILED_TO_COMPILE_STREAMS }; + TrexStatelessPort(uint8_t port_id, const TrexPlatformApi *api); /** * acquire port * throws TrexException in case of an error */ - void acquire(const std::string &user, bool force = false); + void acquire(const std::string &user, uint32_t session_id, bool force = false); /** * release the port from the current user @@ -73,22 +147,20 @@ public: void release(void); /** - * start traffic - * throws TrexException in case of an error + * validate the state of the port before start + * it will return a stream graph + * containing information about the streams + * configured on this port + * + * on error it throws TrexException */ - void start_traffic(double mul, double duration = -1); + const TrexStreamsGraphObj *validate(void); /** - * given a BPS rate calculate ther correct M for this port - * - */ - double calculate_m_from_bps(double max_bps); - - /** - * given a PPS rate calculate the correct M for this port - * + * start traffic + * throws TrexException in case of an error */ - double calculate_m_from_pps(double max_pps); + void start_traffic(const TrexPortMultiplier &mul, double duration); /** * stop traffic @@ -112,7 +184,7 @@ public: * update current traffic on port * */ - void update_traffic(double mul); + void update_traffic(const TrexPortMultiplier &mul); /** * get the port state @@ -139,29 +211,6 @@ public: void get_properties(std::string &driver, TrexPlatformApi::driver_speed_e &speed); - /** - * query for ownership - * - */ - const std::string &get_owner() { - return m_owner; - } - - /** - * owner handler - * for the connection - * - */ - const std::string &get_owner_handler() { - return m_owner_handler; - } - - - bool verify_owner_handler(const std::string &handler) { - - return ( (m_owner != "none") && (m_owner_handler == handler) ); - - } /** * encode stats as JSON @@ -181,6 +230,7 @@ public: verify_state(PORT_STATE_IDLE | PORT_STATE_STREAMS); m_stream_table.add_stream(stream); + delete_streams_graph(); change_state(PORT_STATE_STREAMS); } @@ -189,6 +239,7 @@ public: verify_state(PORT_STATE_STREAMS); m_stream_table.remove_stream(stream); + delete_streams_graph(); if (m_stream_table.size() == 0) { change_state(PORT_STATE_IDLE); @@ -199,6 +250,7 @@ public: verify_state(PORT_STATE_IDLE | PORT_STATE_STREAMS); m_stream_table.remove_and_delete_all_streams(); + delete_streams_graph(); change_state(PORT_STATE_IDLE); } @@ -220,31 +272,34 @@ public: } - -private: - - - /** - * take ownership of the server array - * this is static - * ownership is total - * - */ - void set_owner(const std::string &owner) { - m_owner = owner; - m_owner_handler = generate_handler(); + * returns the number of DP cores linked to this port + * + */ + uint8_t get_dp_core_count() { + return m_cores_id_list.size(); } - void clear_owner() { - m_owner = "none"; - m_owner_handler = ""; + /** + * returns the traffic multiplier currently being used by the DP + * + */ + double get_multiplier() { + return (m_factor); } - bool is_free_to_aquire() { - return (m_owner == "none"); + /** + * get port speed in bits per second + * + */ + uint64_t get_port_speed_bps() const; + + TrexPortOwner & get_owner() { + return m_owner; } +private: + const std::vector<int> get_core_id_list () { return m_cores_id_list; @@ -256,7 +311,17 @@ private: std::string generate_handler(); - void send_message_to_dp(TrexStatelessCpToDpMsgBase *msg); + /** + * send message to all cores using duplicate + * + */ + void send_message_to_all_dp(TrexStatelessCpToDpMsgBase *msg); + + /** + * send message to specific DP core + * + */ + void send_message_to_dp(uint8_t core_id, TrexStatelessCpToDpMsgBase *msg); /** * triggered when event occurs @@ -265,22 +330,97 @@ private: void on_dp_event_occured(TrexDpPortEvent::event_e event_type); + /** + * calculate effective M per core + * + */ + double calculate_effective_factor(const TrexPortMultiplier &mul); + + + + /** + * generates a graph of streams graph + * + */ + void generate_streams_graph(); + + /** + * dispose of it + * + * @author imarom (26-Nov-15) + */ + void delete_streams_graph(); + + TrexStreamTable m_stream_table; uint8_t m_port_id; port_state_e m_port_state; - std::string m_owner; - std::string m_owner_handler; std::string m_driver_name; TrexPlatformApi::driver_speed_e m_speed; /* holds the DP cores associated with this port */ - std::vector<int> m_cores_id_list; + std::vector<int> m_cores_id_list; bool m_last_all_streams_continues; double m_last_duration; + double m_factor; TrexDpPortEvents m_dp_events; + + /* holds a graph of streams rate*/ + const TrexStreamsGraphObj *m_graph_obj; + + /* owner information */ + TrexPortOwner m_owner; +}; + + +/** + * port multiplier object + * + */ +class TrexPortMultiplier { +public: + + + /** + * defines the type of multipler passed to start + */ + enum mul_type_e { + MUL_FACTOR, + MUL_BPS, + MUL_PPS, + MUL_PERCENTAGE + }; + + /** + * multiplier can be absolute value + * increment value or subtract value + */ + enum mul_op_e { + OP_ABS, + OP_ADD, + OP_SUB + }; + + + TrexPortMultiplier(mul_type_e type, mul_op_e op, double value) { + m_type = type; + m_op = op; + m_value = value; + } + + TrexPortMultiplier(const std::string &type_str, const std::string &op_str, double value); + + +public: + static const std::initializer_list<std::string> g_types; + static const std::initializer_list<std::string> g_ops; + + mul_type_e m_type; + mul_op_e m_op; + double m_value; }; #endif /* __TREX_STATELESS_PORT_H__ */ diff --git a/src/stateless/cp/trex_stream.cpp b/src/stateless/cp/trex_stream.cpp index cad603e2..02f43a3a 100644 --- a/src/stateless/cp/trex_stream.cpp +++ b/src/stateless/cp/trex_stream.cpp @@ -53,6 +53,36 @@ std::string TrexStream::get_stream_type_str(stream_type_t stream_type){ } +void +TrexStream::compile() { + + /* in case there are no instructions - nothing to do */ + if (m_vm.is_vm_empty()) { + m_has_vm = false; + return; + } + + m_has_vm = true; + + m_vm.set_packet_size(m_pkt.len); + + m_vm.compile(); + + #if 0 + m_vm.Dump(stdout); + #endif + + m_vm_dp = m_vm.cloneAsVmDp(); + + /* calc m_vm_prefix_size which is the size of the writable packet */ + uint16_t max_pkt_offset = m_vm_dp->get_max_packet_update_offset(); + uint16_t pkt_size = m_pkt.len; + + /* calculate the mbuf size that we should allocate */ + m_vm_prefix_size = calc_writable_mbuf_size(max_pkt_offset, pkt_size); +} + + void TrexStream::Dump(FILE *fd){ fprintf(fd,"\n"); @@ -103,6 +133,8 @@ TrexStream::TrexStream(uint8_t type, m_pkt.binary = NULL; m_pkt.len = 0; + m_has_vm = false; + m_vm_prefix_size = 0; m_rx_check.m_enable = false; @@ -111,12 +143,17 @@ TrexStream::TrexStream(uint8_t type, m_burst_total_pkts=0; m_num_bursts=1; m_ibg_usec=0.0; + m_vm_dp = NULL; } TrexStream::~TrexStream() { if (m_pkt.binary) { delete [] m_pkt.binary; } + if ( m_vm_dp ){ + delete m_vm_dp; + m_vm_dp=NULL; + } } void @@ -199,3 +236,4 @@ int TrexStreamTable::size() { return m_stream_table.size(); } + diff --git a/src/stateless/cp/trex_stream.h b/src/stateless/cp/trex_stream.h index 3e48d7e4..720246f6 100644 --- a/src/stateless/cp/trex_stream.h +++ b/src/stateless/cp/trex_stream.h @@ -36,6 +36,54 @@ limitations under the License. class TrexRpcCmdAddStream; +static inline uint16_t get_log2_size(uint16_t size){ + + uint16_t _sizes[]={64,128,256,512,1024,2048}; + int i; + for (i=0; i<sizeof(_sizes)/sizeof(_sizes[0]); i++) { + if (size<=_sizes[i]) { + return (_sizes[i]); + } + } + assert(0); + return (0); +} + +/** + * calculate the size of writable mbuf in bytes. maximum size if packet size + * + * @param max_offset_writable + * the last byte that we don't write too. for example when 63 it means that bytes [62] in the array is written (zero base) + * @param pkt_size packet size in bytes + * + * @return the writable size of the first mbuf . the idea is to give at least 64 bytes const mbuf else all packet will be writeable + * + * examples: + * max_offset_writable =63 + * pkt_size =62 + * ==>62 + * + */ +static inline uint16_t calc_writable_mbuf_size(uint16_t max_offset_writable, + uint16_t pkt_size){ + + if ( pkt_size<=64 ){ + return (pkt_size); + } + if (pkt_size<=128) { + return (pkt_size); + } + + //pkt_size> 128 + uint16_t non_writable = pkt_size - (max_offset_writable -1) ; + if ( non_writable<64 ) { + return (pkt_size); + } + return(max_offset_writable-1); +} + + + struct CStreamPktData { uint8_t *binary; uint16_t len; @@ -129,9 +177,18 @@ public: } /* create new stream */ - TrexStream * clone_as_dp(){ - TrexStream * dp=new TrexStream(m_type,m_port_id,m_stream_id); - + TrexStream * clone_as_dp() const { + + TrexStream *dp = new TrexStream(m_type,m_port_id,m_stream_id); + dp->m_has_vm = m_has_vm; + if (m_vm_dp) { + /* should have vm */ + assert(m_has_vm); + dp->m_vm_dp = m_vm_dp->clone(); + }else{ + dp->m_vm_dp = NULL; + } + dp->m_vm_prefix_size = m_vm_prefix_size; dp->m_isg_usec = m_isg_usec; dp->m_next_stream_id = m_next_stream_id; @@ -161,10 +218,28 @@ public: } void Dump(FILE *fd); + + bool is_vm(){ + return ( m_has_vm ); + } + + StreamVmDp * getDpVm(){ + return ( m_vm_dp); + } + + void post_vm_compile(); + + /** + * internal compilation of stream (for DP) + * + */ + void compile(); + public: /* basic */ uint8_t m_type; uint8_t m_port_id; + uint16_t m_vm_prefix_size; /* writeable mbuf size */ uint32_t m_stream_id; /* id from RPC can be anything */ @@ -175,6 +250,10 @@ public: /* indicators */ bool m_enabled; bool m_self_start; + bool m_has_vm; /* do we have instructions to run */ + + + StreamVmDp * m_vm_dp; /* compile VM */ CStreamPktData m_pkt; /* pkt */ diff --git a/src/stateless/cp/trex_stream_vm.cpp b/src/stateless/cp/trex_stream_vm.cpp index 2e760ae9..e10e1a81 100644 --- a/src/stateless/cp/trex_stream_vm.cpp +++ b/src/stateless/cp/trex_stream_vm.cpp @@ -1,5 +1,6 @@ /* Itay Marom + Hanoch Haim Cisco Systems, Inc. */ @@ -19,6 +20,108 @@ See the License for the specific language governing permissions and limitations under the License. */ #include <trex_stream_vm.h> +#include <sstream> +#include <assert.h> +#include <iostream> +#include <trex_stateless.h> +#include <common/Network/Packet/IPHeader.h> +#include <common/basic_utils.h> + + + + +void StreamVmInstructionFixChecksumIpv4::Dump(FILE *fd){ + fprintf(fd," fix_check_sum , %lu \n",(ulong)m_pkt_offset); +} + + +void StreamVmInstructionFlowMan::sanity_check_valid_size(uint32_t ins_id,StreamVm *lp){ + uint8_t valid[]={1,2,4,8}; + int i; + for (i=0; i<sizeof(valid)/sizeof(valid[0]); i++) { + if (valid[i]==m_size_bytes) { + return; + } + } + + std::stringstream ss; + + ss << "instruction id '" << ins_id << "' has non valid length " << m_size_bytes ; + + lp->err(ss.str()); +} + +void StreamVmInstructionFlowMan::sanity_check_valid_opt(uint32_t ins_id,StreamVm *lp){ + uint8_t valid[]={FLOW_VAR_OP_INC, + FLOW_VAR_OP_DEC, + FLOW_VAR_OP_RANDOM}; + int i; + for (i=0; i<sizeof(valid)/sizeof(valid[0]); i++) { + if (valid[i]==m_op) { + return; + } + } + + std::stringstream ss; + + ss << "instruction id '" << ins_id << "' has non valid op " << (int)m_op ; + + lp->err(ss.str()); +} + +void StreamVmInstructionFlowMan::sanity_check_valid_range(uint32_t ins_id,StreamVm *lp){ + //TBD check that init,min,max in valid range +} + + + +void StreamVmInstructionFlowMan::sanity_check(uint32_t ins_id,StreamVm *lp){ + + sanity_check_valid_size(ins_id,lp); + sanity_check_valid_opt(ins_id,lp); + sanity_check_valid_range(ins_id,lp); +} + + +void StreamVmInstructionFlowMan::Dump(FILE *fd){ + fprintf(fd," flow_var , %s ,%lu, ",m_var_name.c_str(),(ulong)m_size_bytes); + + switch (m_op) { + + case FLOW_VAR_OP_INC : + fprintf(fd," INC ,"); + break; + case FLOW_VAR_OP_DEC : + fprintf(fd," DEC ,"); + break; + case FLOW_VAR_OP_RANDOM : + fprintf(fd," RANDOM ,"); + break; + default: + fprintf(fd," UNKNOWN,"); + break; + }; + + fprintf(fd," (%lu:%lu:%lu) \n",m_init_value,m_min_value,m_max_value); +} + + +void StreamVmInstructionWriteToPkt::Dump(FILE *fd){ + + fprintf(fd," write_pkt , %s ,%lu, add, %ld, big, %lu \n",m_flow_var_name.c_str(),(ulong)m_pkt_offset,(long)m_add_value,(ulong)(m_is_big_endian?1:0)); +} + + + +void StreamVmInstructionFlowClient::Dump(FILE *fd){ + + fprintf(fd," client_var ,%s , ",m_var_name.c_str()); + + fprintf(fd," ip:(%x-%x) port:(%x-%x) flow_limit:%lu flags: %x\n",m_client_min,m_client_max, m_port_min,m_port_max,(ulong)m_limit_num_flows,m_flags); +} + + + /*************************** * StreamVmInstruction @@ -36,19 +139,714 @@ void StreamVm::add_instruction(StreamVmInstruction *inst) { m_inst_list.push_back(inst); } +StreamDPVmInstructions * +StreamVm::get_dp_instruction_buffer(){ + return &m_instructions; +} + + const std::vector<StreamVmInstruction *> & StreamVm::get_instruction_list() { return m_inst_list; } -bool StreamVm::compile() { - /* implement me */ - return (false); +void StreamVm::var_clear_table(){ + m_flow_var_offset.clear(); +} + +bool StreamVm::var_add(const std::string &var_name,VmFlowVarRec & var){ + m_flow_var_offset[var_name] = var; + return (true); +} + + +uint16_t StreamVm::get_var_offset(const std::string &var_name){ + VmFlowVarRec var; + bool res=var_lookup(var_name,var); + assert(res); + return (var.m_offset); +} + + +bool StreamVm::var_lookup(const std::string &var_name,VmFlowVarRec & var){ + auto search = m_flow_var_offset.find(var_name); + + if (search != m_flow_var_offset.end()) { + var = search->second; + return true; + } else { + return false; + } +} + + + +void StreamVm::err(const std::string &err){ + throw TrexException("*** error: " + err); +} + + +void StreamVm::build_flow_var_table() { + + var_clear_table(); + m_cur_var_offset=0; + uint32_t ins_id=0; + /* scan all flow var instruction and build */ + for (auto inst : m_inst_list) { + if ( inst->get_instruction_type() == StreamVmInstruction::itFLOW_MAN ){ + + StreamVmInstructionFlowMan * ins_man=(StreamVmInstructionFlowMan *)inst; + + /* check that instruction is valid */ + ins_man->sanity_check(ins_id,this); + + VmFlowVarRec var; + /* if this is the first time */ + if ( var_lookup( ins_man->m_var_name,var) == true){ + std::stringstream ss; + ss << "instruction id '" << ins_id << "' flow variable name " << ins_man->m_var_name << " already exists"; + err(ss.str()); + }else{ + var.m_offset=m_cur_var_offset; + var.m_ins.m_ins_flowv = ins_man; + var.m_size_bytes = ins_man->m_size_bytes; + var_add(ins_man->m_var_name,var); + m_cur_var_offset += ins_man->m_size_bytes; + + } + } + + if ( inst->get_instruction_type() == StreamVmInstruction::itFLOW_CLIENT ){ + StreamVmInstructionFlowClient * ins_man=(StreamVmInstructionFlowClient *)inst; + + VmFlowVarRec var; + /* if this is the first time */ + if ( var_lookup( ins_man->m_var_name+".ip",var) == true){ + std::stringstream ss; + ss << "instruction id '" << ins_id << "' client variable name " << ins_man->m_var_name << " already exists"; + err(ss.str()); + } + if ( var_lookup( ins_man->m_var_name+".port",var) == true){ + std::stringstream ss; + ss << "instruction id '" << ins_id << "' client variable name " << ins_man->m_var_name << " already exists"; + err(ss.str()); + } + + if ( var_lookup( ins_man->m_var_name+".flow_limit",var) == true){ + std::stringstream ss; + ss << "instruction id '" << ins_id << "' client variable name " << ins_man->m_var_name << " already exists"; + err(ss.str()); + } + + var.m_offset = m_cur_var_offset; + var.m_ins.m_ins_flow_client = ins_man; + var.m_size_bytes =4; + + VmFlowVarRec var_port; + + var_port.m_offset = m_cur_var_offset+4; + var_port.m_ins.m_ins_flow_client = ins_man; + var_port.m_size_bytes =2; + + VmFlowVarRec var_flow_limit; + + var_flow_limit.m_offset = m_cur_var_offset+6; + var_flow_limit.m_ins.m_ins_flow_client = ins_man; + var_flow_limit.m_size_bytes =4; + + + var_add(ins_man->m_var_name+".ip",var); + var_add(ins_man->m_var_name+".port",var_port); + var_add(ins_man->m_var_name+".flow_limit",var_flow_limit); + + m_cur_var_offset += StreamVmInstructionFlowClient::get_flow_var_size(); + + assert(sizeof(StreamDPFlowClient)==StreamVmInstructionFlowClient::get_flow_var_size()); + } + + /* limit the flow var size */ + if (m_cur_var_offset > StreamVm::svMAX_FLOW_VAR ) { + std::stringstream ss; + ss << "too many flow varibles current size is :" << m_cur_var_offset << " maximum support is " << StreamVm::svMAX_FLOW_VAR; + err(ss.str()); + } + ins_id++; + } + +} + +void StreamVm::alloc_bss(){ + free_bss(); + m_bss=(uint8_t *)malloc(m_cur_var_offset); +} + +void StreamVm::clean_max_field_cnt(){ + m_max_field_update=0; +} + +void StreamVm::add_field_cnt(uint16_t new_cnt){ + + if ( new_cnt > m_max_field_update) { + m_max_field_update = new_cnt; + } +} + + +void StreamVm::free_bss(){ + if (m_bss) { + free(m_bss); + m_bss=0; + } +} + + +void StreamVm::build_program(){ + + /* build the commands into a buffer */ + m_instructions.clear(); + clean_max_field_cnt(); + uint32_t ins_id=0; + + for (auto inst : m_inst_list) { + StreamVmInstruction::instruction_type_t ins_type=inst->get_instruction_type(); + + /* itFIX_IPV4_CS */ + if (ins_type == StreamVmInstruction::itFIX_IPV4_CS) { + StreamVmInstructionFixChecksumIpv4 *lpFix =(StreamVmInstructionFixChecksumIpv4 *)inst; + + add_field_cnt(lpFix->m_pkt_offset +12); + + if ( (lpFix->m_pkt_offset + IPV4_HDR_LEN) > m_pkt_size ) { + + std::stringstream ss; + ss << "instruction id '" << ins_id << "' fix ipv4 command offset " << lpFix->m_pkt_offset << " is too high relative to packet size "<< m_pkt_size; + err(ss.str()); + } + + StreamDPOpIpv4Fix ipv_fix; + ipv_fix.m_offset = lpFix->m_pkt_offset; + ipv_fix.m_op = StreamDPVmInstructions::ditFIX_IPV4_CS; + m_instructions.add_command(&ipv_fix,sizeof(ipv_fix)); + } + + + /* flow man */ + if (ins_type == StreamVmInstruction::itFLOW_MAN) { + StreamVmInstructionFlowMan *lpMan =(StreamVmInstructionFlowMan *)inst; + + + if (lpMan->m_size_bytes == 1 ){ + + uint8_t op=StreamDPVmInstructions::ditINC8; + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_INC ){ + op = StreamDPVmInstructions::ditINC8 ; + } + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_DEC ){ + op = StreamDPVmInstructions::ditDEC8 ; + } + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_RANDOM ){ + op = StreamDPVmInstructions::ditRANDOM8 ; + } + + StreamDPOpFlowVar8 fv8; + fv8.m_op = op; + fv8.m_flow_offset = get_var_offset(lpMan->m_var_name); + fv8.m_min_val = (uint8_t)lpMan->m_min_value; + fv8.m_max_val = (uint8_t)lpMan->m_max_value; + m_instructions.add_command(&fv8,sizeof(fv8)); + } + + if (lpMan->m_size_bytes == 2 ){ + uint8_t op; + + op = StreamDPVmInstructions::ditINC16; + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_INC ){ + op = StreamDPVmInstructions::ditINC16 ; + } + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_DEC ){ + op = StreamDPVmInstructions::ditDEC16 ; + } + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_RANDOM ){ + op = StreamDPVmInstructions::ditRANDOM16 ; + } + + StreamDPOpFlowVar16 fv16; + fv16.m_op = op; + fv16.m_flow_offset = get_var_offset(lpMan->m_var_name); + fv16.m_min_val = (uint16_t)lpMan->m_min_value; + fv16.m_max_val = (uint16_t)lpMan->m_max_value; + m_instructions.add_command(&fv16,sizeof(fv16)); + } + + if (lpMan->m_size_bytes == 4 ){ + uint8_t op; + + op = StreamDPVmInstructions::ditINC32; + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_INC ){ + op = StreamDPVmInstructions::ditINC32 ; + } + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_DEC ){ + op = StreamDPVmInstructions::ditDEC32 ; + } + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_RANDOM ){ + op = StreamDPVmInstructions::ditRANDOM32 ; + } + + StreamDPOpFlowVar32 fv32; + fv32.m_op = op; + fv32.m_flow_offset = get_var_offset(lpMan->m_var_name); + fv32.m_min_val = (uint32_t)lpMan->m_min_value; + fv32.m_max_val = (uint32_t)lpMan->m_max_value; + m_instructions.add_command(&fv32,sizeof(fv32)); + } + + if (lpMan->m_size_bytes == 8 ){ + uint8_t op; + + op = StreamDPVmInstructions::ditINC64; + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_INC ){ + op = StreamDPVmInstructions::ditINC64 ; + } + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_DEC ){ + op = StreamDPVmInstructions::ditDEC64 ; + } + + if ( lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_RANDOM ){ + op = StreamDPVmInstructions::ditRANDOM64 ; + } + + StreamDPOpFlowVar32 fv64; + fv64.m_op = op; + fv64.m_flow_offset = get_var_offset(lpMan->m_var_name); + fv64.m_min_val = (uint64_t)lpMan->m_min_value; + fv64.m_max_val = (uint64_t)lpMan->m_max_value; + m_instructions.add_command(&fv64,sizeof(fv64)); + } + } + + if (ins_type == StreamVmInstruction::itPKT_WR) { + StreamVmInstructionWriteToPkt *lpPkt =(StreamVmInstructionWriteToPkt *)inst; + + VmFlowVarRec var; + if ( var_lookup(lpPkt->m_flow_var_name ,var) == false){ + + std::stringstream ss; + ss << "instruction id '" << ins_id << "' packet write with no valid flow varible name '" << lpPkt->m_flow_var_name << "'" ; + err(ss.str()); + } + + if (lpPkt->m_pkt_offset + var.m_size_bytes > m_pkt_size ) { + std::stringstream ss; + ss << "instruction id '" << ins_id << "' packet write with packet_offset " << lpPkt->m_pkt_offset + var.m_size_bytes << " bigger than packet size "<< m_pkt_size; + err(ss.str()); + } + add_field_cnt(lpPkt->m_pkt_offset + var.m_size_bytes); + + + uint8_t op_size=var.m_size_bytes; + bool is_big = lpPkt->m_is_big_endian; + uint8_t flags = (is_big?StreamDPOpPktWrBase::PKT_WR_IS_BIG:0); + uint8_t flow_offset = get_var_offset(lpPkt->m_flow_var_name); + + if (op_size == 1) { + StreamDPOpPktWr8 pw8; + pw8.m_op = StreamDPVmInstructions::itPKT_WR8; + pw8.m_flags =flags; + pw8.m_offset =flow_offset; + pw8.m_pkt_offset = lpPkt->m_pkt_offset; + pw8.m_val_offset = (int8_t)lpPkt->m_add_value; + m_instructions.add_command(&pw8,sizeof(pw8)); + } + + if (op_size == 2) { + StreamDPOpPktWr16 pw16; + pw16.m_op = StreamDPVmInstructions::itPKT_WR16; + pw16.m_flags =flags; + pw16.m_offset =flow_offset; + pw16.m_pkt_offset = lpPkt->m_pkt_offset; + pw16.m_val_offset = (int16_t)lpPkt->m_add_value; + m_instructions.add_command(&pw16,sizeof(pw16)); + } + + if (op_size == 4) { + StreamDPOpPktWr32 pw32; + pw32.m_op = StreamDPVmInstructions::itPKT_WR32; + pw32.m_flags =flags; + pw32.m_offset =flow_offset; + pw32.m_pkt_offset = lpPkt->m_pkt_offset; + pw32.m_val_offset = (int32_t)lpPkt->m_add_value; + m_instructions.add_command(&pw32,sizeof(pw32)); + } + + if (op_size == 8) { + StreamDPOpPktWr64 pw64; + pw64.m_op = StreamDPVmInstructions::itPKT_WR64; + pw64.m_flags =flags; + pw64.m_offset =flow_offset; + pw64.m_pkt_offset = lpPkt->m_pkt_offset; + pw64.m_val_offset = (int64_t)lpPkt->m_add_value; + m_instructions.add_command(&pw64,sizeof(pw64)); + } + + } + + + if (ins_type == StreamVmInstruction::itFLOW_CLIENT) { + StreamVmInstructionFlowClient *lpMan =(StreamVmInstructionFlowClient *)inst; + + if ( lpMan->is_unlimited_flows() ){ + StreamDPOpClientsUnLimit client_cmd; + client_cmd.m_op = StreamDPVmInstructions::itCLIENT_VAR_UNLIMIT; + + client_cmd.m_flow_offset = get_var_offset(lpMan->m_var_name+".ip"); /* start offset */ + client_cmd.m_flags = 0; /* not used */ + client_cmd.m_pad = 0; + client_cmd.m_min_ip = lpMan->m_client_min; + client_cmd.m_max_ip = lpMan->m_client_max; + m_instructions.add_command(&client_cmd,sizeof(client_cmd)); + + }else{ + StreamDPOpClientsLimit client_cmd; + client_cmd.m_op = StreamDPVmInstructions::itCLIENT_VAR; + + client_cmd.m_flow_offset = get_var_offset(lpMan->m_var_name+".ip"); /* start offset */ + client_cmd.m_flags = 0; /* not used */ + client_cmd.m_pad = 0; + client_cmd.m_min_port = lpMan->m_port_min; + client_cmd.m_max_port = lpMan->m_port_max; + client_cmd.m_min_ip = lpMan->m_client_min; + client_cmd.m_max_ip = lpMan->m_client_max; + client_cmd.m_limit_flows = lpMan->m_limit_num_flows; + m_instructions.add_command(&client_cmd,sizeof(client_cmd)); + } + } + + ins_id++; + } +} + + +void StreamVm::build_bss() { + alloc_bss(); + uint8_t * p=(uint8_t *)m_bss; + + for (auto inst : m_inst_list) { + + if ( inst->get_instruction_type() == StreamVmInstruction::itFLOW_MAN ){ + + StreamVmInstructionFlowMan * ins_man=(StreamVmInstructionFlowMan *)inst; + + switch (ins_man->m_size_bytes) { + case 1: + *p=(uint8_t)ins_man->m_init_value; + p+=1; + break; + case 2: + *((uint16_t*)p)=(uint16_t)ins_man->m_init_value; + p+=2; + break; + case 4: + *((uint32_t*)p)=(uint32_t)ins_man->m_init_value; + p+=4; + break; + case 8: + *((uint64_t*)p)=(uint64_t)ins_man->m_init_value; + p+=8; + break; + default: + assert(0); + } + } + + if ( inst->get_instruction_type() == StreamVmInstruction::itFLOW_CLIENT ){ + + StreamVmInstructionFlowClient * ins_man=(StreamVmInstructionFlowClient *)inst; + if (ins_man->m_client_min>0) { + *((uint32_t*)p)=(uint32_t)(ins_man->m_client_min-1); + }else{ + *((uint32_t*)p)=(uint32_t)ins_man->m_client_min; + } + p+=4; + + if (ins_man->is_unlimited_flows() ) { + *((uint16_t*)p)=StreamDPOpClientsUnLimit::CLIENT_UNLIMITED_MIN_PORT; + }else{ + *((uint16_t*)p)=(uint16_t)ins_man->m_port_min; + } + p+=2; + + *((uint32_t*)p)=0; + p+=4; + } + + } +} + + + +void StreamVm::compile() { + + /* build flow var offset table */ + build_flow_var_table() ; + + /* build init flow var memory */ + build_bss(); + + build_program(); + + if ( get_max_packet_update_offset() >svMAX_PACKET_OFFSET_CHANGE ){ + std::stringstream ss; + ss << "maximum offset is" << get_max_packet_update_offset() << " bigger than maximum " <<svMAX_PACKET_OFFSET_CHANGE; + err(ss.str()); + } } + StreamVm::~StreamVm() { for (auto inst : m_inst_list) { delete inst; + } + free_bss(); +} + + +void StreamVm::Dump(FILE *fd){ + fprintf(fd," instructions \n"); + uint32_t cnt=0; + for (auto inst : m_inst_list) { + fprintf(fd," [%04lu] : ",(ulong)cnt); + inst->Dump(fd); + cnt++; } + + if ( get_bss_size() ) { + fprintf(fd," BSS size %lu\n",(ulong)get_bss_size()); + utl_DumpBuffer(fd,get_bss_ptr(),get_bss_size(),0); + } + + if ( m_instructions.get_program_size() > 0 ){ + fprintf(fd," RAW instructions \n"); + m_instructions.Dump(fd); + } +} + + +void StreamDPVmInstructions::clear(){ + m_inst_list.clear(); +} + + +void StreamDPVmInstructions::add_command(void *buffer,uint16_t size){ + int i; + uint8_t *p= (uint8_t *)buffer; + /* push byte by byte */ + for (i=0; i<size; i++) { + m_inst_list.push_back(*p); + p++; + } +} + +uint8_t * StreamDPVmInstructions::get_program(){ + return (&m_inst_list[0]); +} + +uint32_t StreamDPVmInstructions::get_program_size(){ + return (m_inst_list.size()); +} + +void StreamDPVmInstructions::Dump(FILE *fd){ + + uint8_t * p=get_program(); + + + uint32_t program_size = get_program_size(); + uint8_t * p_end=p+program_size; + + StreamDPOpFlowVar8 *lpv8; + StreamDPOpFlowVar16 *lpv16; + StreamDPOpFlowVar32 *lpv32; + StreamDPOpFlowVar64 *lpv64; + StreamDPOpIpv4Fix *lpIpv4Fix; + StreamDPOpPktWr8 *lpw8; + StreamDPOpPktWr16 *lpw16; + StreamDPOpPktWr32 *lpw32; + StreamDPOpPktWr64 *lpw64; + StreamDPOpClientsLimit *lp_client; + StreamDPOpClientsUnLimit *lp_client_unlimited; + + + while ( p < p_end) { + uint8_t op_code=*p; + switch (op_code) { + + case ditINC8 : + lpv8 =(StreamDPOpFlowVar8 *)p; + lpv8->dump(fd,"INC8"); + p+=sizeof(StreamDPOpFlowVar8); + break; + case ditINC16 : + lpv16 =(StreamDPOpFlowVar16 *)p; + lpv16->dump(fd,"INC16"); + p+=sizeof(StreamDPOpFlowVar16); + break; + case ditINC32 : + lpv32 =(StreamDPOpFlowVar32 *)p; + lpv32->dump(fd,"INC32"); + p+=sizeof(StreamDPOpFlowVar32); + break; + case ditINC64 : + lpv64 =(StreamDPOpFlowVar64 *)p; + lpv64->dump(fd,"INC64"); + p+=sizeof(StreamDPOpFlowVar64); + break; + + case ditDEC8 : + lpv8 =(StreamDPOpFlowVar8 *)p; + lpv8->dump(fd,"DEC8"); + p+=sizeof(StreamDPOpFlowVar8); + break; + case ditDEC16 : + lpv16 =(StreamDPOpFlowVar16 *)p; + lpv16->dump(fd,"DEC16"); + p+=sizeof(StreamDPOpFlowVar16); + break; + case ditDEC32 : + lpv32 =(StreamDPOpFlowVar32 *)p; + lpv32->dump(fd,"DEC32"); + p+=sizeof(StreamDPOpFlowVar32); + break; + case ditDEC64 : + lpv64 =(StreamDPOpFlowVar64 *)p; + lpv64->dump(fd,"DEC64"); + p+=sizeof(StreamDPOpFlowVar64); + break; + + case ditRANDOM8 : + lpv8 =(StreamDPOpFlowVar8 *)p; + lpv8->dump(fd,"RAND8"); + p+=sizeof(StreamDPOpFlowVar8); + break; + case ditRANDOM16 : + lpv16 =(StreamDPOpFlowVar16 *)p; + lpv16->dump(fd,"RAND16"); + p+=sizeof(StreamDPOpFlowVar16); + break; + case ditRANDOM32 : + lpv32 =(StreamDPOpFlowVar32 *)p; + lpv32->dump(fd,"RAND32"); + p+=sizeof(StreamDPOpFlowVar32); + break; + case ditRANDOM64 : + lpv64 =(StreamDPOpFlowVar64 *)p; + lpv64->dump(fd,"RAND64"); + p+=sizeof(StreamDPOpFlowVar64); + break; + + case ditFIX_IPV4_CS : + lpIpv4Fix =(StreamDPOpIpv4Fix *)p; + lpIpv4Fix->dump(fd,"Ipv4Fix"); + p+=sizeof(StreamDPOpIpv4Fix); + break; + + case itPKT_WR8 : + lpw8 =(StreamDPOpPktWr8 *)p; + lpw8->dump(fd,"Wr8"); + p+=sizeof(StreamDPOpPktWr8); + break; + + case itPKT_WR16 : + lpw16 =(StreamDPOpPktWr16 *)p; + lpw16->dump(fd,"Wr16"); + p+=sizeof(StreamDPOpPktWr16); + break; + + case itPKT_WR32 : + lpw32 =(StreamDPOpPktWr32 *)p; + lpw32->dump(fd,"Wr32"); + p+=sizeof(StreamDPOpPktWr32); + break; + + case itPKT_WR64 : + lpw64 =(StreamDPOpPktWr64 *)p; + lpw64->dump(fd,"Wr64"); + p+=sizeof(StreamDPOpPktWr64); + break; + + case itCLIENT_VAR : + lp_client =(StreamDPOpClientsLimit *)p; + lp_client->dump(fd,"Client"); + p+=sizeof(StreamDPOpClientsLimit); + break; + + case itCLIENT_VAR_UNLIMIT : + lp_client_unlimited =(StreamDPOpClientsUnLimit *)p; + lp_client_unlimited->dump(fd,"ClientUnlimted"); + p+=sizeof(StreamDPOpClientsUnLimit); + break; + + + default: + assert(0); + } + }; +} + + +void StreamDPOpFlowVar8::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, of:%lu, (%lu- %lu) \n", opt.c_str(),(ulong)m_op,(ulong)m_flow_offset,(ulong)m_min_val,(ulong)m_max_val); +} + +void StreamDPOpFlowVar16::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, of:%lu, (%lu-%lu) \n", opt.c_str(),(ulong)m_op,(ulong)m_flow_offset,(ulong)m_min_val,(ulong)m_max_val); +} + +void StreamDPOpFlowVar32::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, of:%lu, (%lu-%lu) \n", opt.c_str(),(ulong)m_op,(ulong)m_flow_offset,(ulong)m_min_val,(ulong)m_max_val); +} + +void StreamDPOpFlowVar64::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, of:%lu, (%lu-%lu) \n", opt.c_str(),(ulong)m_op,(ulong)m_flow_offset,(ulong)m_min_val,(ulong)m_max_val); +} + +void StreamDPOpPktWr8::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, flags:%lu, pkt_of:%lu, f_of:%lu \n", opt.c_str(),(ulong)m_op,(ulong)m_flags,(ulong)m_pkt_offset,(ulong)m_offset); } +void StreamDPOpPktWr16::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, flags:%lu, pkt_of:%lu , f_of:%lu \n", opt.c_str(),(ulong)m_op,(ulong)m_flags,(ulong)m_pkt_offset,(ulong)m_offset); +} + +void StreamDPOpPktWr32::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, flags:%lu, pkt_of:%lu , f_of:%lu \n", opt.c_str(),(ulong)m_op,(ulong)m_flags,(ulong)m_pkt_offset,(ulong)m_offset); +} + +void StreamDPOpPktWr64::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, flags:%lu, pkt_of:%lu , f_of:%lu \n", opt.c_str(),(ulong)m_op,(ulong)m_flags,(ulong)m_pkt_offset,(ulong)m_offset); +} + + +void StreamDPOpIpv4Fix::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, offset: %lu \n", opt.c_str(),(ulong)m_op,(ulong)m_offset); +} + + +void StreamDPOpClientsLimit::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, flow_offset: %lu (%x-%x) (%x-%x) flow_limit :%lu flags:%x \n", opt.c_str(),(ulong)m_op,(ulong)m_flow_offset,m_min_ip,m_max_ip,m_min_port,m_max_port,(ulong)m_limit_flows,m_flags); +} + +void StreamDPOpClientsUnLimit::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, flow_offset: %lu (%x-%x) flags:%x \n", opt.c_str(),(ulong)m_op,(ulong)m_flow_offset,m_min_ip,m_max_ip,m_flags); +} + + diff --git a/src/stateless/cp/trex_stream_vm.h b/src/stateless/cp/trex_stream_vm.h index 56edbcaf..e65a87e3 100644 --- a/src/stateless/cp/trex_stream_vm.h +++ b/src/stateless/cp/trex_stream_vm.h @@ -24,6 +24,526 @@ limitations under the License. #include <string> #include <stdint.h> #include <vector> +#include <unordered_map> +#include <assert.h> +#include <common/Network/Packet/IPHeader.h> +#include "pal_utl.h" +#include "mbuf.h" + + + + +class StreamVm; + + +/* in memory program */ + +struct StreamDPOpFlowVar8 { + uint8_t m_op; + uint8_t m_flow_offset; + uint8_t m_min_val; + uint8_t m_max_val; +public: + void dump(FILE *fd,std::string opt); + + inline void run_inc(uint8_t * flow_var) { + uint8_t * p=(flow_var+m_flow_offset); + *p=*p+1; + if (*p>m_max_val) { + *p=m_min_val; + } + } + + inline void run_dec(uint8_t * flow_var) { + uint8_t * p=(flow_var+m_flow_offset); + *p=*p-1; + if (*p<m_min_val) { + *p=m_max_val; + } + } + + inline void run_rand(uint8_t * flow_var) { + uint8_t * p=(flow_var+m_flow_offset); + *p= m_min_val + (rand() % (int)(m_max_val - m_min_val + 1)); + } + + +} __attribute__((packed)) ; + +struct StreamDPOpFlowVar16 { + uint8_t m_op; + uint8_t m_flow_offset; + uint16_t m_min_val; + uint16_t m_max_val; +public: + void dump(FILE *fd,std::string opt); + + inline void run_inc(uint8_t * flow_var) { + uint16_t * p=(uint16_t *)(flow_var+m_flow_offset); + *p=*p+1; + if (*p>m_max_val) { + *p=m_min_val; + } + } + + inline void run_dec(uint8_t * flow_var) { + uint16_t * p=(uint16_t *)(flow_var+m_flow_offset); + *p=*p-1; + if (*p<m_min_val) { + *p=m_max_val; + } + } + + inline void run_rand(uint8_t * flow_var) { + uint16_t * p=(uint16_t *)(flow_var+m_flow_offset); + *p= m_min_val + (rand() % (int)(m_max_val - m_min_val + 1)); + } + + + +} __attribute__((packed)) ; + +struct StreamDPOpFlowVar32 { + uint8_t m_op; + uint8_t m_flow_offset; + uint32_t m_min_val; + uint32_t m_max_val; +public: + void dump(FILE *fd,std::string opt); + + inline void run_inc(uint8_t * flow_var) { + uint32_t * p=(uint32_t *)(flow_var+m_flow_offset); + *p=*p+1; + if (*p>m_max_val) { + *p=m_min_val; + } + } + + inline void run_dec(uint8_t * flow_var) { + uint32_t * p=(uint32_t *)(flow_var+m_flow_offset); + *p=*p-1; + if (*p<m_min_val) { + *p=m_max_val; + } + } + + inline void run_rand(uint8_t * flow_var) { + uint32_t * p=(uint32_t *)(flow_var+m_flow_offset); + *p= m_min_val + (rand() % (int)(m_max_val - m_min_val + 1)); + } + +} __attribute__((packed)) ; + +struct StreamDPOpFlowVar64 { + uint8_t m_op; + uint8_t m_flow_offset; + uint64_t m_min_val; + uint64_t m_max_val; +public: + void dump(FILE *fd,std::string opt); + + inline void run_inc(uint8_t * flow_var) { + uint64_t * p=(uint64_t *)(flow_var+m_flow_offset); + *p=*p+1; + if (*p>m_max_val) { + *p=m_min_val; + } + } + + inline void run_dec(uint8_t * flow_var) { + uint64_t * p=(uint64_t *)(flow_var+m_flow_offset); + *p=*p-1; + if (*p<m_min_val) { + *p=m_max_val; + } + } + + inline void run_rand(uint8_t * flow_var) { + uint64_t * p=(uint64_t *)(flow_var+m_flow_offset); + *p= m_min_val + (rand() % (int)(m_max_val - m_min_val + 1)); + } + + +} __attribute__((packed)) ; + + +struct StreamDPOpPktWrBase { + enum { + PKT_WR_IS_BIG = 1 + }; /* for flags */ + + uint8_t m_op; + uint8_t m_flags; + uint8_t m_offset; + +public: + bool is_big(){ + return ( (m_flags &StreamDPOpPktWrBase::PKT_WR_IS_BIG) == StreamDPOpPktWrBase::PKT_WR_IS_BIG ?true:false); + } + +} __attribute__((packed)) ; + + +struct StreamDPOpPktWr8 : public StreamDPOpPktWrBase { + int8_t m_val_offset; + uint16_t m_pkt_offset; + +public: + void dump(FILE *fd,std::string opt); + + inline void wr(uint8_t * flow_var_base,uint8_t * pkt_base) { + uint8_t * p_pkt = (pkt_base+m_pkt_offset); + uint8_t * p_flow_var = (flow_var_base+m_offset); + *p_pkt=(*p_flow_var+m_val_offset); + + } + + +} __attribute__((packed)) ; + + +struct StreamDPOpPktWr16 : public StreamDPOpPktWrBase { + uint16_t m_pkt_offset; + int16_t m_val_offset; +public: + void dump(FILE *fd,std::string opt); + + inline void wr(uint8_t * flow_var_base,uint8_t * pkt_base) { + uint16_t * p_pkt = (uint16_t*)(pkt_base+m_pkt_offset); + uint16_t * p_flow_var = (uint16_t*)(flow_var_base+m_offset); + + if ( likely(is_big())){ + *p_pkt=PKT_HTONS((*p_flow_var+m_val_offset)); + }else{ + *p_pkt=(*p_flow_var+m_val_offset); + } + + } +} __attribute__((packed)); + +struct StreamDPOpPktWr32 : public StreamDPOpPktWrBase { + uint16_t m_pkt_offset; + int32_t m_val_offset; +public: + void dump(FILE *fd,std::string opt); + + inline void wr(uint8_t * flow_var_base,uint8_t * pkt_base) { + uint32_t * p_pkt = (uint32_t*)(pkt_base+m_pkt_offset); + uint32_t * p_flow_var = (uint32_t*)(flow_var_base+m_offset); + if ( likely(is_big())){ + *p_pkt=PKT_HTONL((*p_flow_var+m_val_offset)); + }else{ + *p_pkt=(*p_flow_var+m_val_offset); + } + } + +} __attribute__((packed)); + +struct StreamDPOpPktWr64 : public StreamDPOpPktWrBase { + uint16_t m_pkt_offset; + int64_t m_val_offset; + +public: + void dump(FILE *fd,std::string opt); + + inline void wr(uint8_t * flow_var_base,uint8_t * pkt_base) { + uint64_t * p_pkt = (uint64_t*)(pkt_base+m_pkt_offset); + uint64_t * p_flow_var = (uint64_t*)(flow_var_base+m_offset); + if ( likely(is_big())){ + *p_pkt=pal_ntohl64((*p_flow_var+m_val_offset)); + }else{ + *p_pkt=(*p_flow_var+m_val_offset); + } + } + + +} __attribute__((packed)); + +struct StreamDPOpIpv4Fix { + uint8_t m_op; + uint16_t m_offset; +public: + void dump(FILE *fd,std::string opt); + void run(uint8_t * pkt_base){ + IPHeader * ipv4= (IPHeader *)(pkt_base+m_offset); + ipv4->updateCheckSum(); + } + +} __attribute__((packed)); + + +/* flow varible of Client command */ +struct StreamDPFlowClient { + uint32_t cur_ip; + uint16_t cur_port; + uint32_t cur_flow_id; +} __attribute__((packed)); + + +struct StreamDPOpClientsLimit { + uint8_t m_op; + uint8_t m_flow_offset; /* offset into the flow var bytes */ + uint8_t m_flags; + uint8_t m_pad; + uint16_t m_min_port; + uint16_t m_max_port; + + uint32_t m_min_ip; + uint32_t m_max_ip; + uint32_t m_limit_flows; /* limit the number of flows */ + +public: + void dump(FILE *fd,std::string opt); + inline void run(uint8_t * flow_var_base) { + StreamDPFlowClient * lp= (StreamDPFlowClient *)(flow_var_base+m_flow_offset); + lp->cur_ip++; + if (lp->cur_ip > m_max_ip ) { + lp->cur_ip= m_min_ip; + lp->cur_port++; + if (lp->cur_port > m_max_port) { + lp->cur_port = m_min_port; + } + } + + if (m_limit_flows) { + lp->cur_flow_id++; + if ( lp->cur_flow_id > m_limit_flows ){ + /* reset to the first flow */ + lp->cur_flow_id = 1; + lp->cur_ip = m_min_ip; + lp->cur_port = m_min_port; + } + } + } + + +} __attribute__((packed)); + +struct StreamDPOpClientsUnLimit { + enum __MIN_PORT { + CLIENT_UNLIMITED_MIN_PORT = 1025 + }; + + uint8_t m_op; + uint8_t m_flow_offset; /* offset into the flow var bytes */ + uint8_t m_flags; + uint8_t m_pad; + uint32_t m_min_ip; + uint32_t m_max_ip; + +public: + void dump(FILE *fd,std::string opt); + inline void run(uint8_t * flow_var_base) { + StreamDPFlowClient * lp= (StreamDPFlowClient *)(flow_var_base+m_flow_offset); + lp->cur_ip++; + if (lp->cur_ip > m_max_ip ) { + lp->cur_ip= m_min_ip; + lp->cur_port++; + if (lp->cur_port == 0) { + lp->cur_port = StreamDPOpClientsUnLimit::CLIENT_UNLIMITED_MIN_PORT; + } + } + } + +} __attribute__((packed)); + + +/* datapath instructions */ +class StreamDPVmInstructions { +public: + enum INS_TYPE { + ditINC8 , + ditINC16 , + ditINC32 , + ditINC64 , + + ditDEC8 , + ditDEC16 , + ditDEC32 , + ditDEC64 , + + ditRANDOM8 , + ditRANDOM16 , + ditRANDOM32 , + ditRANDOM64 , + + ditFIX_IPV4_CS , + + itPKT_WR8 , + itPKT_WR16 , + itPKT_WR32 , + itPKT_WR64 , + itCLIENT_VAR , + itCLIENT_VAR_UNLIMIT + }; + + +public: + void clear(); + void add_command(void *buffer,uint16_t size); + uint8_t * get_program(); + uint32_t get_program_size(); + + + void Dump(FILE *fd); + + +private: + std::vector<uint8_t> m_inst_list; +}; + + +class StreamDPVmInstructionsRunner { +public: + inline void run(uint32_t program_size, + uint8_t * program, /* program */ + uint8_t * flow_var, /* flow var */ + uint8_t * pkt); /* pkt */ + +}; + + +inline void StreamDPVmInstructionsRunner::run(uint32_t program_size, + uint8_t * program, /* program */ + uint8_t * flow_var, /* flow var */ + uint8_t * pkt){ + + + uint8_t * p=program; + uint8_t * p_end=p+program_size; + + + union ua_ { + StreamDPOpFlowVar8 *lpv8; + StreamDPOpFlowVar16 *lpv16; + StreamDPOpFlowVar32 *lpv32; + StreamDPOpFlowVar64 *lpv64; + StreamDPOpIpv4Fix *lpIpv4Fix; + StreamDPOpPktWr8 *lpw8; + StreamDPOpPktWr16 *lpw16; + StreamDPOpPktWr32 *lpw32; + StreamDPOpPktWr64 *lpw64; + StreamDPOpClientsLimit *lpcl; + StreamDPOpClientsUnLimit *lpclu; + } ua ; + + while ( p < p_end) { + uint8_t op_code=*p; + switch (op_code) { + + case StreamDPVmInstructions::itCLIENT_VAR : + ua.lpcl =(StreamDPOpClientsLimit *)p; + ua.lpcl->run(flow_var); + p+=sizeof(StreamDPOpClientsLimit); + break; + + case StreamDPVmInstructions::itCLIENT_VAR_UNLIMIT : + ua.lpclu =(StreamDPOpClientsUnLimit *)p; + ua.lpclu->run(flow_var); + p+=sizeof(StreamDPOpClientsUnLimit); + break; + + case StreamDPVmInstructions::ditINC8 : + ua.lpv8 =(StreamDPOpFlowVar8 *)p; + ua.lpv8->run_inc(flow_var); + p+=sizeof(StreamDPOpFlowVar8); + break; + + case StreamDPVmInstructions::ditINC16 : + ua.lpv16 =(StreamDPOpFlowVar16 *)p; + ua.lpv16->run_inc(flow_var); + p+=sizeof(StreamDPOpFlowVar16); + break; + case StreamDPVmInstructions::ditINC32 : + ua.lpv32 =(StreamDPOpFlowVar32 *)p; + ua.lpv32->run_inc(flow_var); + p+=sizeof(StreamDPOpFlowVar32); + break; + case StreamDPVmInstructions::ditINC64 : + ua.lpv64 =(StreamDPOpFlowVar64 *)p; + ua.lpv64->run_inc(flow_var); + p+=sizeof(StreamDPOpFlowVar64); + break; + + case StreamDPVmInstructions::ditDEC8 : + ua.lpv8 =(StreamDPOpFlowVar8 *)p; + ua.lpv8->run_dec(flow_var); + p+=sizeof(StreamDPOpFlowVar8); + break; + case StreamDPVmInstructions::ditDEC16 : + ua.lpv16 =(StreamDPOpFlowVar16 *)p; + ua.lpv16->run_dec(flow_var); + p+=sizeof(StreamDPOpFlowVar16); + break; + case StreamDPVmInstructions::ditDEC32 : + ua.lpv32 =(StreamDPOpFlowVar32 *)p; + ua.lpv32->run_dec(flow_var); + p+=sizeof(StreamDPOpFlowVar32); + break; + case StreamDPVmInstructions::ditDEC64 : + ua.lpv64 =(StreamDPOpFlowVar64 *)p; + ua.lpv64->run_dec(flow_var); + p+=sizeof(StreamDPOpFlowVar64); + break; + + case StreamDPVmInstructions::ditRANDOM8 : + ua.lpv8 =(StreamDPOpFlowVar8 *)p; + ua.lpv8->run_rand(flow_var); + p+=sizeof(StreamDPOpFlowVar8); + break; + case StreamDPVmInstructions::ditRANDOM16 : + ua.lpv16 =(StreamDPOpFlowVar16 *)p; + ua.lpv16->run_rand(flow_var); + p+=sizeof(StreamDPOpFlowVar16); + break; + case StreamDPVmInstructions::ditRANDOM32 : + ua.lpv32 =(StreamDPOpFlowVar32 *)p; + ua.lpv32->run_rand(flow_var); + p+=sizeof(StreamDPOpFlowVar32); + break; + case StreamDPVmInstructions::ditRANDOM64 : + ua.lpv64 =(StreamDPOpFlowVar64 *)p; + ua.lpv64->run_rand(flow_var); + p+=sizeof(StreamDPOpFlowVar64); + break; + + case StreamDPVmInstructions::ditFIX_IPV4_CS : + ua.lpIpv4Fix =(StreamDPOpIpv4Fix *)p; + ua.lpIpv4Fix->run(pkt); + p+=sizeof(StreamDPOpIpv4Fix); + break; + + case StreamDPVmInstructions::itPKT_WR8 : + ua.lpw8 =(StreamDPOpPktWr8 *)p; + ua.lpw8->wr(flow_var,pkt); + p+=sizeof(StreamDPOpPktWr8); + break; + + case StreamDPVmInstructions::itPKT_WR16 : + ua.lpw16 =(StreamDPOpPktWr16 *)p; + ua.lpw16->wr(flow_var,pkt); + p+=sizeof(StreamDPOpPktWr16); + break; + + case StreamDPVmInstructions::itPKT_WR32 : + ua.lpw32 =(StreamDPOpPktWr32 *)p; + ua.lpw32->wr(flow_var,pkt); + p+=sizeof(StreamDPOpPktWr32); + break; + + case StreamDPVmInstructions::itPKT_WR64 : + ua.lpw64 =(StreamDPOpPktWr64 *)p; + ua.lpw64->wr(flow_var,pkt); + p+=sizeof(StreamDPOpPktWr64); + break; + default: + assert(0); + } + }; +}; + + + /** * interface for stream VM instruction @@ -32,8 +552,24 @@ limitations under the License. class StreamVmInstruction { public: + enum INS_TYPE { + itNONE = 0, + itFIX_IPV4_CS = 4, + itFLOW_MAN = 5, + itPKT_WR = 6, + itFLOW_CLIENT = 7 + + }; + + typedef uint8_t instruction_type_t ; + + virtual instruction_type_t get_instruction_type()=0; + virtual ~StreamVmInstruction(); + virtual void Dump(FILE *fd)=0; + + private: static const std::string m_name; }; @@ -48,8 +584,14 @@ public: } -private: - uint16_t m_pkt_offset; + virtual instruction_type_t get_instruction_type(){ + return ( StreamVmInstruction::itFIX_IPV4_CS); + } + + virtual void Dump(FILE *fd); + +public: + uint16_t m_pkt_offset; /* the offset of IPv4 header from the start of the packet */ }; /** @@ -61,6 +603,10 @@ class StreamVmInstructionFlowMan : public StreamVmInstruction { public: + virtual instruction_type_t get_instruction_type(){ + return ( StreamVmInstruction::itFLOW_MAN); + } + /** * different types of operations on the object */ @@ -85,7 +631,16 @@ public: } + virtual void Dump(FILE *fd); + + void sanity_check(uint32_t ins_id,StreamVm *lp); + private: + void sanity_check_valid_range(uint32_t ins_id,StreamVm *lp); + void sanity_check_valid_size(uint32_t ins_id,StreamVm *lp); + void sanity_check_valid_opt(uint32_t ins_id,StreamVm *lp); + +public: /* flow var name */ @@ -105,6 +660,83 @@ private: }; + +/** + * flow client instruction - save state for client range and port range + * + * @author hhaim + */ +class StreamVmInstructionFlowClient : public StreamVmInstruction { + +public: + enum client_flags_e { + CLIENT_F_UNLIMITED_FLOWS=1, /* unlimited number of flow, don't care about ports */ + }; + + + virtual instruction_type_t get_instruction_type(){ + return ( StreamVmInstruction::itFLOW_CLIENT); + } + + + StreamVmInstructionFlowClient(const std::string &var_name, + uint32_t client_min_value, + uint32_t client_max_value, + uint16_t port_min, + uint16_t port_max, + uint32_t limit_num_flows, /* zero means don't limit */ + uint16_t flags + ) { + m_var_name = var_name; + m_client_min = client_min_value; + m_client_max = client_max_value; + + m_port_min = port_min; + m_port_max = port_max; + + m_limit_num_flows = limit_num_flows; + m_flags = flags; + } + + virtual void Dump(FILE *fd); + + static uint8_t get_flow_var_size(){ + return (4+2+4); + } + + bool is_unlimited_flows(){ + return ( (m_flags & StreamVmInstructionFlowClient::CLIENT_F_UNLIMITED_FLOWS ) == + StreamVmInstructionFlowClient::CLIENT_F_UNLIMITED_FLOWS ); + } +public: + + + /* flow var name */ + std::string m_var_name; + + uint32_t m_client_min; // min ip + uint32_t m_client_max; // max ip + uint16_t m_port_min; // start port + uint16_t m_port_max; // start port + uint32_t m_limit_num_flows; // number of flows + uint16_t m_flags; +}; + + + +class VmFlowVarRec { +public: + uint32_t m_offset; + union { + StreamVmInstructionFlowMan * m_ins_flowv; + StreamVmInstructionFlowClient * m_ins_flow_client; + } m_ins; + uint8_t m_size_bytes; +}; + + + + /** * write flow var to packet * @@ -121,7 +753,14 @@ public: m_pkt_offset(pkt_offset), m_add_value(add_value), m_is_big_endian(is_big_endian) {} -private: + + virtual instruction_type_t get_instruction_type(){ + return ( StreamVmInstruction::itPKT_WR); + } + + virtual void Dump(FILE *fd); + +public: /* flow var name to write */ std::string m_flow_var_name; @@ -136,12 +775,161 @@ private: bool m_is_big_endian; }; + +/** + * describes a VM program for DP + * + */ + +class StreamVmDp { +public: + StreamVmDp(){ + m_bss_ptr=NULL; + m_program_ptr =NULL ; + m_bss_size=0; + m_program_size=0; + m_max_pkt_offset_change=0; + } + + StreamVmDp( uint8_t * bss, + uint16_t bss_size, + uint8_t * prog, + uint16_t prog_size, + uint16_t max_pkt_offset + ){ + + if (bss) { + assert(bss_size); + m_bss_ptr=(uint8_t*)malloc(bss_size); + assert(m_bss_ptr); + memcpy(m_bss_ptr,bss,bss_size); + m_bss_size=bss_size; + }else{ + m_bss_ptr=NULL; + m_bss_size=0; + } + + if (prog) { + assert(prog_size); + m_program_ptr=(uint8_t*)malloc(prog_size); + memcpy(m_program_ptr,prog,prog_size); + m_program_size = prog_size; + }else{ + m_program_ptr = NULL; + m_program_size=0; + } + m_max_pkt_offset_change =max_pkt_offset; + } + + ~StreamVmDp(){ + if (m_bss_ptr) { + free(m_bss_ptr); + m_bss_ptr=0; + m_bss_size=0; + } + if (m_program_ptr) { + free(m_program_ptr); + m_program_size=0; + m_program_ptr=0; + } + } + + StreamVmDp * clone() const { + StreamVmDp * lp= new StreamVmDp(m_bss_ptr, + m_bss_size, + m_program_ptr, + m_program_size, + m_max_pkt_offset_change + ); + assert(lp); + return (lp); + } + + uint8_t* clone_bss(){ + assert(m_bss_size>0); + uint8_t *p=(uint8_t *)malloc(m_bss_size); + assert(p); + memcpy(p,m_bss_ptr,m_bss_size); + return (p); + } + + uint16_t get_bss_size(){ + return(m_bss_size); + } + + uint8_t* get_bss(){ + return (m_bss_ptr); + } + + uint8_t* get_program(){ + return (m_program_ptr); + } + + uint16_t get_program_size(){ + return (m_program_size); + } + + uint16_t get_max_packet_update_offset(){ + return (m_max_pkt_offset_change); + } + +private: + uint8_t * m_bss_ptr; /* pointer to the data section */ + uint8_t * m_program_ptr; /* pointer to the program */ + uint16_t m_bss_size; + uint16_t m_program_size; /* program size*/ + uint16_t m_max_pkt_offset_change; + +}; + + /** * describes a VM program * */ class StreamVm { + public: + enum STREAM_VM { + svMAX_FLOW_VAR = 64, /* maximum flow varible */ + svMAX_PACKET_OFFSET_CHANGE = 512 + }; + + + + StreamVm(){ + m_bss=0; + m_pkt_size=0; + m_cur_var_offset=0; + } + + + /* set packet size */ + void set_packet_size(uint16_t pkt_size){ + m_pkt_size = pkt_size; + } + + uint16_t get_packet_size(){ + return ( m_pkt_size); + } + + + StreamVmDp * cloneAsVmDp(){ + + StreamVmDp * lp= new StreamVmDp(get_bss_ptr(), + get_bss_size(), + get_dp_instruction_buffer()->get_program(), + get_dp_instruction_buffer()->get_program_size(), + get_max_packet_update_offset() + ); + assert(lp); + return (lp); + + } + + bool is_vm_empty() { + return (m_inst_list.size() == 0); + } /** * add new instruction to the VM @@ -155,17 +943,75 @@ public: */ const std::vector<StreamVmInstruction *> & get_instruction_list(); + StreamDPVmInstructions * get_dp_instruction_buffer(); + + uint16_t get_bss_size(){ + return (m_cur_var_offset ); + } + + uint8_t * get_bss_ptr(){ + return (m_bss ); + } + + + uint16_t get_max_packet_update_offset(){ + return ( m_max_field_update ); + } + + + /** * compile the VM * return true of success, o.w false * */ - bool compile(); + void compile(); ~StreamVm(); + void Dump(FILE *fd); + + /* raise exception */ + void err(const std::string &err); + private: + + /* lookup for varible offset, */ + bool var_lookup(const std::string &var_name,VmFlowVarRec & var); + + void var_clear_table(); + + bool var_add(const std::string &var_name,VmFlowVarRec & var); + + uint16_t get_var_offset(const std::string &var_name); + + void build_flow_var_table() ; + + void build_bss(); + + void build_program(); + + void alloc_bss(); + + void free_bss(); + +private: + + void clean_max_field_cnt(); + + void add_field_cnt(uint16_t new_cnt); + +private: + uint16_t m_pkt_size; + uint16_t m_cur_var_offset; + uint16_t m_max_field_update; /* the location of the last byte that is going to be changed in the packet */ + std::vector<StreamVmInstruction *> m_inst_list; + std::unordered_map<std::string, VmFlowVarRec> m_flow_var_offset; + uint8_t * m_bss; + + StreamDPVmInstructions m_instructions; + }; #endif /* __TREX_STREAM_VM_API_H__ */ diff --git a/src/stateless/cp/trex_streams_compiler.cpp b/src/stateless/cp/trex_streams_compiler.cpp index b28989be..c4900e66 100644 --- a/src/stateless/cp/trex_streams_compiler.cpp +++ b/src/stateless/cp/trex_streams_compiler.cpp @@ -142,8 +142,9 @@ private: /************************************** * stream compiled object *************************************/ -TrexStreamsCompiledObj::TrexStreamsCompiledObj(uint8_t port_id, double mul) : m_port_id(port_id), m_mul(mul) { - m_all_continues=false; +TrexStreamsCompiledObj::TrexStreamsCompiledObj(uint8_t port_id) { + m_port_id = port_id; + m_all_continues = false; } TrexStreamsCompiledObj::~TrexStreamsCompiledObj() { @@ -155,53 +156,40 @@ TrexStreamsCompiledObj::~TrexStreamsCompiledObj() { void -TrexStreamsCompiledObj::add_compiled_stream(TrexStream * stream){ +TrexStreamsCompiledObj::add_compiled_stream(TrexStream *stream){ obj_st obj; - obj.m_stream = stream->clone_as_dp(); + obj.m_stream = stream; m_objs.push_back(obj); } -void -TrexStreamsCompiledObj::add_compiled_stream(TrexStream * stream, - uint32_t my_dp_id, int next_dp_id) { - obj_st obj; - - obj.m_stream = stream->clone_as_dp(); - /* compress the id's*/ - obj.m_stream->fix_dp_stream_id(my_dp_id,next_dp_id); - - m_objs.push_back(obj); -} - -void TrexStreamsCompiledObj::Dump(FILE *fd){ - for (auto obj : m_objs) { - obj.m_stream->Dump(fd); - } -} - - TrexStreamsCompiledObj * TrexStreamsCompiledObj::clone() { - /* use multiplier of 1 to avoid double mult */ - TrexStreamsCompiledObj *new_compiled_obj = new TrexStreamsCompiledObj(m_port_id, 1); + TrexStreamsCompiledObj *new_compiled_obj = new TrexStreamsCompiledObj(m_port_id); /** * clone each element */ for (auto obj : m_objs) { - new_compiled_obj->add_compiled_stream(obj.m_stream); + TrexStream *new_stream = obj.m_stream->clone_as_dp(); + new_compiled_obj->add_compiled_stream(new_stream); } - new_compiled_obj->m_mul = m_mul; - return new_compiled_obj; + } +void TrexStreamsCompiledObj::Dump(FILE *fd){ + for (auto obj : m_objs) { + obj.m_stream->Dump(fd); + } +} + + void TrexStreamsCompiler::add_warning(const std::string &warning) { m_warnings.push_back("*** warning: " + warning); @@ -219,7 +207,7 @@ TrexStreamsCompiler::check_stream(const TrexStream *stream) { /* cont. stream can point only on itself */ if (stream->get_type() == TrexStream::stCONTINUOUS) { if (stream->m_next_stream_id != -1) { - ss << "continous stream '" << stream->m_stream_id << "' cannot point on another stream"; + ss << "continous stream '" << stream->m_stream_id << "' cannot point to another stream"; err(ss.str()); } } @@ -381,12 +369,34 @@ TrexStreamsCompiler::pre_compile_check(const std::vector<TrexStream *> &streams, * stream compiler *************************************/ bool -TrexStreamsCompiler::compile(const std::vector<TrexStream *> &streams, - TrexStreamsCompiledObj &obj, - std::string *fail_msg) { +TrexStreamsCompiler::compile(uint8_t port_id, + const std::vector<TrexStream *> &streams, + std::vector<TrexStreamsCompiledObj *> &objs, + uint8_t dp_core_count, + double factor, + std::string *fail_msg) { + + try { + return compile_internal(port_id,streams,objs,dp_core_count,factor,fail_msg); + } catch (const TrexException &ex) { + if (fail_msg) { + *fail_msg = ex.what(); + } else { + std::cout << ex.what(); + } + return false; + } + +} +bool +TrexStreamsCompiler::compile_internal(uint8_t port_id, + const std::vector<TrexStream *> &streams, + std::vector<TrexStreamsCompiledObj *> &objs, + uint8_t dp_core_count, + double factor, + std::string *fail_msg) { #if 0 - fprintf(stdout,"------------pre compile \n"); for (auto stream : streams) { stream->Dump(stdout); } @@ -397,49 +407,100 @@ TrexStreamsCompiler::compile(const std::vector<TrexStream *> &streams, /* compile checks */ - try { - pre_compile_check(streams,nodes); - } catch (const TrexException &ex) { - if (fail_msg) { - *fail_msg = ex.what(); - } else { - std::cout << ex.what(); + pre_compile_check(streams, nodes); + + /* check if all are cont. streams */ + bool all_continues = true; + for (const auto stream : streams) { + if (stream->get_type() != TrexStream::stCONTINUOUS) { + all_continues = false; + break; } - return false; } + /* allocate objects for all DP cores */ + for (uint8_t i = 0; i < dp_core_count; i++) { + TrexStreamsCompiledObj *obj = new TrexStreamsCompiledObj(port_id); + obj->m_all_continues = all_continues; + objs.push_back(obj); + } - bool all_continues=true; - /* for now we do something trivial, */ + /* compile all the streams */ for (auto stream : streams) { /* skip non-enabled streams */ if (!stream->m_enabled) { continue; } - if (stream->get_type() != TrexStream::stCONTINUOUS ) { - all_continues=false; - } + + /* compile a single stream to all cores */ + compile_stream(stream, factor, dp_core_count, objs, nodes); + } - int new_id= nodes.get(stream->m_stream_id)->m_compressed_stream_id; - assert(new_id>=0); - uint32_t my_stream_id = (uint32_t)new_id; - int my_next_stream_id=-1; - if (stream->m_next_stream_id>=0) { - my_next_stream_id=nodes.get(stream->m_next_stream_id)->m_compressed_stream_id; - } + return true; +} - /* add it */ - obj.add_compiled_stream(stream, - my_stream_id, - my_next_stream_id - ); - +/** + * compiles a single stream to DP objects + * + * @author imarom (03-Dec-15) + * + */ +void +TrexStreamsCompiler::compile_stream(const TrexStream *stream, + double factor, + uint8_t dp_core_count, + std::vector<TrexStreamsCompiledObj *> &objs, + GraphNodeMap &nodes) { + + + /* fix the stream ids */ + int new_id = nodes.get(stream->m_stream_id)->m_compressed_stream_id; + assert(new_id >= 0); + + int new_next_id = -1; + if (stream->m_next_stream_id >= 0) { + new_next_id = nodes.get(stream->m_next_stream_id)->m_compressed_stream_id; } - obj.m_all_continues =all_continues; - return true; + + /* calculate rate */ + double per_core_rate = (stream->m_pps * (factor / dp_core_count)); + int per_core_burst_total_pkts = (stream->m_burst_total_pkts / dp_core_count); + + /* compile VM */ + /* fix this const away problem */ + ((TrexStream *)stream)->compile(); + + std::vector<TrexStream *> per_core_streams(dp_core_count); + + /* for each core - creates its own version of the stream */ + for (uint8_t i = 0; i < dp_core_count; i++) { + TrexStream *dp_stream = stream->clone_as_dp(); + + /* fix stream ID */ + dp_stream->fix_dp_stream_id(new_id, new_next_id); + + + /* adjust rate and packets count */ + dp_stream->m_pps = per_core_rate; + dp_stream->m_burst_total_pkts = per_core_burst_total_pkts; + + per_core_streams[i] = dp_stream; + } + + /* take care of remainder from a burst */ + int burst_remainder = stream->m_burst_total_pkts - (per_core_burst_total_pkts * dp_core_count); + per_core_streams[0]->m_burst_total_pkts += burst_remainder; + + /* attach the compiled stream of every core to its object */ + for (uint8_t i = 0; i < dp_core_count; i++) { + objs[i]->add_compiled_stream(per_core_streams[i]); + } + + } + /************************************** * streams graph *************************************/ @@ -486,10 +547,13 @@ TrexStreamsGraph::add_rate_events_for_stream_cont(double &offset_usec, const Tre start_event.time = offset_usec + stream->m_isg_usec; start_event.diff_pps = stream->get_pps(); start_event.diff_bps = stream->get_bps(); - m_graph_obj.add_rate_event(start_event); + m_graph_obj->add_rate_event(start_event); /* no more events after this stream */ offset_usec = -1; + + /* also mark we have an inifite time */ + m_graph_obj->m_expected_duration = -1; } /** @@ -510,13 +574,13 @@ TrexStreamsGraph::add_rate_events_for_stream_single_burst(double &offset_usec, c start_event.time = offset_usec + stream->m_isg_usec; start_event.diff_pps = stream->get_pps(); start_event.diff_bps = stream->get_bps(); - m_graph_obj.add_rate_event(start_event); + m_graph_obj->add_rate_event(start_event); /* stop event */ stop_event.time = start_event.time + stream->get_burst_length_usec(); stop_event.diff_pps = -(start_event.diff_pps); stop_event.diff_bps = -(start_event.diff_bps); - m_graph_obj.add_rate_event(stop_event); + m_graph_obj->add_rate_event(stop_event); /* next stream starts from here */ offset_usec = stop_event.time; @@ -549,10 +613,10 @@ TrexStreamsGraph::add_rate_events_for_stream_multi_burst(double &offset_usec, co for (int i = 0; i < stream->m_num_bursts; i++) { start_event.time = offset_usec + delay; - m_graph_obj.add_rate_event(start_event); + m_graph_obj->add_rate_event(start_event); stop_event.time = start_event.time + stream->get_burst_length_usec(); - m_graph_obj.add_rate_event(stop_event); + m_graph_obj->add_rate_event(stop_event); /* after the first burst, the delay is inter burst gap */ delay = stream->m_ibg_usec; @@ -602,6 +666,7 @@ TrexStreamsGraph::generate_graph_for_one_root(uint32_t root_stream_id) { /* loop detection */ auto search = loop_hash.find(stream->m_next_stream_id); if (search != loop_hash.end()) { + m_graph_obj->on_loop_detection(); break; } @@ -615,8 +680,12 @@ TrexStreamsGraph::generate_graph_for_one_root(uint32_t root_stream_id) { * see graph object for more details * */ -const TrexStreamsGraphObj & +const TrexStreamsGraphObj * TrexStreamsGraph::generate(const std::vector<TrexStream *> &streams) { + + /* main object to hold the graph - returned to the user */ + m_graph_obj = new TrexStreamsGraphObj(); + std::vector <uint32_t> root_streams; /* before anything we create a hash streams ID @@ -644,7 +713,7 @@ TrexStreamsGraph::generate(const std::vector<TrexStream *> &streams) { } - m_graph_obj.generate(); + m_graph_obj->generate(); return m_graph_obj; } @@ -670,6 +739,11 @@ TrexStreamsGraphObj::find_max_rate() { max_rate_bps = std::max(max_rate_bps, current_rate_bps); } + /* if not mark as inifite - get the last event time */ + if (m_expected_duration != -1) { + m_expected_duration = m_rate_events.back().time; + } + m_max_pps = max_rate_pps; m_max_bps = max_rate_bps; } diff --git a/src/stateless/cp/trex_streams_compiler.h b/src/stateless/cp/trex_streams_compiler.h index 70a31c5e..d2b0cd1d 100644 --- a/src/stateless/cp/trex_streams_compiler.h +++ b/src/stateless/cp/trex_streams_compiler.h @@ -38,9 +38,10 @@ class GraphNodeMap; */ class TrexStreamsCompiledObj { friend class TrexStreamsCompiler; + public: - TrexStreamsCompiledObj(uint8_t port_id, double m_mul); + TrexStreamsCompiledObj(uint8_t port_id); ~TrexStreamsCompiledObj(); struct obj_st { @@ -56,32 +57,22 @@ public: return (m_port_id); } - /** - * clone the compiled object - * - */ - TrexStreamsCompiledObj * clone(); - - double get_multiplier(){ - return (m_mul); - } - bool get_all_streams_continues(){ return (m_all_continues); } void Dump(FILE *fd); + TrexStreamsCompiledObj* clone(); + private: - void add_compiled_stream(TrexStream * stream, - uint32_t my_dp_id, int next_dp_id); - void add_compiled_stream(TrexStream * stream); + void add_compiled_stream(TrexStream *stream); + std::vector<obj_st> m_objs; bool m_all_continues; uint8_t m_port_id; - double m_mul; }; class TrexStreamsCompiler { @@ -93,7 +84,13 @@ public: * @author imarom (28-Oct-15) * */ - bool compile(const std::vector<TrexStream *> &streams, TrexStreamsCompiledObj &obj, std::string *fail_msg = NULL); + bool compile(uint8_t port_id, + const std::vector<TrexStream *> &streams, + std::vector<TrexStreamsCompiledObj *> &objs, + uint8_t dp_core_count = 1, + double factor = 1.0, + std::string *fail_msg = NULL); + /** * @@ -106,6 +103,13 @@ public: private: + bool compile_internal(uint8_t port_id, + const std::vector<TrexStream *> &streams, + std::vector<TrexStreamsCompiledObj *> &objs, + uint8_t dp_core_count, + double factor, + std::string *fail_msg); + void pre_compile_check(const std::vector<TrexStream *> &streams, GraphNodeMap & nodes); void allocate_pass(const std::vector<TrexStream *> &streams, GraphNodeMap *nodes); @@ -115,8 +119,15 @@ private: void add_warning(const std::string &warning); void err(const std::string &err); + void compile_stream(const TrexStream *stream, + double factor, + uint8_t dp_core_count, + std::vector<TrexStreamsCompiledObj *> &objs, + GraphNodeMap &nodes); + + void compile_stream_vm(TrexStream *stream); + std::vector<std::string> m_warnings; - }; class TrexStreamsGraph; @@ -131,6 +142,12 @@ class TrexStreamsGraphObj { public: + TrexStreamsGraphObj() { + m_max_pps = 0; + m_max_bps = 0; + m_expected_duration = 0; + } + /** * rate event is defined by those: * time - the time of the event on the timeline @@ -153,12 +170,21 @@ public: return m_max_bps; } + int get_duration() const { + return m_expected_duration; + } + const std::list<rate_event_st> & get_events() const { return m_rate_events; } + private: + void on_loop_detection() { + m_expected_duration = -1; + } + void add_rate_event(const rate_event_st &ev) { m_rate_events.push_back(ev); } @@ -166,11 +192,13 @@ private: void generate(); void find_max_rate(); - double m_max_pps; - double m_max_bps; + double m_max_pps; + double m_max_bps; + int m_expected_duration; /* list of rate events */ std::list<rate_event_st> m_rate_events; + }; /** @@ -181,11 +209,15 @@ private: class TrexStreamsGraph { public: + TrexStreamsGraph() { + m_graph_obj = NULL; + } + /** * generate a sequence graph for streams * */ - const TrexStreamsGraphObj & generate(const std::vector<TrexStream *> &streams); + const TrexStreamsGraphObj * generate(const std::vector<TrexStream *> &streams); private: @@ -200,7 +232,7 @@ private: std::unordered_map<uint32_t, const TrexStream *> m_streams_hash; /* main object to hold the graph - returned to the user */ - TrexStreamsGraphObj m_graph_obj; + TrexStreamsGraphObj *m_graph_obj; }; #endif /* __TREX_STREAMS_COMPILER_H__ */ diff --git a/src/stateless/dp/trex_stateless_dp_core.cpp b/src/stateless/dp/trex_stateless_dp_core.cpp index dd4937cd..585ff2c7 100644 --- a/src/stateless/dp/trex_stateless_dp_core.cpp +++ b/src/stateless/dp/trex_stateless_dp_core.cpp @@ -106,12 +106,56 @@ std::string CGenNodeStateless::get_stream_state_str(stream_state_t stream_state) } +rte_mbuf_t * CGenNodeStateless::alloc_node_with_vm(){ + + rte_mbuf_t * m; + /* alloc small packet buffer*/ + uint16_t prefix_size = prefix_header_size(); + m = CGlobalInfo::pktmbuf_alloc( get_socket_id(), prefix_size ); + if (m==0) { + return (m); + } + /* TBD remove this, should handle cases of error */ + assert(m); + char *p=rte_pktmbuf_append(m, prefix_size); + memcpy( p ,m_original_packet_data_prefix, prefix_size); + + + /* run the VM program */ + StreamDPVmInstructionsRunner runner; + + runner.run( m_vm_program_size, + m_vm_program, + m_vm_flow_var, + (uint8_t*)p); + + + rte_mbuf_t * m_const = get_const_mbuf(); + if ( m_const != NULL) { + utl_rte_pktmbuf_add_after(m,m_const); + } + return (m); +} + + void CGenNodeStateless::free_stl_node(){ /* if we have cache mbuf free it */ rte_mbuf_t * m=get_cache_mbuf(); if (m) { rte_pktmbuf_free(m); m_cache_mbuf=0; + }else{ + /* non cache - must have an header */ + m=get_const_mbuf(); + if (m) { + rte_pktmbuf_free(m); /* reduce the ref counter */ + } + free_prefix_header(); + } + if (m_vm_flow_var) { + /* free flow var */ + free(m_vm_flow_var); + m_vm_flow_var=0; } } @@ -139,7 +183,7 @@ bool TrexStatelessDpPerPort::resume_traffic(uint8_t port_id){ return (true); } -bool TrexStatelessDpPerPort::update_traffic(uint8_t port_id, double mul) { +bool TrexStatelessDpPerPort::update_traffic(uint8_t port_id, double factor) { assert( (m_state == TrexStatelessDpPerPort::ppSTATE_TRANSMITTING || (m_state == TrexStatelessDpPerPort::ppSTATE_PAUSE)) ); @@ -148,7 +192,7 @@ bool TrexStatelessDpPerPort::update_traffic(uint8_t port_id, double mul) { CGenNodeStateless * node = dp_stream.m_node; assert(node->get_port_id() == port_id); - node->set_multiplier(mul); + node->update_rate(factor); } return (true); @@ -423,6 +467,7 @@ TrexStatelessDpCore::add_stream(TrexStatelessDpPerPort * lp_port, CGenNodeStateless *node = m_core->create_node_sl(); /* add periodic */ + node->m_cache_mbuf=0; node->m_type = CGenNode::STATELESS_PKT; node->m_ref_stream_info = stream->clone_as_dp(); @@ -442,6 +487,10 @@ TrexStatelessDpCore::add_stream(TrexStatelessDpPerPort * lp_port, pkt_dir_t dir = m_core->m_node_gen.m_v_if->port_id_to_dir(stream->m_port_id); node->m_flags = 0; + node->m_src_port =0; + node->m_original_packet_data_prefix = 0; + + /* set socket id */ node->set_socket_id(m_core->m_node_gen.m_socket_id); @@ -453,8 +502,7 @@ TrexStatelessDpCore::add_stream(TrexStatelessDpPerPort * lp_port, node->m_pause =0; node->m_stream_type = stream->m_type; - node->m_base_pps = stream->get_pps(); - node->set_multiplier(comp->get_multiplier()); + node->m_next_time_offset = 1.0 / stream->get_pps(); /* stateless specific fields */ switch ( stream->m_type ) { @@ -487,23 +535,77 @@ TrexStatelessDpCore::add_stream(TrexStatelessDpPerPort * lp_port, node->m_port_id = stream->m_port_id; - /* allocate const mbuf */ - rte_mbuf_t *m = CGlobalInfo::pktmbuf_alloc(node->get_socket_id(), pkt_size); - assert(m); - - char *p = rte_pktmbuf_append(m, pkt_size); - assert(p); - /* copy the packet */ - memcpy(p,stream_pkt,pkt_size); - /* set dir 0 or 1 client or server */ node->set_mbuf_cache_dir(dir); - /* TBD repace the mac if req we should add flag */ - m_core->m_node_gen.m_v_if->update_mac_addr_from_global_cfg(dir, m); - /* set the packet as a readonly */ - node->set_cache_mbuf(m); + if (stream->is_vm() == false ) { + /* no VM */ + + node->m_vm_flow_var = NULL; + node->m_vm_program = NULL; + node->m_vm_program_size =0; + + /* allocate const mbuf */ + rte_mbuf_t *m = CGlobalInfo::pktmbuf_alloc(node->get_socket_id(), pkt_size); + assert(m); + + char *p = rte_pktmbuf_append(m, pkt_size); + assert(p); + /* copy the packet */ + memcpy(p,stream_pkt,pkt_size); + + /* TBD repace the mac if req we should add flag */ + m_core->m_node_gen.m_v_if->update_mac_addr_from_global_cfg(dir,(uint8_t*) p); + + /* set the packet as a readonly */ + node->set_cache_mbuf(m); + + node->m_original_packet_data_prefix =0; + }else{ + + /* set the program */ + TrexStream * local_mem_stream = node->m_ref_stream_info; + + StreamVmDp * lpDpVm = local_mem_stream->getDpVm(); + + node->m_vm_flow_var = lpDpVm->clone_bss(); /* clone the flow var */ + node->m_vm_program = lpDpVm->get_program(); /* same ref to the program */ + node->m_vm_program_size =lpDpVm->get_program_size(); + + + /* we need to copy the object */ + if ( pkt_size > stream->m_vm_prefix_size ) { + /* we need const packet */ + uint16_t const_pkt_size = pkt_size - stream->m_vm_prefix_size ; + rte_mbuf_t *m = CGlobalInfo::pktmbuf_alloc(node->get_socket_id(), const_pkt_size ); + assert(m); + + char *p = rte_pktmbuf_append(m, const_pkt_size); + assert(p); + + /* copy packet data */ + memcpy(p,(stream_pkt+ stream->m_vm_prefix_size),const_pkt_size); + + node->set_const_mbuf(m); + } + + + if (stream->m_vm_prefix_size > pkt_size ) { + stream->m_vm_prefix_size = pkt_size; + } + /* copy the headr */ + uint16_t header_size = stream->m_vm_prefix_size; + assert(header_size); + node->alloc_prefix_header(header_size); + uint8_t *p=node->m_original_packet_data_prefix; + assert(p); + + memcpy(p,stream_pkt , header_size); + /* TBD repace the mac if req we should add flag */ + m_core->m_node_gen.m_v_if->update_mac_addr_from_global_cfg(dir, p); + } + CDpOneStream one_stream; @@ -597,11 +699,11 @@ TrexStatelessDpCore::pause_traffic(uint8_t port_id){ } void -TrexStatelessDpCore::update_traffic(uint8_t port_id, double mul) { +TrexStatelessDpCore::update_traffic(uint8_t port_id, double factor) { TrexStatelessDpPerPort * lp_port = get_port_db(port_id); - lp_port->update_traffic(port_id, mul); + lp_port->update_traffic(port_id, factor); } diff --git a/src/stateless/dp/trex_stateless_dp_core.h b/src/stateless/dp/trex_stateless_dp_core.h index 563159b2..7dc4a2b2 100644 --- a/src/stateless/dp/trex_stateless_dp_core.h +++ b/src/stateless/dp/trex_stateless_dp_core.h @@ -68,7 +68,7 @@ public: bool resume_traffic(uint8_t port_id); - bool update_traffic(uint8_t port_id, double mul); + bool update_traffic(uint8_t port_id, double factor); bool stop_traffic(uint8_t port_id, bool stop_on_id, diff --git a/src/stateless/dp/trex_stream_node.h b/src/stateless/dp/trex_stream_node.h index 5997376f..d33785fe 100644 --- a/src/stateless/dp/trex_stream_node.h +++ b/src/stateless/dp/trex_stream_node.h @@ -54,6 +54,16 @@ struct CGenNodeStateless : public CGenNodeBase { friend class TrexStatelessDpCore; public: + + /* flags MASKS*/ + enum { + SL_NODE_FLAGS_DIR =1, //USED by master + SL_NODE_FLAGS_MBUF_CACHE =2, //USED by master + + SL_NODE_CONST_MBUF =4 + + }; + enum { ss_FREE_RESUSE =1, /* should be free by scheduler */ ss_INACTIVE =2, /* will be active by other stream or stopped */ @@ -83,14 +93,20 @@ private: uint32_t m_multi_bursts; /* in case of multi_burst how many bursts */ /* cache line 1 */ - TrexStream * m_ref_stream_info; /* the stream info */ + TrexStream * m_ref_stream_info; /* the stream info */ CGenNodeStateless * m_next_stream; - double m_base_pps; - /* pad to match the size of CGenNode */ - uint8_t m_pad_end[48]; + uint8_t * m_original_packet_data_prefix; /* pointer to the original first pointer 64/128/512 */ + + /* Fast Field VM section */ + uint8_t * m_vm_flow_var; /* pointer to the vm flow var */ + uint8_t * m_vm_program; /* pointer to the program */ + uint16_t m_vm_program_size; /* up to 64K op codes */ + /* End Fast Field VM Section */ + /* pad to match the size of CGenNode */ + uint8_t m_pad_end[30]; public: @@ -105,8 +121,9 @@ public: * on the PPS and multiplier * */ - void set_multiplier(double mul) { - m_next_time_offset = 1.0 / (m_base_pps * mul) ; + void update_rate(double factor) { + /* update the inter packet gap */ + m_next_time_offset = m_next_time_offset / factor; } /* we restart the stream, schedule it using stream isg */ @@ -256,13 +273,51 @@ public: } inline rte_mbuf_t * get_cache_mbuf(){ - if ( m_flags &NODE_FLAGS_MBUF_CACHE ) { + if ( m_flags & NODE_FLAGS_MBUF_CACHE ) { + return ((rte_mbuf_t *)m_cache_mbuf); + }else{ + return ((rte_mbuf_t *)0); + } + } + + inline void set_const_mbuf(rte_mbuf_t * m){ + m_cache_mbuf=(void *)m; + m_flags |= SL_NODE_CONST_MBUF; + } + + inline rte_mbuf_t * get_const_mbuf(){ + if ( m_flags &SL_NODE_CONST_MBUF ) { return ((rte_mbuf_t *)m_cache_mbuf); }else{ return ((rte_mbuf_t *)0); } } + /* prefix header exits only in non cache mode size is 64/128/512 other are not possible right now */ + inline void alloc_prefix_header(uint16_t size){ + set_prefix_header_size(size); + m_original_packet_data_prefix = (uint8_t *)malloc(size); + assert(m_original_packet_data_prefix); + } + + inline void free_prefix_header(){ + if (m_original_packet_data_prefix) { + free(m_original_packet_data_prefix); + } + } + + /* prefix headr could be 64/128/512 */ + inline void set_prefix_header_size(uint16_t size){ + m_src_port=size; + } + + inline uint16_t prefix_header_size(){ + return (m_src_port); + } + + + rte_mbuf_t * alloc_node_with_vm(); + void free_stl_node(); public: diff --git a/src/stateless/messaging/trex_stateless_messaging.cpp b/src/stateless/messaging/trex_stateless_messaging.cpp index 3210f29a..257de168 100644 --- a/src/stateless/messaging/trex_stateless_messaging.cpp +++ b/src/stateless/messaging/trex_stateless_messaging.cpp @@ -168,14 +168,14 @@ TrexStatelessDpCanQuit::clone(){ ************************/ bool TrexStatelessDpUpdate::handle(TrexStatelessDpCore *dp_core) { - dp_core->update_traffic(m_port_id, m_mul); + dp_core->update_traffic(m_port_id, m_factor); return true; } TrexStatelessCpToDpMsgBase * TrexStatelessDpUpdate::clone() { - TrexStatelessCpToDpMsgBase *new_msg = new TrexStatelessDpUpdate(m_port_id, m_mul); + TrexStatelessCpToDpMsgBase *new_msg = new TrexStatelessDpUpdate(m_port_id, m_factor); return new_msg; } diff --git a/src/stateless/messaging/trex_stateless_messaging.h b/src/stateless/messaging/trex_stateless_messaging.h index 7390be60..d56596bf 100644 --- a/src/stateless/messaging/trex_stateless_messaging.h +++ b/src/stateless/messaging/trex_stateless_messaging.h @@ -231,9 +231,9 @@ public: class TrexStatelessDpUpdate : public TrexStatelessCpToDpMsgBase { public: - TrexStatelessDpUpdate(uint8_t port_id, double mul) { + TrexStatelessDpUpdate(uint8_t port_id, double factor) { m_port_id = port_id; - m_mul = mul; + m_factor = factor; } virtual bool handle(TrexStatelessDpCore *dp_core); @@ -242,7 +242,7 @@ public: private: uint8_t m_port_id; - double m_mul; + double m_factor; }; |