summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/flowhash_template.h
AgeCommit message (Expand)AuthorFilesLines
2018-05-28Fix flowhash size computation for very large hash tablesPierre Pfister1-1/+1
2018-02-01Add flowhash hash table to vppinfraPierre Pfister1-0/+597
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/*
 *------------------------------------------------------------------
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <net/if.h>

#include <linux/netlink.h>
#include <linux/rtnetlink.h>

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/devices/netlink.h>

typedef struct
{
  u8 *data;
} vnet_netlink_msg_t;

static void
vnet_netlink_msg_init (vnet_netlink_msg_t * m, u16 type, u16 flags,
		       void *msg_data, int msg_len)
{
  struct nlmsghdr *nh;
  u8 *p;
  clib_memset (m, 0, sizeof (vnet_netlink_msg_t));
  vec_add2 (m->data, p, NLMSG_SPACE (msg_len));
  ASSERT (m->data == p);

  nh = (struct nlmsghdr *) p;
  nh->nlmsg_flags = flags | NLM_F_ACK;
  nh->nlmsg_type = type;
  clib_memcpy (m->data + sizeof (struct nlmsghdr), msg_data, msg_len);
}

static void
vnet_netlink_msg_add_rtattr (vnet_netlink_msg_t * m, u16 rta_type,
			     void *rta_data, int rta_data_len)
{
  struct rtattr *rta;
  u8 *p;

  vec_add2 (m->data, p, RTA_SPACE (rta_data_len));
  rta = (struct rtattr *) p;
  rta->rta_type = rta_type;
  rta->rta_len = RTA_LENGTH (rta_data_len);
  clib_memcpy (RTA_DATA (rta), rta_data, rta_data_len);
}

static clib_error_t *
vnet_netlink_msg_send (vnet_netlink_msg_t * m)
{
  clib_error_t *err = 0;
  struct sockaddr_nl ra = { 0 };
  int len, sock;
  struct nlmsghdr *nh = (struct nlmsghdr *) m->data;
  nh->nlmsg_len = vec_len (m->data);
  char buf[4096];

  if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
    return clib_error_return_unix (0, "socket(AF_NETLINK)");

  ra.nl_family = AF_NETLINK;
  ra.nl_pid = 0;

  if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
    {
      err = clib_error_return_unix (0, "bind");
      goto done;
    }

  if ((send (sock, m->data, vec_len (m->data), 0)) == -1)
    err = clib_error_return_unix (0, "send");

  if ((len = recv (sock, buf, sizeof (buf), 0)) == -1)
    err = clib_error_return_unix (0, "recv");

  for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
       nh = NLMSG_NEXT (nh, len))
    {
      if (nh->nlmsg_type == NLMSG_DONE)
	goto done;

      if (nh->nlmsg_type == NLMSG_ERROR)
	{
	  struct nlmsgerr *e = (struct nlmsgerr *) NLMSG_DATA (nh);
	  if (e->error)
	    err = clib_error_return (0, "netlink error %d", e->error);
	  goto done;
	}
    }

done:
  close (sock);
  vec_free (m->data);
  return err;
}

clib_error_t *
vnet_netlink_set_link_name (int ifindex, char *new_ifname)
{
  vnet_netlink_msg_t m;
  struct ifinfomsg ifmsg = { 0 };
  clib_error_t *err = 0;

  ifmsg.ifi_index = ifindex;
  vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
			 &ifmsg, sizeof (struct ifinfomsg));

  vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname,
			       strlen (new_ifname) + 1);

  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "set link name %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_set_link_netns (int ifindex, int netns_fd, char *new_ifname)
{
  vnet_netlink_msg_t m;
  struct ifinfomsg ifmsg = { 0 };
  clib_error_t *err = 0;

  ifmsg.ifi_index = ifindex;
  vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
			 &ifmsg, sizeof (struct ifinfomsg));

  vnet_netlink_msg_add_rtattr (&m, IFLA_NET_NS_FD, &netns_fd, sizeof (int));
  if (new_ifname)
    vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname,
				 strlen (new_ifname) + 1);

  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "set link netns %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_set_link_master (int ifindex, char *master_ifname)
{
  vnet_netlink_msg_t m;
  struct ifinfomsg ifmsg = { 0 };
  int i;
  clib_error_t *err = 0;

  ifmsg.ifi_index = ifindex;

  if ((i = if_nametoindex (master_ifname)) == 0)
    clib_error_return_unix (0, "unknown master interface '%s'",
			    master_ifname);

  vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
			 &ifmsg, sizeof (struct ifinfomsg));
  vnet_netlink_msg_add_rtattr (&m, IFLA_MASTER, &i, sizeof (int));
  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "set link master %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_set_link_addr (int ifindex, u8 * mac)
{
  vnet_netlink_msg_t m;
  struct ifinfomsg ifmsg = { 0 };
  clib_error_t *err = 0;

  ifmsg.ifi_index = ifindex;

  vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
			 &ifmsg, sizeof (struct ifinfomsg));
  vnet_netlink_msg_add_rtattr (&m, IFLA_ADDRESS, mac, 6);
  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "set link addr %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_set_link_state (int ifindex, int up)
{
  vnet_netlink_msg_t m;
  struct ifinfomsg ifmsg = { 0 };
  clib_error_t *err = 0;

  ifmsg.ifi_flags = ((up) ? IFF_UP : 0);
  ifmsg.ifi_change = IFF_UP;
  ifmsg.ifi_index = ifindex;

  vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
			 &ifmsg, sizeof (struct ifinfomsg));
  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "set link state %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_set_link_mtu (int ifindex, int mtu)
{
  vnet_netlink_msg_t m;
  struct ifinfomsg ifmsg = { 0 };
  clib_error_t *err = 0;

  ifmsg.ifi_index = ifindex;

  vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
			 &ifmsg, sizeof (struct ifinfomsg));
  vnet_netlink_msg_add_rtattr (&m, IFLA_MTU, &mtu, sizeof (int));
  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "set link mtu %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_add_ip4_addr (int ifindex, void *addr, int pfx_len)
{
  vnet_netlink_msg_t m;
  struct ifaddrmsg ifa = { 0 };
  clib_error_t *err = 0;

  ifa.ifa_family = AF_INET;
  ifa.ifa_prefixlen = pfx_len;
  ifa.ifa_index = ifindex;

  vnet_netlink_msg_init (&m, RTM_NEWADDR,
			 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
			 &ifa, sizeof (struct ifaddrmsg));

  vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4);
  vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4);
  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "add ip4 addr %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_add_ip6_addr (int ifindex, void *addr, int pfx_len)
{
  vnet_netlink_msg_t m;
  struct ifaddrmsg ifa = { 0 };
  clib_error_t *err = 0;

  ifa.ifa_family = AF_INET6;
  ifa.ifa_prefixlen = pfx_len;
  ifa.ifa_index = ifindex;

  vnet_netlink_msg_init (&m, RTM_NEWADDR,
			 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
			 &ifa, sizeof (struct ifaddrmsg));

  vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16);
  vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16);
  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "add ip6 addr %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_add_ip4_route (void *dst, u8 dst_len, void *gw)
{
  vnet_netlink_msg_t m;
  struct rtmsg rtm = { 0 };
  u8 dflt[4] = { 0 };
  clib_error_t *err = 0;

  rtm.rtm_family = AF_INET;
  rtm.rtm_table = RT_TABLE_MAIN;
  rtm.rtm_type = RTN_UNICAST;
  rtm.rtm_dst_len = dst_len;

  vnet_netlink_msg_init (&m, RTM_NEWROUTE,
			 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
			 &rtm, sizeof (struct rtmsg));

  vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 4);
  vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 4);
  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "add ip4 route %U", format_clib_error, err);
  return err;
}

clib_error_t *
vnet_netlink_add_ip6_route (void *dst, u8 dst_len, void *gw)
{
  vnet_netlink_msg_t m;
  struct rtmsg rtm = { 0 };
  u8 dflt[16] = { 0 };
  clib_error_t *err = 0;

  rtm.rtm_family = AF_INET6;
  rtm.rtm_table = RT_TABLE_MAIN;
  rtm.rtm_type = RTN_UNICAST;
  rtm.rtm_dst_len = dst_len;

  vnet_netlink_msg_init (&m, RTM_NEWROUTE,
			 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
			 &rtm, sizeof (struct rtmsg));

  vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 16);
  vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 16);
  err = vnet_netlink_msg_send (&m);
  if (err)
    err = clib_error_return (0, "add ip6 route %U", format_clib_error, err);
  return err;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */