OpenSDN source code
ping.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <stdint.h>
7 #include "base/os.h"
8 #include "vr_defs.h"
9 #include "cmn/agent_cmn.h"
10 #include "oper/nexthop.h"
11 #include "oper/route_common.h"
12 #include "oper/mirror_table.h"
13 #include "pkt/proto.h"
14 #include "pkt/proto_handler.h"
15 #include "diag/diag_types.h"
16 #include "diag/diag_pkt_handler.h"
17 #include "diag/diag.h"
18 #include "diag/ping.h"
19 
20 using namespace boost::posix_time;
21 
22 Ping::Ping(const PingReq *ping_req, DiagTable *diag_table):
23  DiagEntry(ping_req->get_source_ip(), ping_req->get_dest_ip(),
24  ping_req->get_protocol(), ping_req->get_source_port(),
25  ping_req->get_dest_port(), ping_req->get_vrf_name(),
26  ping_req->get_interval() * 100, ping_req->get_count(), diag_table),
27  data_len_(ping_req->get_packet_size()), context_(ping_req->context()),
28  pkt_lost_count_(0) {
29 
30 }
31 
33 }
34 
35 void
38  ad->key_ = htons(key_);
39  ad->seq_no_ = htonl(seq_no_);
40  ad->rtt_ = microsec_clock::universal_time();
41  memset(ad->data_, 0, sizeof(ad->data_));
42 }
43 
46  //Allocate buffer to hold packet
47  if (sip_.is_v4()) {
49  } else {
51  }
52  boost::shared_ptr<PktInfo> pkt_info(new PktInfo(agent, len_,
53  PktHandler::DIAG, 0));
54  uint8_t *msg = pkt_info->packet_buffer()->data();
55  memset(msg, 0, len_);
56 
57  AgentDiagPktData *ad;
58  if (sip_.is_v4()) {
59  ad = (AgentDiagPktData *)(msg + KPingTcpHdr);
60  } else {
61  ad = (AgentDiagPktData *)(msg + KPing6TcpHdr);
62  }
63 
64  FillAgentHeader(ad);
65  DiagPktHandler *pkt_handler = new DiagPktHandler(diag_table_->agent(), pkt_info,
66  *(diag_table_->agent()->event_manager())->io_service());
67 
68  //Update pointers to ethernet header, ip header and l4 header
69  if (sip_.is_v4()) {
70  pkt_info->UpdateHeaderPtr();
71  pkt_handler->TcpHdr(htonl(sip_.to_v4().to_ulong()), sport_,
72  htonl(dip_.to_v4().to_ulong()), dport_, false, rand(),
73  data_len_ + sizeof(tcphdr));
74  pkt_handler->IpHdr(data_len_ + sizeof(tcphdr) + sizeof(struct ip),
75  ntohl(sip_.to_v4().to_ulong()),
76  ntohl(dip_.to_v4().to_ulong()), IPPROTO_TCP,
78  pkt_handler->EthHdr(agent->vhost_interface()->mac(),
79  agent->vrrp_mac(), ETHERTYPE_IP);
80  }else {
81  pkt_info->eth = (struct ether_header *)(pkt_info->pkt);
82  pkt_info->ip6 = (struct ip6_hdr *)(pkt_info->eth + 1);
83  pkt_info->transp.tcp = (struct tcphdr *)(pkt_info->ip6 + 1);
84  pkt_handler->TcpHdr(data_len_ + sizeof(tcphdr),
85  (uint8_t *)sip_.to_v6().to_string().c_str(), sport_,
86  (uint8_t *)dip_.to_v6().to_string().c_str(), dport_,
87  false, rand(), IPPROTO_TCP);
88  pkt_handler->Ip6Hdr(pkt_info->ip6,
89  data_len_ + sizeof(tcphdr) + sizeof(struct ip6_hdr),
90  IPPROTO_TCP, DEFAULT_IP_TTL,
91  sip_.to_v6().to_bytes().data(),
92  dip_.to_v6().to_bytes().data());
93  pkt_handler->EthHdr(agent->vhost_interface()->mac(),
94  agent->vrrp_mac(), ETHERTYPE_IPV6);
95  }
96 
97  return pkt_handler;
98 }
99 
102  //Allocate buffer to hold packet
103  if (sip_.is_v4()) {
105  } else {
107  }
108  boost::shared_ptr<PktInfo> pkt_info(new PktInfo(agent, len_,
109  PktHandler::DIAG, 0));
110  uint8_t *msg = pkt_info->packet_buffer()->data();
111  memset(msg, 0, len_);
112 
113  AgentDiagPktData *ad;
114  if (sip_.is_v4()) {
115  ad = (AgentDiagPktData *)(msg + KPingUdpHdr);
116  } else {
117  ad = (AgentDiagPktData *)(msg + KPing6UdpHdr);
118  }
119 
120  FillAgentHeader(ad);
121 
122  DiagPktHandler *pkt_handler = new DiagPktHandler(diag_table_->agent(), pkt_info,
123  *(diag_table_->agent()->event_manager())->io_service());
124 
125  //Update pointers to ethernet header, ip header and l4 header
126  if (sip_.is_v4()) {
127  pkt_info->UpdateHeaderPtr();
128  pkt_handler->UdpHdr(data_len_+ sizeof(udphdr), sip_.to_v4().to_ulong(), sport_,
129  dip_.to_v4().to_ulong(), dport_);
130  pkt_handler->IpHdr(data_len_ + sizeof(udphdr) + sizeof(struct ip),
131  ntohl(sip_.to_v4().to_ulong()),
132  ntohl(dip_.to_v4().to_ulong()), IPPROTO_UDP,
134  pkt_handler->EthHdr(agent->vhost_interface()->mac(),
135  agent->vrrp_mac(), ETHERTYPE_IP);
136  } else {
137  pkt_info->eth = (struct ether_header *)(pkt_info->pkt);
138  pkt_info->ip6 = (struct ip6_hdr *)(pkt_info->eth + 1);
139  pkt_info->transp.udp = (struct udphdr *)(pkt_info->ip6 + 1);
140  pkt_handler->UdpHdr(data_len_ + sizeof(udphdr),
141  sip_.to_v6().to_bytes().data(), sport_,
142  dip_.to_v6().to_bytes().data(), dport_,
143  IPPROTO_UDP);
144  pkt_handler->Ip6Hdr(pkt_info->ip6,
145  data_len_ + sizeof(udphdr) + sizeof(struct ip6_hdr),
146  IPPROTO_UDP, DEFAULT_IP_TTL,
147  sip_.to_v6().to_bytes().data(),
148  dip_.to_v6().to_bytes().data());
149  pkt_handler->EthHdr(agent->vhost_interface()->mac(),
150  agent->vrrp_mac(), ETHERTYPE_IPV6);
151  }
152  return pkt_handler;
153 }
154 
156  Agent *agent = Agent::GetInstance();
157  DiagPktHandler *pkt_handler = NULL;
158  //Increment the attempt count
159  seq_no_++;
160  switch(proto_) {
161  case IPPROTO_TCP:
162  pkt_handler = CreateTcpPkt(agent);
163  break;
164 
165  case IPPROTO_UDP:
166  pkt_handler = CreateUdpPkt(agent);
167  break;
168  }
169 
171  if (sip_.is_v4()) {
172  table = agent->vrf_table()->GetInet4UnicastRouteTable(vrf_name_);
173  } else {
174  table = agent->vrf_table()->GetInet6UnicastRouteTable(vrf_name_);
175  }
176  AgentRoute *rt = table->FindRoute(sip_);
177  if (!rt) {
178  delete pkt_handler;
179  return;
180  }
181 
182  const NextHop *nh;
183  nh = rt->GetActiveNextHop();
184  if (!nh || nh->GetType() != NextHop::INTERFACE) {
185  delete pkt_handler;
186  return;
187  }
188 
189  const InterfaceNH *intf_nh;
190  intf_nh = static_cast<const InterfaceNH *>(nh);
191 
192  uint32_t intf_id = intf_nh->GetInterface()->id();
193  uint32_t vrf_id = diag_table_->agent()->vrf_table()->FindVrfFromName(vrf_name_)->vrf_id();
194  //Send request out
195  pkt_handler->SetDiagChkSum();
196  pkt_handler->pkt_info()->set_len(len_);
197  pkt_handler->Send(intf_id, vrf_id, AgentHdr::TX_ROUTE,
198  CMD_PARAM_PACKET_CTRL, CMD_PARAM_1_DIAG, PktHandler::DIAG);
199  delete pkt_handler;
200  return;
201 }
202 
203 void Ping::RequestTimedOut(uint32_t seqno) {
204  PingResp *resp = new PingResp();
205  pkt_lost_count_++;
206  resp->set_resp("Timed Out");
207  resp->set_seq_no(seqno);
208  resp->set_context(context_);
209  resp->set_more(true);
210  resp->Response();
211 }
212 
213 void time_duration_to_string(time_duration &td, std::string &str) {
214  std::ostringstream td_str;
215 
216  if (td.minutes()) {
217  td_str << td.minutes() << "m " << td.seconds() << "s";
218  } else if (td.total_milliseconds()) {
219  td_str << td.total_milliseconds() << "ms";
220  } else if (td.total_microseconds()) {
221  td_str << td.total_microseconds() << "us";
222  } else {
223  td_str << td.total_nanoseconds() << "ns";
224  }
225 
226  str = td_str.str();
227 }
228 
230  //Send reply
231  PingResp *resp = new PingResp();
232  AgentDiagPktData *ad = (AgentDiagPktData *)handler->GetData();
233 
234  resp->set_seq_no(ntohl(ad->seq_no_));
235 
236  //Calculate rtt
237  time_duration rtt = microsec_clock::universal_time() - ad->rtt_;
238  avg_rtt_ += rtt;
239  std::string rtt_str;
240  time_duration_to_string(rtt, rtt_str);
241  resp->set_rtt(rtt_str);
242 
243  resp->set_resp("Success");
244  resp->set_context(context_);
245  resp->set_more(true);
246  resp->Response();
247 }
248 
250  PingSummaryResp *resp = new PingSummaryResp();
251 
252  if (pkt_lost_count_ != GetMaxAttempts()) {
253  //If we had some succesful replies, send in
254  //average rtt for succesful ping requests
256  std::string avg_rtt_string;
257  time_duration_to_string(avg_rtt_, avg_rtt_string);
258  resp->set_average_rtt(avg_rtt_string);
259  }
260 
261  resp->set_request_sent(seq_no_);
262  resp->set_response_received(seq_no_ - pkt_lost_count_);
263  uint32_t pkt_loss_percent = (pkt_lost_count_ * 100/seq_no_);
264  resp->set_pkt_loss(pkt_loss_percent);
265  resp->set_context(context_);
266  resp->Response();
267 }
268 
269 void PingReq::HandleRequest() const {
270  std::string err_str;
271  boost::system::error_code ec;
272  Ping *ping = NULL;
273 
274  {
275  IpAddress sip(IpAddress::from_string(get_source_ip(), ec));
276  if (ec.failed()) {
277  err_str = "Invalid source IP";
278  goto error;
279  }
280 
281  IpAddress dip(IpAddress::from_string(get_dest_ip(), ec));
282  if (ec.failed()) {
283  err_str = "Invalid destination IP";
284  goto error;
285  }
286 
287  uint8_t proto = get_protocol();
288  if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
289  err_str = "Invalid protocol. Valid Protocols are TCP and UDP";
290  goto error;
291  }
292 
293  if (Agent::GetInstance()->vrf_table()->FindVrfFromName(get_vrf_name()) == NULL) {
294  err_str = "Invalid VRF";
295  goto error;
296  }
297 
298  const NextHop *nh = NULL;
299  Agent *agent = Agent::GetInstance();
301  if (sip.is_v4()) {
302  table = agent->vrf_table()->GetInet4UnicastRouteTable(get_vrf_name());
303  } else {
304  table = agent->vrf_table()->GetInet6UnicastRouteTable(get_vrf_name());
305  }
306  AgentRoute *rt = table->FindRoute(sip);
307  if (rt) {
308  nh = rt->GetActiveNextHop();
309  }
310  if (!nh || nh->GetType() != NextHop::INTERFACE) {
311  err_str = "VM not present on this server";
312  goto error;
313  }
314  }
315  ping = new Ping(this, Agent::GetInstance()->diag_table());
316  ping->Init();
317  return;
318 
319 error:
320  PingErrResp *resp = new PingErrResp;
321  resp->set_error_response(err_str);
322  resp->set_context(context());
323  resp->Response();
324  return;
325 }
boost::asio::ip::address IpAddress
Definition: address.h:14
Base class for all Route entries in agent.
Definition: agent_route.h:225
const NextHop * GetActiveNextHop() const
Definition: agent_route.cc:883
Definition: agent.h:361
VrfTable * vrf_table() const
Definition: agent.h:488
EventManager * event_manager() const
Definition: agent.h:1106
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
Definition: diag.h:22
uint32_t seq_no_
Definition: diag.h:71
DiagKey key_
Definition: diag.h:66
DiagTable * diag_table_
Definition: diag.h:65
void Init()
Definition: diag.cc:48
std::string vrf_name_
Definition: diag.h:62
uint16_t dport_
Definition: diag.h:61
IpAddress dip_
Definition: diag.h:58
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
void TcpHdr(in_addr_t, uint16_t, in_addr_t, uint16_t, bool, uint32_t, uint16_t)
uint8_t * GetData()
Agent * agent() const
Definition: diag.h:118
InetUnicastRouteEntry * FindRoute(const IpAddress &ip)
const Interface * GetInterface() const
Definition: nexthop.h:1294
const MacAddress & mac() const
Definition: interface.h:134
const uint32_t id() const
Definition: interface.h:126
@ INTERFACE
Definition: nexthop.h:352
Type GetType() const
Definition: nexthop.h:406
Definition: ping.h:15
DiagPktHandler * CreateTcpPkt(Agent *agent)
Definition: ping.cc:45
uint32_t pkt_lost_count_
Definition: ping.h:42
static const uint32_t KPing6UdpHdr
Definition: ping.h:21
virtual ~Ping()
Definition: ping.cc:32
Ping(const PingReq *pr, DiagTable *diag_table)
Definition: ping.cc:22
virtual void SendSummary()
Definition: ping.cc:249
boost::posix_time::time_duration avg_rtt_
Definition: ping.h:41
virtual void RequestTimedOut(uint32_t seq_no)
Definition: ping.cc:203
virtual void HandleReply(DiagPktHandler *handler)
Definition: ping.cc:229
uint16_t data_len_
Definition: ping.h:38
DiagPktHandler * CreateUdpPkt(Agent *agent)
Definition: ping.cc:101
virtual void SendRequest()
Definition: ping.cc:155
std::string context_
Definition: ping.h:40
uint16_t len_
Definition: ping.h:39
static const uint32_t KPingUdpHdr
Definition: ping.h:17
static const uint32_t KPingTcpHdr
Definition: ping.h:19
static const uint32_t KPing6TcpHdr
Definition: ping.h:23
void FillAgentHeader(AgentDiagPktData *pkt)
Definition: ping.cc:36
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
void Ip6Hdr(ip6_hdr *ip, uint16_t plen, uint8_t next_header, uint8_t hlim, uint8_t *src, uint8_t *dest)
const uint32_t vrf_id() const
Definition: vrf.h:103
VrfEntry * FindVrfFromName(const string &name)
Definition: vrf.cc:874
InetUnicastAgentRouteTable * GetInet6UnicastRouteTable(const std::string &vrf_name)
Definition: vrf.cc:923
InetUnicastAgentRouteTable * GetInet4UnicastRouteTable(const std::string &vrf_name)
Definition: vrf.cc:899
void time_duration_to_string(time_duration &td, std::string &str)
Definition: ping.cc:213
#define DEFAULT_IP_ID
Definition: pkt_handler.h:48
#define DEFAULT_IP_TTL
Definition: pkt_handler.h:44
boost::posix_time::ptime rtt_
Definition: diag.h:87
uint32_t op_
Definition: diag.h:83
uint32_t seq_no_
Definition: diag.h:85
DiagEntry::DiagKey key_
Definition: diag.h:84
@ DIAG_REQUEST
Definition: diag.h:76
char data_[8]
Definition: diag.h:86
void set_len(uint32_t len)