OpenSDN source code
freebsd/pkt0_interface.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 
6 #include <stdio.h>
7 #include <string.h>
8 #include <fcntl.h>
9 #include <assert.h>
10 #include <sys/ioctl.h>
11 #include <sys/socket.h>
12 
13 #include <net/if.h>
14 #include <sys/sockio.h>
15 #include <ifaddrs.h>
16 #include <net/ethernet.h>
17 
18 #include "base/logging.h"
19 #include "cmn/agent_cmn.h"
20 #include "sandesh/sandesh_trace.h"
21 #include "pkt/pkt_types.h"
22 #include "pkt/pkt_init.h"
23 #include "../pkt0_interface.h"
24 
25 #define TAP_TRACE(obj, ...) \
26 do { \
27  Tap##obj::TraceMsg(PacketTraceBuf, __FILE__, __LINE__, __VA_ARGS__); \
28 } while (false) \
29 
31 
32 static bool InterfaceExists(std::string if_name) {
33  struct ifaddrs *ifaddrs, *ifa;
34 
35  if (getifaddrs(&ifaddrs) < 0)
36  return false;
37 
38  for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
39  if (ifa->ifa_addr && strcmp(ifa->ifa_name, if_name.c_str()) == 0) {
40  freeifaddrs(ifaddrs);
41  return true;
42  }
43  }
44 
45  freeifaddrs(ifaddrs);
46  return false;
47 }
48 
50  unsigned int flags;
52 
53  // Create a socket for the TAP device
54  int socket_fd = socket(AF_LOCAL, SOCK_DGRAM, 0);
55  if (socket_fd < 0) {
56  LOG(ERROR, "Error creating socket for a TAP device, errno: " <<
57  errno << ": " << strerror(errno));
58  assert(0);
59  }
60 
61  // XXX: If interface of the name name_ exists, delete it first. It is
62  // necessary on FreeBSD as we have to create a new one to obtain path
63  // to the device file (/dev/tapX) used for receiving/sending the actual
64  // data. Should find a way to attach to the existing interface instead.
65  struct ifreq ifr;
66  if (InterfaceExists(name_)) {
67  memset(&ifr, 0, sizeof(ifr));
68  strncpy(ifr.ifr_name, name_.c_str(), IF_NAMESIZE);
69  if (ioctl(socket_fd, SIOCIFDESTROY, &ifr) < 0) {
70  LOG(ERROR, "Error destroying the existing " << name_ <<
71  " device, errno: " << errno << ": " << strerror(errno));
72  assert(0);
73  }
74  }
75 
76  // Create a device with 'tap' name as required by the FreeBSD. Later it
77  // can be renamed to pkt0 for example.
78  memset(&ifr, 0, sizeof(ifr));
79  strncpy(ifr.ifr_name, "tap", IF_NAMESIZE);
80  if (ioctl(socket_fd, SIOCIFCREATE2, &ifr) < 0) {
81  LOG(ERROR, "Error creating the TAP device, errno: " << errno <<
82  ": " << strerror(errno));
83  assert(0);
84  }
85 
86  // Set interface name and save the /dev/tapX path for later use
87  std::string dev_name = std::string("/dev/") +
88  std::string(ifr.ifr_name);
89  ifr.ifr_data = (caddr_t) name_.c_str();
90  if (ioctl(socket_fd, SIOCSIFNAME, &ifr) < 0) {
91  LOG(ERROR, "Can not change interface name to " << name_ <<
92  "with error: " << errno << ": " << strerror(errno));
93  assert(0);
94  }
95 
96  // Save mac address
97  memset(&ifr, 0, sizeof(ifr));
98  strncpy(ifr.ifr_name, name_.c_str(), IF_NAMESIZE);
99  if (ioctl(socket_fd, SIOCGIFADDR, &ifr)< 0) {
100  LOG(ERROR, "Can not get mac address of the TAP device, errno: "
101  << errno << ": " << strerror(errno));
102  assert(0);
103  }
104  memcpy(mac_address_, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
105  close(socket_fd);
106 
107  // Set the interface up
108  socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
109  if (socket_fd < 0) {
110  LOG(ERROR, "Can not open socket for " << name_ << ", errno: " <<
111  errno << ": " << strerror(errno));
112  assert(0);
113  }
114  memset(&ifr, 0, sizeof(ifr));
115  strncpy(ifr.ifr_name, name_.c_str(), IF_NAMESIZE);
116  if (ioctl(socket_fd, SIOCGIFFLAGS, &ifr) < 0) {
117  LOG(ERROR, "Can not get socket flags, errno: " << errno << ": " <<
118  strerror(errno));
119  assert(0);
120  }
121 
122  flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
123  flags |= (IFF_UP|IFF_PPROMISC);
124  ifr.ifr_flags = flags & 0xffff;
125  ifr.ifr_flagshigh = flags >> 16;
126 
127  if (ioctl(socket_fd, SIOCSIFFLAGS, &ifr) < 0) {
128  LOG(ERROR, "Can not set socket flags, errno: " << errno << ": " <<
129  strerror(errno));
130  assert(0);
131  }
132  close(socket_fd);
133 
134  // Open a device file for the tap interface
135  tap_fd_ = open(dev_name.c_str(), O_RDWR);
136  if (tap_fd_ < 0) {
137  LOG(ERROR, "Can not open the device " << dev_name << " errno: "
138  << errno << ": " << strerror(errno));
139  assert(0);
140  }
141 
142  // We dont want the fd to be inherited by child process such as
143  // virsh etc... So, close tap fd on fork.
144  if (fcntl(tap_fd_, F_SETFD, FD_CLOEXEC) < 0) {
145  LOG(ERROR, "Packet Tap Error <" << errno << ": " <<
146  strerror(errno) << "> setting fcntl on " << name_ );
147  assert(0);
148  }
149 
150  boost::system::error_code ec;
151  input_.assign(tap_fd_, ec);
152  assert(!ec);
153 
155  AsyncRead();
156 }
157 
158 void Pkt0Interface::SendImpl(uint8_t *buff, uint16_t buff_len, const PacketBufferPtr &pkt,
159  buffer_list& buff_list) {
160  input_.async_write_some(buff_list,
161  boost::bind(&Pkt0Interface::WriteHandler, this,
162  boost::asio::placeholders::error,
163  boost::asio::placeholders::bytes_transferred, buff));
164 }
void set_pkt_interface_name(const std::string &name)
Definition: agent.h:950
PktHandler * pkt_handler() const
virtual void InitControlInterface()
void WriteHandler(const boost::system::error_code &error, std::size_t length, uint8_t *buff)
unsigned char mac_address_[ETHER_ADDR_LEN]
std::vector< boost::asio::const_buffer > buffer_list
void SendImpl(uint8_t *buff, uint16_t buff_len, const PacketBufferPtr &pkt, buffer_list &buff_list)
boost::asio::posix::stream_descriptor input_
std::string name_
Agent * agent() const
Definition: pkt_handler.h:317
virtual void InitControlInterface()
static bool InterfaceExists(std::string if_name)
#define LOG(_Level, _Msg)
Definition: logging.h:35
boost::shared_ptr< PacketBuffer > PacketBufferPtr
Definition: packet_buffer.h:19