summaryrefslogtreecommitdiffstats
path: root/meta-facebook/meta-wedge/recipes-wedge/oob-nic/oob-nic/src/intf.c
blob: 81834b99c5f0639a72736887b1afd55180e73f0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright 2014-present Facebook. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "intf.h"

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/rtnetlink.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <linux/fib_rules.h>

#include "openbmc/log.h"
#include "libnetlink.h"
#include "ll_map.h"

struct oob_intf_t {
  char oi_name[32];
  int oi_fd;
  int oi_ifidx;
  uint8_t oi_mac[6];
  struct rtnl_handle oi_rth;
};

#define TUN_DEVICE "/dev/net/tun"

static int oob_intf_set_mac(oob_intf *intf, const uint8_t mac[6]) {
  int rc;
  struct {
    struct nlmsghdr n;
    struct ifinfomsg ifi;
    char buf[256];
  } req;

  memset(&req, 0, sizeof(req));
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  req.n.nlmsg_type = RTM_NEWLINK;
  req.n.nlmsg_flags = NLM_F_REQUEST;
  req.ifi.ifi_family = AF_UNSPEC;
  req.ifi.ifi_index = intf->oi_ifidx;
  memcpy(intf->oi_mac, mac, sizeof(intf->oi_mac));
  addattr_l(&req.n, sizeof(req), IFLA_ADDRESS,
            intf->oi_mac, sizeof(intf->oi_mac));
  rc = rtnl_talk(&intf->oi_rth, &req.n, 0, 0, NULL);
  if (rc < 0) {
    rc = errno;
    LOG_ERR(rc, "Failed to set mac to interface %s @ index %d",
            intf->oi_name, intf->oi_ifidx);
    return -rc;
  }

  LOG_INFO("Set interface %s @ index %d mac to %x:%x:%x:%x:%x:%x",
           intf->oi_name, intf->oi_ifidx,
           intf->oi_mac[0], intf->oi_mac[1], intf->oi_mac[2],
           intf->oi_mac[3], intf->oi_mac[4], intf->oi_mac[5]);

  return 0;
}

static int oob_intf_bring_up(oob_intf *intf) {
  int rc;
  struct {
    struct nlmsghdr n;
    struct ifinfomsg ifi;
    char buf[256];
  } req;

  memset(&req, 0, sizeof(req));
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  req.n.nlmsg_type = RTM_NEWLINK;
  req.n.nlmsg_flags = NLM_F_REQUEST;
  req.ifi.ifi_family = AF_UNSPEC;
  req.ifi.ifi_change |= IFF_UP;
  req.ifi.ifi_flags |= IFF_UP;
  req.ifi.ifi_index = intf->oi_ifidx;
  rc = rtnl_talk(&intf->oi_rth, &req.n, 0, 0, NULL);
  if (rc < 0) {
    rc = errno;
    LOG_ERR(rc, "Failed to bring up interface %s @ index %d",
            intf->oi_name, intf->oi_ifidx);
    return -rc;
  }

  LOG_INFO("Brought up interface %s @ index %d", intf->oi_name, intf->oi_ifidx);

  return 0;
}

