OpenSDN source code
overlay_traceroute.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <stdint.h>
7 #include <netinet/udp.h>
8 #include "vr_defs.h"
9 #include "cmn/agent_cmn.h"
10 #include "oper/route_common.h"
11 #include "pkt/proto.h"
12 #include "pkt/proto_handler.h"
13 #include "diag/diag_types.h"
14 #include "diag/diag.h"
16 #include "diag/overlay_ping.h"
17 #include "diag/traceroute.h"
18 #include "diag/diag_pkt_handler.h"
19 
20 using namespace boost::posix_time;
21 OverlayTraceRoute::OverlayTraceRoute(const OverlayTraceReq *traceroute_req,
22  DiagTable *diag_table) :
23  DiagEntry(traceroute_req->get_source_ip(), traceroute_req->get_dest_ip(),
24  traceroute_req->get_protocol(), traceroute_req->get_source_port(),
25  traceroute_req->get_dest_port(), Agent::GetInstance()->fabric_vrf_name(),
26  traceroute_req->get_interval() * 100,traceroute_req->get_max_attempts(), diag_table),
27  vn_uuid_(StringToUuid(traceroute_req->get_vn_uuid())),
28  remote_vm_mac_(traceroute_req->get_vm_remote_mac()), ttl_(2),
29  max_ttl_(traceroute_req->get_max_hops()),
30  context_(traceroute_req->context()) {
31 }
32 
34 }
35 
36 
38 {
39  Agent *agent = diag_table_->agent();
40  Ip4Address tunneldst;
41  Ip4Address tunnelsrc;
42  seq_no_++;
43  string vrf_name;
44  boost::system::error_code ec;
45  uint8_t data_len = 50;
46  int vxlan_id = agent->vn_table()->Find(vn_uuid_)->GetVxLanId();
47  VxLanId *vxlan = agent->vxlan_table()->Find(vxlan_id);
48  if (!vxlan)
49  return;
50 
52  remote_vm_mac_.ToString(), agent);
53  if (!rt)
54  return;
55  const AgentPath *path = rt->GetActivePath();
56  const TunnelNH *nh = static_cast<const TunnelNH *>(path->nexthop());
57 
58  tunneldst = *nh->GetDip();
59  tunnelsrc = *nh->GetSip();
61  boost::shared_ptr<PktInfo> pkt_info(new PktInfo(agent, len_,
62  PktHandler::DIAG, 0));
63  uint8_t *buf = pkt_info->packet_buffer()->data();
64  memset(buf, 0, len_);
65  OverlayOamPktData *pktdata = NULL;
67 
68  boost::posix_time::ptime senttime = microsec_clock::universal_time();
69  FillOamPktHeader(pktdata, vxlan_id, senttime);
70  DiagPktHandler *pkt_handler = new DiagPktHandler(diag_table_->agent(), pkt_info,
71  *(diag_table_->agent()->event_manager())->io_service());
72  // FIll outer header
73  pkt_info->eth = (struct ether_header *)(buf);
74  pkt_handler->EthHdr(agent->vhost_interface()->mac(), *nh->GetDmac(),
75  ETHERTYPE_IP);
76  pkt_info->ip = (struct ip *)(pkt_info->eth +1);
77  pkt_info->transp.udp = (struct udphdr *)(pkt_info->ip + 1);
78  uint8_t len;
79  len = data_len+2 * sizeof(udphdr)+sizeof(VxlanHdr)+
80  sizeof(struct ip) + sizeof(struct ether_header);
81  pkt_handler->UdpHdr(len, ntohl(tunnelsrc.to_ulong()), HashValUdpSourcePort(),
82  ntohl(tunneldst.to_ulong()), VXLAN_UDP_DEST_PORT);
83 
84  pkt_handler->IpHdr(len + sizeof(struct ip), ntohl(tunnelsrc.to_ulong()),
85  ntohl(tunneldst.to_ulong()), IPPROTO_UDP,
87  // Fill VxLan Header
88  VxlanHdr *vxlanhdr = (VxlanHdr *)(buf + sizeof(udphdr)+ sizeof(struct ip)
89  + sizeof(struct ether_header));
90  vxlanhdr->vxlan_id = ntohl(vxlan_id << 8);
92 
93  //Fill inner packet details.
94  pkt_info->eth = (struct ether_header *)(vxlanhdr + 1);
95 
97  ETHERTYPE_IP);
98 
99  pkt_info->ip = (struct ip *)(pkt_info->eth +1);
100  Ip4Address dip = Ip4Address::from_string("127.0.0.1", ec);
101  pkt_info->transp.udp = (struct udphdr *)(pkt_info->ip + 1);
102  len = data_len+sizeof(struct udphdr);
103  pkt_handler->UdpHdr(len, sip_.to_v4().to_ulong(), sport_,
104  dip.to_ulong(), VXLAN_UDP_DEST_PORT);
105  pkt_handler->IpHdr(len + sizeof(struct ip), ntohl(sip_.to_v4().to_ulong()),
106  ntohl(dip.to_ulong()), proto_,
108  //pkt_handler->SetDiagChkSum();
109  pkt_handler->pkt_info()->set_len(len_);
111  Interface *intf = static_cast<Interface *>
112  (agent->interface_table()->Find(&key1, true));
113  pkt_handler->Send(intf->id(), agent->fabric_vrf()->vrf_id(),
114  AgentHdr::TX_SWITCH, CMD_PARAM_PACKET_CTRL,
115  CMD_PARAM_1_DIAG, PktHandler::DIAG);
116 
117  delete pkt_handler;
118  return;
119 }
120 
121 void OverlayTraceReq::HandleRequest() const {
122  std::string err_str;
123  boost::system::error_code ec;
124  OverlayTraceRoute *overlaytraceroute = NULL;
125 
126  {
127  Agent *agent = Agent::GetInstance();
128  boost::uuids::uuid vn_uuid = StringToUuid(get_vn_uuid());
129  IpAddress sip(IpAddress::from_string(get_source_ip(), ec));
130  if (ec.failed()) {
131  err_str = "Invalid source IP";
132  goto error;
133  }
134 
135  if (!sip.is_v4()) {
136  err_str = "V6 is not supported";
137  goto error;
138  }
139 
140  IpAddress dip(IpAddress::from_string(get_dest_ip(), ec));
141  if (ec.failed()) {
142  err_str = "Invalid destination IP";
143  goto error;
144  }
145 
146  if (!dip.is_v4()) {
147  err_str = "V6 is not supported";
148  goto error;
149  }
150 
151  uint8_t proto = get_protocol();
152  if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
153  err_str = "Invalid protocol - Supported protocols are TCP & UDP";
154  goto error;
155  }
156  VnEntry *vn = agent->vn_table()->Find(vn_uuid);
157 
158  if (!vn) {
159  err_str = "Invalid VN segment";
160  goto error;
161  }
162 
163  int vxlan_id = vn->GetVxLanId();
164  VxLanId* vxlan = agent->vxlan_table()->Find(vxlan_id);
165 
166  if (!vxlan) {
167  err_str = "Invalid vxlan segment";
168  goto error;
169  }
170 
172  get_vm_remote_mac(), agent);
173  if (!rt) {
174  err_str = "Invalid remote mac";
175  goto error;
176  }
177  }
178  overlaytraceroute = new OverlayTraceRoute(this,
179  Agent::GetInstance()->diag_table());
180  overlaytraceroute->Init();
181  overlaytraceroute->ReplyLocalHop();
182  return;
183 
184 error:
185  TraceRouteErrResp *resp = new TraceRouteErrResp;
186  resp->set_error_response(err_str);
187  resp->set_context(context());
188  resp->Response();
189  return;
190 }
191 // if timed out max times for a TTL, reply and increment ttl
193  if (seq_no_ >= GetMaxAttempts()) {
194  std::string address;
195  for (uint32_t i = 0; i < GetMaxAttempts(); i++)
196  address += "* ";
197 
198  done_ = ((ttl_ >= max_ttl_) ? true : false);
200  IncrementTtl();
201  }
202 }
203 
204 // Ready to send a response and increment ttl
206  if (ttl_ >= max_ttl_) {
207  handler->set_done(true);
208  done_ = true;
209  }
210  struct ip *ip = handler->pkt_info()->tunnel.ip;
211  IpAddress saddr = IpAddress(Ip4Address(ntohl(ip->ip_src.s_addr)));
212  TraceRoute::SendSandeshReply(saddr.to_v4().to_string(), context_,
213  !handler->IsDone());
214  IncrementTtl();
215 }
216 
217 // Reply with local node as the first hop
220  context_, true);
221 }
222 
224  ttl_++;
225  seq_no_ = 0;
226 }
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
AgentDBEntry * Find(const DBEntry *key, bool ret_del)
Definition: agent_db.cc:135
NextHop * nexthop() const
Definition: agent_path.cc:88
const AgentPath * GetActivePath() const
Definition: agent_route.cc:878
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
EventManager * event_manager() const
Definition: agent.h:1106
VxLanTable * vxlan_table() const
Definition: agent.h:538
VrfEntry * fabric_vrf() const
Definition: agent.h:918
VnTable * vn_table() const
Definition: agent.h:498
static Agent * GetInstance()
Definition: agent.h:439
const std::string & fabric_interface_name() const
Definition: agent.h:1132
const Interface * vhost_interface() const
Definition: agent.h:938
Ip4Address router_id() const
Definition: agent.h:669
Definition: diag.h:22
uint32_t seq_no_
Definition: diag.h:71
DiagTable * diag_table_
Definition: diag.h:65
void Init()
Definition: diag.cc:48
uint32_t HashValUdpSourcePort()
Definition: diag.cc:164
void FillOamPktHeader(OverlayOamPktData *pktdata, uint32_t vxlan_id, const boost::posix_time::ptime &time)
Definition: diag.cc:174
uint16_t sport_
Definition: diag.h:60
IpAddress sip_
Definition: diag.h:57
uint8_t proto_
Definition: diag.h:59
uint32_t GetMaxAttempts()
Definition: diag.h:43
bool IsDone() const
void set_done(bool done)
Agent * agent() const
Definition: diag.h:118
const MacAddress & mac() const
Definition: interface.h:134
const uint32_t id() const
Definition: interface.h:126
std::string ToString() const
Definition: mac_address.cc:54
static const uint32_t kVxlanIBit
Definition: overlay_ping.h:23
static BridgeRouteEntry * L2RouteGet(VxLanId *vxlan, string remotemac, Agent *agent)
Definition: overlay_ping.cc:34
static const uint32_t kOverlayUdpHdrLength
Definition: overlay_ping.h:19
static const MacAddress in_source_mac_
Definition: overlay_ping.h:25
static const MacAddress in_dst_mac_
Definition: overlay_ping.h:24
static const uint32_t kVxlanRABit
Definition: overlay_ping.h:22
void RequestTimedOut(uint32_t seqno)
OverlayTraceRoute(const OverlayTraceReq *req, DiagTable *diag_table)
virtual void SendRequest()
boost::uuids::uuid vn_uuid_
virtual void HandleReply(DiagPktHandler *handler)
void Send(uint32_t itf, uint32_t vrf, uint16_t, PktHandler::PktModuleName)
void IpHdr(uint16_t len, in_addr_t src, in_addr_t dest, uint8_t protocol, uint16_t id, uint8_t ttl)
void UdpHdr(uint16_t len, in_addr_t src, uint16_t src_port, in_addr_t dest, uint16_t dest_port)
int EthHdr(const MacAddress &src, const MacAddress &dest, const uint16_t proto)
PktInfo * pkt_info() const
Definition: proto_handler.h:89
static void SendSandeshReply(const std::string &address, const std::string &context, bool more)
Definition: traceroute.cc:23
const Ip4Address * GetDip() const
Definition: tunnel_nh.h:38
const Ip4Address * GetSip() const
Definition: tunnel_nh.h:37
const MacAddress * GetDmac() const
Definition: tunnel_nh.h:40
Definition: vn.h:152
int GetVxLanId() const
Definition: vn.cc:729
VnEntry * Find(const boost::uuids::uuid &vn_uuid)
Definition: vn.cc:771
const uint32_t vrf_id() const
Definition: vrf.h:103
Definition: vxlan.h:14
VxLanId * Find(uint32_t vxlan_id)
Definition: vxlan.cc:222
#define DEFAULT_IP_ID
Definition: pkt_handler.h:48
#define DEFAULT_IP_TTL
Definition: pkt_handler.h:44
#define VXLAN_UDP_DEST_PORT
Definition: pkt_handler.h:36
static boost::uuids::uuid StringToUuid(const std::string &str)
Definition: string_util.h:146
TunnelInfo tunnel
Definition: pkt_handler.h:407
void set_len(uint32_t len)
struct ip * ip
Definition: pkt_handler.h:218
uint32_t vxlan_id
Definition: pkt_handler.h:92
uint32_t reserved
Definition: pkt_handler.h:91
boost::uuids::uuid uuid