OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
proto_handler.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef vnsw_agent_proto_handler_hpp
6 #define vnsw_agent_proto_handler_hpp
7 
8 #include "pkt_handler.h"
9 
10 // Pseudo header for UDP checksum
11 struct PseudoUdpHdr {
12  in_addr_t src;
13  in_addr_t dest;
14  uint8_t res;
15  uint8_t prot;
16  uint16_t len;
17  PseudoUdpHdr(in_addr_t s, in_addr_t d, uint8_t p, uint16_t l) :
18  src(s), dest(d), res(0), prot(p), len(l) { }
19 };
20 
21 struct vlanhdr {
22  uint16_t tpid;
23  uint16_t tci;
24 };
25 
26 // Protocol handler, to process an incoming packet
27 // Each protocol has a handler derived from this
28 class ProtoHandler {
29 public:
30  ProtoHandler(Agent *agent, boost::shared_ptr<PktInfo> info,
31  boost::asio::io_context &io);
32  virtual ~ProtoHandler();
33 
34  virtual bool Run() = 0;
35 
36  void Send(uint32_t itf, uint32_t vrf, uint16_t, PktHandler::PktModuleName);
37  void Send(uint32_t itf, uint32_t vrf, uint16_t cmd,
38  uint32_t param1, uint32_t param2, PktHandler::PktModuleName mod);
39 
40  int EthHdr(const MacAddress &src, const MacAddress &dest,
41  const uint16_t proto);
42  int EthHdr(char *buff, uint16_t len, const MacAddress &src,
43  const MacAddress &dest, const uint16_t proto, uint16_t vlan_id);
44  int EthHdr(char *buff, uint16_t len, const Interface *interface,
45  const MacAddress &src, const MacAddress &dest,
46  const uint16_t proto);
47  int EthHdr(char *buff, uint16_t len, uint32_t ifindex,
48  const MacAddress &src, const MacAddress &dest,
49  const uint16_t proto);
50 
51  void VlanHdr(uint8_t *ptr, uint16_t tci);
52  void IpHdr(uint16_t len, in_addr_t src, in_addr_t dest, uint8_t protocol,
53  uint16_t id, uint8_t ttl);
54  uint16_t IpHdr(char *buff, uint16_t buf_len, uint16_t len, in_addr_t src,
55  in_addr_t dest, uint8_t protocol, uint16_t id, uint8_t ttl);
56  void Ip6Hdr(ip6_hdr *ip, uint16_t plen, uint8_t next_header,
57  uint8_t hlim, uint8_t *src, uint8_t *dest);
58  void UdpHdr(uint16_t len, in_addr_t src, uint16_t src_port, in_addr_t dest,
59  uint16_t dest_port);
60  void UdpHdr(udphdr *hdr, uint16_t len, const uint8_t *src, uint16_t src_port,
61  const uint8_t *dest, uint16_t dest_port, uint8_t next_hdr);
62  void UdpHdr(uint16_t len, const uint8_t *src, uint16_t src_port,
63  const uint8_t *dest, uint16_t dest_port, uint8_t next_hdr);
64  uint16_t UdpHdr(udphdr *udp, uint16_t buf_len, uint16_t len, in_addr_t src,
65  uint16_t src_port, in_addr_t dest, uint16_t dest_port);
66  uint16_t IcmpHdr(char *buff, uint16_t buf_len, uint8_t type, uint8_t code,
67  uint16_t word1, uint16_t word2);
68  void IcmpChecksum(char *buff, uint16_t buf_len);
69 
70  void IgmpChecksum(char *buff, uint16_t buf_len);
71 
72  uint32_t Sum(uint16_t *, std::size_t, uint32_t) const;
73  uint16_t Csum(uint16_t *, std::size_t, uint32_t) const;
74  uint16_t UdpCsum(in_addr_t, in_addr_t, std::size_t, udphdr *) const;
75  uint16_t Ipv6Csum(const uint8_t *src, const uint8_t *dest,
76  uint16_t plen, uint8_t next_hdr, uint16_t *hdr) const;
77  uint16_t Icmpv6Csum(const uint8_t *src, const uint8_t *dest,
78  icmp6_hdr *icmp, uint16_t plen) const;
79 
80  Agent *agent() const { return agent_; }
81  uint32_t GetVrfIndex() const { return pkt_info_->GetAgentHdr().vrf; }
82  uint32_t GetInterfaceIndex() const {
83  return pkt_info_->GetAgentHdr().ifindex;
84  }
85  uint16_t GetLength() const { return pkt_info_->len; }
86  uint32_t GetCmdParam() const { return pkt_info_->GetAgentHdr().cmd_param; }
87 
88  PktInfo *pkt_info() const { return pkt_info_.get(); }
89  uint32_t EncapHeaderLen() const;
90 protected:
92  boost::shared_ptr<PktInfo> pkt_info_;
93  boost::asio::io_context &io_;
94 
95 private:
96  void FillUdpHdr(udphdr *udp, uint16_t len,
97  uint16_t src_port, uint16_t dest_port);
98 
100 };
101 
102 #endif // vnsw_agent_proto_handler_hpp
uint32_t EncapHeaderLen() const
PseudoUdpHdr(in_addr_t s, in_addr_t d, uint8_t p, uint16_t l)
Definition: proto_handler.h:17
ProtoHandler(Agent *agent, boost::shared_ptr< PktInfo > info, boost::asio::io_context &io)
uint16_t UdpCsum(in_addr_t, in_addr_t, std::size_t, udphdr *) const
uint16_t Ipv6Csum(const uint8_t *src, const uint8_t *dest, uint16_t plen, uint8_t next_hdr, uint16_t *hdr) const
uint16_t GetLength() const
Definition: proto_handler.h:85
uint16_t Csum(uint16_t *, std::size_t, uint32_t) const
uint16_t len
Definition: proto_handler.h:16
uint32_t GetVrfIndex() const
Definition: proto_handler.h:81
Agent * agent() const
Definition: proto_handler.h:80
virtual ~ProtoHandler()
boost::shared_ptr< PktInfo > pkt_info_
Definition: proto_handler.h:92
void IgmpChecksum(char *buff, uint16_t buf_len)
int EthHdr(const MacAddress &src, const MacAddress &dest, const uint16_t proto)
uint8_t type
Definition: load_balance.h:109
Definition: agent.h:358
uint32_t GetInterfaceIndex() const
Definition: proto_handler.h:82
in_addr_t dest
Definition: proto_handler.h:13
void Send(uint32_t itf, uint32_t vrf, uint16_t, PktHandler::PktModuleName)
boost::asio::io_context & io_
Definition: proto_handler.h:93
in_addr_t src
Definition: proto_handler.h:12
void VlanHdr(uint8_t *ptr, uint16_t tci)
PktInfo * pkt_info() const
Definition: proto_handler.h:88
DISALLOW_COPY_AND_ASSIGN(ProtoHandler)
void Ip6Hdr(ip6_hdr *ip, uint16_t plen, uint8_t next_header, uint8_t hlim, uint8_t *src, uint8_t *dest)
Agent * agent_
Definition: proto_handler.h:91
uint16_t tpid
Definition: proto_handler.h:22
void IcmpChecksum(char *buff, uint16_t buf_len)
void UdpHdr(uint16_t len, in_addr_t src, uint16_t src_port, in_addr_t dest, uint16_t dest_port)
void FillUdpHdr(udphdr *udp, uint16_t len, uint16_t src_port, uint16_t dest_port)
uint8_t prot
Definition: proto_handler.h:15
uint32_t Sum(uint16_t *, std::size_t, uint32_t) const
uint16_t IcmpHdr(char *buff, uint16_t buf_len, uint8_t type, uint8_t code, uint16_t word1, uint16_t word2)
uint16_t Icmpv6Csum(const uint8_t *src, const uint8_t *dest, icmp6_hdr *icmp, uint16_t plen) const
virtual bool Run()=0
uint32_t GetCmdParam() const
Definition: proto_handler.h:86
uint16_t tci
Definition: proto_handler.h:23
void IpHdr(uint16_t len, in_addr_t src, in_addr_t dest, uint8_t protocol, uint16_t id, uint8_t ttl)