oob_intf* oob_intf_create(const char *name, const uint8_t mac[6]) {

  int rc;
  int flags;
  struct ifreq ifr;
  oob_intf *intf = NULL;

#define _CHECK_RC(fmt, ...) do {                \
  if (rc < 0) {                                 \
    rc = errno;                                 \
    LOG_ERR(rc, fmt, ##__VA_ARGS__);            \
    goto err_out;                               \
  }                                             \
} while(0)

  intf = malloc(sizeof(*intf));
  if (!intf) {
    rc = ENOMEM;
    LOG_ERR(rc, "Failed to allocate memory for interface");
    goto err_out;
  }
  memset(intf, 0, sizeof(*intf));
  strncpy(intf->oi_name, name, sizeof(intf->oi_name));
  intf->oi_name[sizeof(intf->oi_name) - 1] = '\0';
  intf->oi_fd = -1;

  rc = rtnl_open(&intf->oi_rth, 0);
  _CHECK_RC("Failed to open rth_handler");

  rc = open(TUN_DEVICE, O_RDWR);
  _CHECK_RC("Failed to open %s", TUN_DEVICE);
  intf->oi_fd = rc;

  memset(&ifr, 0, sizeof(ifr));
  /*
   * IFF_TAP: TAP interface
   * IFF_NO_PI: Do not provide pracket information
   */
  ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
  strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
  ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';

  rc = ioctl(intf->oi_fd, TUNSETIFF, (void *) &ifr);
  _CHECK_RC("Failed to create tap interface %s", ifr.ifr_name);

  /* make fd non-blocking */
  rc = fcntl(intf->oi_fd, F_GETFL);
  _CHECK_RC("Failed to get flags from fd ", intf->oi_fd);
  flags = rc | O_NONBLOCK;
  rc = fcntl(intf->oi_fd, F_SETFL, rc);
  _CHECK_RC("Failed to set non-blocking flags ", flags,
            " to fd ", intf->oi_fd);

  /* set CLOEXEC */
  rc = fcntl(intf->oi_fd, F_GETFD);
  _CHECK_RC("Failed to get flags from fd ", intf->oi_fd);
  flags = rc | FD_CLOEXEC;
  rc = fcntl(intf->oi_fd, F_SETFD, flags);
  _CHECK_RC("Failed to set close-on-exec flags ", flags,
            " to fd ", intf->oi_fd);

  // TODO: if needed, we can adjust send buffer size, TUNSETSNDBUF
  intf->oi_ifidx = ll_name_to_index(intf->oi_name);

  /* now set the mac address */
  oob_intf_set_mac(intf, mac);

#if 0
  /* make it persistent */
  rc = ioctl(intf->oi_fd, TUNSETPERSIST, 0);
  _CHECK_RC("Failed to make the tap interface %s persistent", intf->oi_name);
#endif

  LOG_INFO("Create/attach to tap interface %s @ fd %d, index %d",
           intf->oi_name, intf->oi_fd, intf->oi_ifidx);

  //oob_intf_bring_up(intf);

  return intf;

 err_out:
  if (intf) {
    rtnl_close(&intf->oi_rth);
    if (intf->oi_fd != -1) {
      close(intf->oi_fd);
    }
    free(intf);
  }

  return NULL;
}

int oob_intf_get_fd(const oob_intf *intf) {
  return intf->oi_fd;
}

int oob_intf_receive(const oob_intf *intf, char *buf, int len) {
  int rc;
  do {
    rc = read(intf->oi_fd, buf, len);
  } while (rc == -1 && errno == EINTR);
  if (rc < 0) {
    rc = errno;
    if (rc != EAGAIN) {
      LOG_ERR(rc, "Failed to read on interface fd %d", intf->oi_fd);
      return -rc;
    } else {
      /* nothing is available */
      return 0;
    }
  } else if (rc == 0) {
    // Nothing to read. It shall not happen as the fd is non-blocking.
    // Just add this case to be safe.
    return 0;
  } else if (rc > len) {
    // The pkt is larger than the buffer. We don't have complete packet.
    // It shall not happen unless the MTU is mis-match. Drop the packet.
    LOG_ERR(ENOSPC, "Received a too large packet (%d bytes > %d) from the "
            "tap interface. Drop it...", rc, len);
    return -ENOSPC;
  } else {
    LOG_VER("Recv a packet of %d bytes from %s", rc, intf->oi_name);
    return rc;
  }
}

int oob_intf_send(const oob_intf *intf, const char *buf, int len) {
  int rc;
  do {
    rc = write(intf->oi_fd, buf, len);
  } while (rc == -1 && errno == EINTR);
  if (rc < 0) {
    rc = errno;
    LOG_ERR(rc, "Failed to send on interface fd %d", intf->oi_fd);
    return -rc;
  } else if (rc < len) {
    LOG_ERR(EIO, "Failed to send the full packet (%d bytes > %d) for fd %d",
            len, rc, intf->oi_fd);
    return -EIO;
  } else {
    LOG_VER("Sent a packet of %d bytes to %s", rc, intf->oi_name);
    return rc;
  }
}
OpenPOWER on IntegriCloud