OpenSDN source code
test_xml_packet.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 #include <base/os.h>
6 #include <iostream>
7 #include <fstream>
8 #include <pugixml/pugixml.hpp>
9 #include <boost/uuid/uuid.hpp>
10 #include <boost/uuid/string_generator.hpp>
11 
12 #include <test/test_cmn_util.h>
13 #include <pkt/test/test_pkt_util.h>
14 #include <oper/physical_device.h>
16 
17 #include <test-xml/test_xml.h>
18 #include <test-xml/test_xml_oper.h>
21 
22 using namespace std;
23 using namespace pugi;
24 using namespace boost::uuids;
25 using namespace AgentUtXmlUtils;
26 int hash_id = 1;
27 
29  name_ = "pkt";
30  len_ = 64;
31  vrf_id_ = -1;
32  vrf_str_ = "";
33  ingress_ = false;
34  fwd_mode_ = "l3";
35  pkt_module_ = "flow";
36 
37  intf_id_ = 0;
38  intf_ = "";
39 
40  tunnel_type_ = "";
41  tunnel_sip_ = "0.0.0.0";
42  tunnel_dip_ = "0.0.0.0";
43  label_ = -1;
44  vxlan_id_ = 0;
45 
46  smac_ = "00:00:00:00:00:01";
47  dmac_ = "00:00:00:00:00:02";
48  sip_ = "1.1.1.1";
49  dip_ = "1.1.1.2";
50  proto_id_ = 1;
51  proto_ = "icmp";
52  sport_ = 1;
53  dport_ = 2;
54  tcp_ack_ = false;
55 
56  trap_code_ = "flow";
57  hash_id_ = 0;
58 }
59 
61 }
62 
64  const {
65  if (tunnel_type_ == "gre" || tunnel_type_ == "GRE")
66  return TunnelType::MPLS_GRE;
67  if (tunnel_type_ == "udp" || tunnel_type_ == "UDP")
68  return TunnelType::MPLS_UDP;
69  if (tunnel_type_ == "vxlan" || tunnel_type_ == "VXLAN")
70  return TunnelType::VXLAN;
71  return TunnelType::INVALID;
72 }
73 
75  if (proto_ == "tcp")
76  return 6;
77  if (proto_ == "udp")
78  return 17;
79  if (proto_ == "icmp")
80  return 1;
81 
82  return atoi(proto_.c_str());
83 }
84 
86  if (trap_code_ == "flow")
88 
89  assert(0);
90  return AgentHdr::INVALID;
91 }
92 
94  if (pkt_module_ == "flow" || pkt_module_ == "FLOW")
95  return PktHandler::FLOW;
96  if (pkt_module_ == "invalid" || pkt_module_ == "INVALID")
97  return PktHandler::INVALID;
98 
99  assert(0);
100  return PktHandler::INVALID;
101 }
102 
104  if (fwd_mode_ == "l2" || fwd_mode_ == "L2")
105  return true;
106  return false;
107 }
108 
110  if (fwd_mode_ == "l3" || fwd_mode_ == "L3")
111  return true;
112  return false;
113 }
114 
115 bool AgentUtXmlPacketUtils::ReadXml(const pugi::xml_node &node) {
116  GetStringAttribute(node, "fwd_mode", &fwd_mode_);
117  GetStringAttribute(node, "pkt-module", &pkt_module_);
118 
119  GetStringAttribute(node, "tunnel_type", &tunnel_type_);
120  if (tunnel_type_ != "") {
121  GetStringAttribute(node, "tunnel_sip", &tunnel_sip_);
122  GetStringAttribute(node, "tunnel_dip", &tunnel_dip_);
123  GetUintAttribute(node, "label", (uint16_t *)&label_);
124  GetUintAttribute(node, "vxlan-id", (uint16_t *)&vxlan_id_);
125  }
126 
127  GetStringAttribute(node, "smac", &smac_);
128  GetStringAttribute(node, "dmac", &dmac_);
129 
130  GetUintAttribute(node, "intf-id", (uint16_t *)&intf_id_);
131  GetStringAttribute(node, "intf", &intf_);
132  GetStringAttribute(node, "interface", &intf_);
133 
134  GetStringAttribute(node, "sip", &sip_);
135  GetStringAttribute(node, "dip", &dip_);
136  GetStringAttribute(node, "proto", &proto_);
137  GetUintAttribute(node, "proto", &proto_id_);
138  GetUintAttribute(node, "sport", &sport_);
139  GetUintAttribute(node, "dport", &dport_);
140  GetUintAttribute(node, "hash_id", (uint16_t *)&hash_id_);
141 
142  if (hash_id_ == 0) {
143  hash_id_ = hash_id++;
144  }
145  return true;
146 }
147 
149  stringstream s;
150  if (tunnel_type_ == "") {
151  s << "Packet < " << name_ << "> Interface <" << intf_ << "> ";
152  } else {
153  s << "Packet < " << name_ << "> Tunnel <" << tunnel_type_ << " : "
154  << intf_ << " : " << label_ << " : " << tunnel_sip_ << " : "
155  << tunnel_dip_ << "> ";
156  }
157  s << "<" << sip_ << " : " << dip_ << " : " << proto_ << " : " << sport_
158  << " : " << dport_ << ">" << endl;
159  return s.str();
160 }
161 
162 static int GetVxlan(Interface *intf) {
163  int vxlan = 0;
164  if (intf) {
165  VmInterface *vmi = static_cast<VmInterface *>(intf);
166  if (vmi->vn()) {
167  vxlan = vmi->vn()->GetVxLanId();
168  }
169  }
170  return vxlan;
171 }
172 
174  PktGen *pkt) {
175  TunnelType::Type tun_type = GetTunnelType();
176  if (tun_type != TunnelType::INVALID) {
177  pkt->AddEthHdr("00:00:00:00:00:01", "00:00:00:00:00:02", 0x800);
178  const VmInterface *vhost = static_cast<const VmInterface *>
179  (agent->vhost_interface());
180  string dip = tunnel_dip_;
181  if (dip == "0.0.0.0") {
182  dip = vhost->primary_ip_addr().to_string();
183  }
184 
185  uint8_t proto = IPPROTO_UDP;
186  if (tun_type == TunnelType::MPLS_GRE)
187  proto = IPPROTO_GRE;
188 
189  pkt->AddIpHdr(tunnel_sip_.c_str(), dip.c_str(), proto);
190  if (tun_type == TunnelType::MPLS_GRE) {
191  pkt->AddGreHdr();
192  pkt->AddMplsHdr(label_, true);
193  } else if (tun_type == TunnelType::MPLS_UDP) {
194  pkt->AddUdpHdr(VR_MPLS_OVER_UDP_SRC_PORT, VR_MPLS_OVER_UDP_DST_PORT,
195  (len_ + 100));
196  pkt->AddMplsHdr(label_, true);
197  } else if (tun_type == TunnelType::VXLAN) {
198  pkt->AddUdpHdr(VR_VXLAN_UDP_SRC_PORT, VR_VXLAN_UDP_DST_PORT, 0);
199  int vxlan = GetVxlan(intf);
200  pkt->AddVxlanHdr(vxlan);
201  }
202 
203  if (fwd_mode_ == "l2") {
204  pkt->AddEthHdr(dmac_.c_str(), smac_.c_str(), 0x800);
205  }
206  } else {
207  pkt->AddEthHdr(dmac_.c_str(), smac_.c_str(), 0x800);
208  }
209 
210  pkt->AddIpHdr(sip_.c_str(), dip_.c_str(), proto_id_);
211  if (proto_id_ == 17) {
212  pkt->AddUdpHdr(sport_, dport_, len_);
213  } else if (proto_id_ == 6) {
214  pkt->AddTcpHdr(sport_, dport_, false, false, tcp_ack_, len_);
215  } else if (proto_id_ == 1) {
216  pkt->AddIcmpHdr();
217  } else {
218  assert(0);
219  }
220  return true;
221 }
222 
224  PktGen *pkt) {
225  return true;
226 }
227 
228 bool AgentUtXmlPacketUtils::MakePacket(Agent *agent, PktGen *pkt) {
229  bool ret = false;
230 
231  boost::system::error_code ec;
232  IpAddress ip = IpAddress::from_string(sip_, ec);
233 
234  bool ingress_flow = true;
235  TunnelType::Type tunnel_type = GetTunnelType();
236  if (tunnel_type != TunnelType::INVALID) {
237  ingress_flow = false;
238  }
239 
240  VmInterface *intf = NULL;
241  if (atoi(intf_.c_str())) {
242  intf = static_cast<VmInterface *>(VmPortGet(atoi(intf_.c_str())));
243  if (intf) {
244  intf_id_ = intf->id();
245  }
246  }
247 
248  if (tunnel_type == TunnelType::MPLS_GRE) {
249  if (IsL2Mode()) {
250  label_ = intf->l2_label();
251  } else {
252  label_ = intf->label();
253  }
254  }
255 
256  if (fwd_mode_ != "l2") {
257  if (ingress_flow)
258  dmac_ = agent->vrrp_mac().ToString();
259  else
260  dmac_ = intf->mac().ToString();
261  }
262 
263  if (proto_id_ == 0) {
264  proto_id_ = GetIpProto();
265  }
266 
267  uint16_t if_id = intf_id_;
268  if (tunnel_type != TunnelType::INVALID) {
269  const VmInterface *vhost = static_cast<const VmInterface *>
270  (agent->vhost_interface());
271  if_id = vhost->parent_list()[0]->id();
272  }
273 
274  int vxlan_id = 0;
275  if ((tunnel_type == TunnelType::VXLAN) && (ingress_flow == false)) {
276  vxlan_id = GetVxlan(intf);
277  }
278 
279  if (intf->vmi_type() == VmInterface::BAREMETAL) {
280  vxlan_id = GetVxlan(intf);
281  }
282 
283  pkt->AddEthHdr("00:00:00:00:00:01", "00:00:00:00:00:02", 0x800);
284  pkt->AddAgentHdr(if_id, GetTrapCode(), hash_id_, vrf_id_, label_, vxlan_id);
285  if (ip.is_v4()) {
286  ret = InetPacket(intf, agent, pkt);
287  } else {
288  ret = Inet6Packet(intf, agent, pkt);
289  }
290 
291  return ret;
292 }
293 
295 // AgentUtXmlPacket routines
297 AgentUtXmlPacket::AgentUtXmlPacket(const string &name, const xml_node &node,
298  AgentUtXmlTestCase *test_case) :
299  AgentUtXmlNode(name, node, false, test_case), pkt_() {
300 }
301 
303 }
304 
307  return pkt_.ReadXml(node());
308 }
309 
310 bool AgentUtXmlPacket::ToXml(xml_node *parent) {
311  assert(0);
312  return true;
313 }
314 
315 void AgentUtXmlPacket::ToString(string *str) {
316  *str = pkt_.ToString();
317  return;
318 }
319 
321  return "packet";
322 }
323 
325  cout << "Generate packet" << endl;
326  Agent *agent = Agent::GetInstance();
327  PktGen pkt;
328  pkt_.MakePacket(agent, &pkt);
329 
330  uint8_t *ptr(new uint8_t[pkt.GetBuffLen()]);
331  memcpy(ptr, pkt.GetBuff(), pkt.GetBuffLen());
332 
333  TestPkt0Interface *pkt0 = static_cast<TestPkt0Interface *>
334  (agent->pkt()->control_interface());
335  pkt0->ProcessFlowPacket(ptr, pkt.GetBuffLen(), pkt.GetBuffLen());
336  return true;
337 }
338 
340 // AgentUtXmlPktParseValidate routines
343  const xml_node &node) :
344  AgentUtXmlValidationNode(name, node), pkt_() {
345 }
346 
348 }
349 
351  return pkt_.ReadXml(node());
352 }
353 
355  return pkt_.ToString();
356 }
357 
359  PktInfo *info) {
360  bool ret = true;
361 
362  if (type != pkt_.GetPacketModule())
363  return false;
364 
365  if (type == PktHandler::INVALID)
366  return true;
367 
368  if (info->ip_saddr != Ip4Address::from_string(pkt_.sip_) &&
369  (info->ip_saddr != Ip6Address::from_string(pkt_.sip_)))
370  return false;
371 
372  if (info->ip_daddr != Ip4Address::from_string(pkt_.dip_) &&
373  (info->ip_daddr != Ip6Address::from_string(pkt_.dip_)))
374  return false;
375 
376  if (pkt_.IsL3Mode() == false) {
377  if (info->smac != MacAddress::FromString(pkt_.smac_))
378  return false;
379  if (info->dmac != MacAddress::FromString(pkt_.dmac_))
380  return false;
381  }
382 
383  if (pkt_.proto_id_ != info->ip_proto) {
384  return false;
385  }
386 
387  if (pkt_.proto_id_ == 6 || pkt_.proto_id_ == 17) {
388  if (pkt_.sport_ != info->sport)
389  return false;
390  if (pkt_.dport_ != info->dport)
391  return false;
392  }
393 
394  return ret;
395 }
396 
398  Agent *agent = Agent::GetInstance();
399  cout << "Generating packet" << endl;
400 
401  PktGen pkt;
402  pkt_.MakePacket(agent, &pkt);
403 
404  uint32_t buff_len = pkt.GetBuffLen();
405  uint32_t payload_len = pkt.GetBuffLen();
406  uint8_t *ptr(new uint8_t[buff_len]);
407  memcpy(ptr, pkt.GetBuff(), pkt.GetBuffLen());
408 
410  (PktHandler::RX_PACKET, ptr, buff_len, buff_len - payload_len,
411  payload_len, 0));
412 
413  PktInfo pkt_info(buff);
414  VrouterControlInterface *pkt0 = static_cast<VrouterControlInterface *>
415  (agent->pkt()->control_interface());
416 
417  AgentHdr agent_hdr;
418  pkt0->DecodeAgentHdr(&agent_hdr, (uint8_t *)(ptr), payload_len);
419 
421  type = agent->pkt()->pkt_handler()->ParsePacket(agent_hdr, &pkt_info,
422  (ptr + ETH_HLEN +
423  sizeof(struct agent_hdr)));
424  return Validate(type, &pkt_info);
425 }
boost::asio::ip::address IpAddress
Definition: address.h:14
const pugi::xml_node & node() const
Definition: test_xml.h:132
virtual bool ReadXml()=0
Definition: test_xml.cc:467
TunnelType::Type GetTunnelType() const
bool ReadXml(const pugi::xml_node &node)
bool InetPacket(Interface *intf, Agent *agent, PktGen *pkt)
virtual ~AgentUtXmlPacketUtils()
PktHandler::PktModuleName GetPacketModule() const
uint8_t GetTrapCode() const
bool MakePacket(Agent *agent, PktGen *pkt)
bool Inet6Packet(Interface *intf, Agent *agent, PktGen *pkt)
uint8_t GetIpProto() const
AgentUtXmlPacketUtils pkt_
virtual bool ReadXml()
virtual std::string NodeType()
virtual void ToString(std::string *str)
virtual bool Run()
AgentUtXmlPacket(const std::string &name, const pugi::xml_node &node, AgentUtXmlTestCase *test_case)
virtual ~AgentUtXmlPacket()
virtual bool ToXml(pugi::xml_node *parent)
virtual const std::string ToString()
AgentUtXmlPacketUtils pkt_
AgentUtXmlPktParseValidate(const std::string &name, const pugi::xml_node &node)
const pugi::xml_node & node() const
Definition: agent.h:361
static const MacAddress & vrrp_mac()
Definition: agent.h:442
static Agent * GetInstance()
Definition: agent.h:439
const Interface * vhost_interface() const
Definition: agent.h:938
PktModule * pkt() const
Definition: agent.cc:969
const MacAddress & mac() const
Definition: interface.h:134
uint32_t label() const
Definition: interface.h:130
uint32_t l2_label() const
Definition: interface.h:131
const uint32_t id() const
Definition: interface.h:126
std::string ToString() const
Definition: mac_address.cc:54
static MacAddress FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: mac_address.cc:72
PacketBufferPtr Allocate(uint32_t module, uint16_t len, uint32_t mdata)
PktModuleName ParsePacket(const AgentHdr &hdr, PktInfo *pkt_info, uint8_t *pkt)
Definition: pkt_handler.cc:281
PacketBufferManager * packet_buffer_manager() const
Definition: pkt_init.h:34
PktHandler * pkt_handler() const
Definition: pkt_init.h:32
ControlInterface * control_interface() const
Definition: pkt_init.h:47
@ MPLS_UDP
Definition: nexthop.h:243
@ MPLS_GRE
Definition: nexthop.h:242
VmInterface::VmiType vmi_type() const
const VnEntry * vn() const
const Ip4Address & primary_ip_addr() const
InterfaceList parent_list() const
int GetVxLanId() const
Definition: vn.cc:729
int DecodeAgentHdr(AgentHdr *hdr, uint8_t *buff, uint32_t len)
uint8_t type
Definition: load_balance.h:2
bool GetUintAttribute(const xml_node &node, const string &name, uint16_t *value)
Definition: test_xml.cc:38
bool GetStringAttribute(const xml_node &node, const string &name, string *value)
Definition: test_xml.cc:26
boost::shared_ptr< PacketBuffer > PacketBufferPtr
Definition: packet_buffer.h:19
@ TRAP_FLOW_MISS
Definition: pkt_handler.h:137
MacAddress dmac
Definition: pkt_handler.h:393
uint32_t dport
Definition: pkt_handler.h:398
IpAddress ip_daddr
Definition: pkt_handler.h:395
uint32_t sport
Definition: pkt_handler.h:397
IpAddress ip_saddr
Definition: pkt_handler.h:394
MacAddress smac
Definition: pkt_handler.h:392
uint8_t ip_proto
Definition: pkt_handler.h:396
static int GetVxlan(Interface *intf)
int hash_id