10 #include <sys/socket.h>
13 #include <sys/sockio.h>
15 #include <net/ethernet.h>
19 #include "sandesh/sandesh_trace.h"
20 #include "pkt/pkt_types.h"
22 #include "../pkt0_interface.h"
24 #define TAP_TRACE(obj, ...) \
26 Tap##obj::TraceMsg(PacketTraceBuf, __FILE__, __LINE__, __VA_ARGS__); \
32 struct ifaddrs *ifaddrs, *ifa;
34 if (getifaddrs(&ifaddrs) < 0)
37 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
38 if (ifa->ifa_addr && strcmp(ifa->ifa_name, if_name.c_str()) == 0) {
53 int socket_fd = socket(AF_LOCAL, SOCK_DGRAM, 0);
55 LOG(ERROR,
"Error creating socket for a TAP device, errno: " <<
56 errno <<
": " << strerror(errno));
66 memset(&ifr, 0,
sizeof(ifr));
67 strncpy(ifr.ifr_name,
name_.c_str(), IF_NAMESIZE);
68 if (ioctl(socket_fd, SIOCIFDESTROY, &ifr) < 0) {
69 LOG(ERROR,
"Error destroying the existing " <<
name_ <<
70 " device, errno: " << errno <<
": " << strerror(errno));
77 memset(&ifr, 0,
sizeof(ifr));
78 strncpy(ifr.ifr_name,
"tap", IF_NAMESIZE);
79 if (ioctl(socket_fd, SIOCIFCREATE2, &ifr) < 0) {
80 LOG(ERROR,
"Error creating the TAP device, errno: " << errno <<
81 ": " << strerror(errno));
86 std::string dev_name = std::string(
"/dev/") +
87 std::string(ifr.ifr_name);
88 ifr.ifr_data = (caddr_t)
name_.c_str();
89 if (ioctl(socket_fd, SIOCSIFNAME, &ifr) < 0) {
90 LOG(ERROR,
"Can not change interface name to " <<
name_ <<
91 "with error: " << errno <<
": " << strerror(errno));
96 memset(&ifr, 0,
sizeof(ifr));
97 strncpy(ifr.ifr_name,
name_.c_str(), IF_NAMESIZE);
98 if (ioctl(socket_fd, SIOCGIFADDR, &ifr)< 0) {
99 LOG(ERROR,
"Can not get mac address of the TAP device, errno: "
100 << errno <<
": " << strerror(errno));
103 memcpy(
mac_address_, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
107 socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
109 LOG(ERROR,
"Can not open socket for " <<
name_ <<
", errno: " <<
110 errno <<
": " << strerror(errno));
113 memset(&ifr, 0,
sizeof(ifr));
114 strncpy(ifr.ifr_name,
name_.c_str(), IF_NAMESIZE);
115 if (ioctl(socket_fd, SIOCGIFFLAGS, &ifr) < 0) {
116 LOG(ERROR,
"Can not get socket flags, errno: " << errno <<
": " <<
121 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
122 flags |= (IFF_UP|IFF_PPROMISC);
123 ifr.ifr_flags = flags & 0xffff;
124 ifr.ifr_flagshigh = flags >> 16;
126 if (ioctl(socket_fd, SIOCSIFFLAGS, &ifr) < 0) {
127 LOG(ERROR,
"Can not set socket flags, errno: " << errno <<
": " <<
134 tap_fd_ = open(dev_name.c_str(), O_RDWR);
136 LOG(ERROR,
"Can not open the device " << dev_name <<
" errno: "
137 << errno <<
": " << strerror(errno));
143 if (fcntl(
tap_fd_, F_SETFD, FD_CLOEXEC) < 0) {
144 LOG(ERROR,
"Packet Tap Error <" << errno <<
": " <<
145 strerror(errno) <<
"> setting fcntl on " <<
name_ );
149 boost::system::error_code ec;
159 input_.async_write_some(buff_list,
161 boost::asio::placeholders::error,
162 boost::asio::placeholders::bytes_transferred, buff));
boost::shared_ptr< PacketBuffer > PacketBufferPtr
void WriteHandler(const boost::system::error_code &error, std::size_t length, uint8_t *buff)
unsigned char mac_address_[ETHER_ADDR_LEN]
static bool InterfaceExists(std::string if_name)
PktHandler * pkt_handler() const
virtual void InitControlInterface()
boost::asio::posix::stream_descriptor input_
std::vector< boost::asio::const_buffer > buffer_list
void SendImpl(uint8_t *buff, uint16_t buff_len, const PacketBufferPtr &pkt, buffer_list &buff_list)
#define LOG(_Level, _Msg)
void set_pkt_interface_name(const std::string &name)
virtual void InitControlInterface()