OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
services_init.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <oper/mirror_table.h>
6 #include <io/event_manager.h>
7 #include <cmn/agent_cmn.h>
8 #include "sandesh/sandesh_types.h"
9 #include "sandesh/sandesh.h"
10 #include "sandesh/sandesh_trace.h"
11 #include "services/services_init.h"
12 #include "services/dhcp_proto.h"
13 #include "services/dhcpv6_proto.h"
14 #include "services/dns_proto.h"
15 #include "services/arp_proto.h"
16 #include "services/icmp_proto.h"
17 #include "services/icmpv6_proto.h"
18 #include "services/bfd_proto.h"
19 #include "services/igmp_proto.h"
21 #include "init/agent_param.h"
22 
23 
31 
32 ServicesModule::ServicesModule(Agent *agent, const std::string &metadata_secret)
33  : agent_(agent), metadata_secret_key_(metadata_secret), dhcp_proto_(NULL),
34  dhcpv6_proto_(NULL), dns_proto_(NULL), arp_proto_(NULL), bfd_proto_(NULL),
35  icmp_proto_(NULL), icmpv6_proto_(NULL), igmp_proto_(NULL), metadata_proxy_(NULL) {
36 }
37 
39 }
40 
41 void ServicesModule::Init(bool run_with_vrouter) {
42  EventManager *event = agent_->event_manager();
43  boost::asio::io_context &io = *event->io_service();
44 
45  dhcp_proto_.reset(new DhcpProto(agent_, io, run_with_vrouter));
47 
48  dhcpv6_proto_.reset(new Dhcpv6Proto(agent_, io, run_with_vrouter));
50 
51  dns_proto_.reset(new DnsProto(agent_, io));
53 
54  arp_proto_.reset(new ArpProto(agent_, io, run_with_vrouter));
56 
57  bfd_proto_.reset(new BfdProto(agent_, io));
59 
60  icmp_proto_.reset(new IcmpProto(agent_, io));
62 
63  icmpv6_proto_.reset(new Icmpv6Proto(agent_, io));
65 
66  icmp_error_proto_.reset(new IcmpErrorProto(agent_, io));
68 
69  igmp_proto_.reset(new IgmpProto(agent_, io));
71 
74 }
75 
77  dns_proto_->ConfigInit();
78 }
79 
81  dns_proto_->IoShutdown();
82  metadata_proxy_->CloseSessions();
83 }
84 
86  dhcp_proto_->Shutdown();
87  dhcp_proto_.reset(NULL);
88  agent_->SetDhcpProto(NULL);
89 
90  dhcpv6_proto_->Shutdown();
91  dhcpv6_proto_.reset(NULL);
92  agent_->set_dhcpv6_proto(NULL);
93 
94  dns_proto_->Shutdown();
95  dns_proto_.reset(NULL);
96  agent_->SetDnsProto(NULL);
97 
98  arp_proto_->Shutdown();
99  arp_proto_.reset(NULL);
100  agent_->SetArpProto(NULL);
101 
102  bfd_proto_->Shutdown();
103  bfd_proto_.reset(NULL);
104  agent_->SetBfdProto(NULL);
105 
106  icmp_proto_->Shutdown();
107  icmp_proto_.reset(NULL);
108  agent_->SetIcmpProto(NULL);
109 
110  icmpv6_proto_->Shutdown();
111  icmpv6_proto_.reset(NULL);
112  agent_->set_icmpv6_proto(NULL);
113 
114  igmp_proto_->Shutdown();
115  igmp_proto_.reset(NULL);
116  agent_->SetIgmpProto(NULL);
117 
118  metadata_proxy_->Shutdown();
119  metadata_proxy_.reset(NULL);
120 }
121 
122 bool ServicesModule::AllocateFd(uint16_t port_number, uint8_t l3_proto) {
123  int fd;
124 
125  // l3 proto can be TCP or UDP
126  if (l3_proto == IPPROTO_TCP) {
127  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
128  } else {
129  fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
130  }
131 
132  if (fd < 0) {
133  LOG(ERROR, "Failed to create socket, errno:" << strerror(errno));
134  return false;
135  }
136 
137  struct sockaddr_in address;
138  memset(&address, 0, sizeof(address));
139  address.sin_family = AF_INET;
140  address.sin_port = htons(port_number);
141  if (::bind(fd, (struct sockaddr*) &address, sizeof(address)) < 0) {
142  LOG(ERROR, "failed to bind socket to port: " << port_number
143  << "errno: " << strerror(errno));
144  close(fd);
145  return false;
146  }
147 
148  reserved_port_fd_list_.push_back(fd);
149 
150  return true;
151 }
153 
154  //reserve UDP dest ports used for tunneling protocos
155  //so that these port numbers are not used for link local services.
158  AllocateFd(VXLAN_UDP_DEST_PORT, IPPROTO_UDP);
159 }
160 
162  std::vector<int>::const_iterator it = reserved_port_fd_list_.begin();
163  while ( it != reserved_port_fd_list_.end()) {
164  // close socket
165  close(*it);
166  it++;
167  }
168 }
SandeshTraceBufferPtr Dhcpv6TraceBuf
void set_icmpv6_proto(Icmpv6Proto *proto)
Definition: agent.h:997
SandeshTraceBufferPtr ArpTraceBuf
SandeshTraceBufferPtr BfdTraceBuf
void SetDnsProto(DnsProto *proto)
Definition: agent.h:988
void SetBfdProto(BfdProto *proto)
Definition: agent.h:991
boost::scoped_ptr< Icmpv6ErrorProto > icmpv6_error_proto_
Definition: services_init.h:60
boost::scoped_ptr< IgmpProto > igmp_proto_
Definition: services_init.h:61
boost::asio::io_context * io_service()
Definition: event_manager.h:42
#define MPLS_OVER_UDP_OLD_DEST_PORT
Definition: services_init.h:14
std::vector< int > reserved_port_fd_list_
Definition: services_init.h:63
SandeshTraceBufferPtr IgmpTraceBuf
boost::scoped_ptr< DnsProto > dns_proto_
Definition: services_init.h:54
void ReserveLocalPorts()
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:18
void set_dhcpv6_proto(Dhcpv6Proto *proto)
Definition: agent.h:985
boost::scoped_ptr< DhcpProto > dhcp_proto_
Definition: services_init.h:52
ServicesModule(Agent *agent, const std::string &metadata_secret)
boost::scoped_ptr< Dhcpv6Proto > dhcpv6_proto_
Definition: services_init.h:53
SandeshTraceBufferPtr DhcpTraceBuf
void SetDhcpProto(DhcpProto *proto)
Definition: agent.h:982
boost::scoped_ptr< IcmpErrorProto > icmp_error_proto_
Definition: services_init.h:59
void FreeLocalPortBindings()
#define VXLAN_UDP_DEST_PORT
Definition: pkt_handler.h:36
boost::scoped_ptr< MetadataProxy > metadata_proxy_
Definition: services_init.h:62
Definition: agent.h:358
EventManager * event_manager() const
Definition: agent.h:1103
void SetIgmpProto(IgmpProto *proto)
Definition: agent.h:1003
void SetIcmpProto(IcmpProto *proto)
Definition: agent.h:994
#define MPLS_OVER_UDP_NEW_DEST_PORT
Definition: services_init.h:15
boost::scoped_ptr< Icmpv6Proto > icmpv6_proto_
Definition: services_init.h:58
void Init(bool run_with_vrouter)
std::string metadata_secret_key_
Definition: services_init.h:51
#define LOG(_Level, _Msg)
Definition: logging.h:33
boost::scoped_ptr< BfdProto > bfd_proto_
Definition: services_init.h:56
boost::scoped_ptr< IcmpProto > icmp_proto_
Definition: services_init.h:57
bool AllocateFd(uint16_t port_number, uint8_t ip_proto)
SandeshTraceBufferPtr MetadataTraceBuf
void SetArpProto(ArpProto *proto)
Definition: agent.h:979
SandeshTraceBufferPtr Icmpv6TraceBuf
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
Definition: sandesh_trace.h:46
boost::scoped_ptr< ArpProto > arp_proto_
Definition: services_init.h:55