10 #include <sys/ioctl.h>
11 #include <sys/socket.h>
14 #include <sys/sockio.h>
16 #include <net/ethernet.h>
20 #include "sandesh/sandesh_trace.h"
21 #include "pkt/pkt_types.h"
23 #include "../pkt0_interface.h"
25 #define TAP_TRACE(obj, ...) \
27 Tap##obj::TraceMsg(PacketTraceBuf, __FILE__, __LINE__, __VA_ARGS__); \
33 struct ifaddrs *ifaddrs, *ifa;
35 if (getifaddrs(&ifaddrs) < 0)
38 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
39 if (ifa->ifa_addr && strcmp(ifa->ifa_name, if_name.c_str()) == 0) {
54 int socket_fd = socket(AF_LOCAL, SOCK_DGRAM, 0);
56 LOG(ERROR,
"Error creating socket for a TAP device, errno: " <<
57 errno <<
": " << strerror(errno));
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));
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));
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));
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));
104 memcpy(
mac_address_, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
108 socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
110 LOG(ERROR,
"Can not open socket for " <<
name_ <<
", errno: " <<
111 errno <<
": " << strerror(errno));
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 <<
": " <<
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;
127 if (ioctl(socket_fd, SIOCSIFFLAGS, &ifr) < 0) {
128 LOG(ERROR,
"Can not set socket flags, errno: " << errno <<
": " <<
135 tap_fd_ = open(dev_name.c_str(), O_RDWR);
137 LOG(ERROR,
"Can not open the device " << dev_name <<
" errno: "
138 << errno <<
": " << strerror(errno));
144 if (fcntl(
tap_fd_, F_SETFD, FD_CLOEXEC) < 0) {
145 LOG(ERROR,
"Packet Tap Error <" << errno <<
": " <<
146 strerror(errno) <<
"> setting fcntl on " <<
name_ );
150 boost::system::error_code ec;
160 input_.async_write_some(buff_list,
162 boost::asio::placeholders::error,
163 boost::asio::placeholders::bytes_transferred, buff));
void set_pkt_interface_name(const std::string &name)
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_
virtual void InitControlInterface()
static bool InterfaceExists(std::string if_name)
#define LOG(_Level, _Msg)
boost::shared_ptr< PacketBuffer > PacketBufferPtr