aboutsummaryrefslogtreecommitdiffstats
path: root/fdio.infra.terraform/1n_nmd/vpp_device
AgeCommit message (Expand)AuthorFilesLines
2021-11-11feat(Terraform): Minio S3 gateway proxypmikus1-2/+2
2021-09-07Terraform: Cleanuppmikus2-5/+5
2021-08-27Infra: Minor terraform cleanuppmikus1-1/+1
2021-05-17Infra: Additional changespmikus1-0/+13
2021-03-11Infra: Switch csit-shim to fdiotoolspmikus3-0/+233
bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 * Copyright 2016 Intel Corporation
 *
 * 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 "tap_inject.h"

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/if_tun.h>
#include <netinet/in.h>
#include <vnet/unix/tuntap.h>

#include <vlib/unix/unix.h>


static clib_error_t *
tap_inject_tap_read (clib_file_t * f)
{
  vlib_main_t * vm = vlib_get_main ();
  tap_inject_main_t * im = tap_inject_get_main ();

  vec_add1 (im->rx_file_descriptors, f->file_descriptor);

  vlib_node_set_interrupt_pending (vm, im->rx_node_index);

  return 0;
}

#define TAP_INJECT_TAP_BASE_NAME "vpp"

clib_error_t *
tap_inject_tap_connect (vnet_hw_interface_t * hw)
{
  vnet_main_t * vnet_main = vnet_get_main ();
  vnet_sw_interface_t * sw = vnet_get_sw_interface (vnet_main, hw->hw_if_index);
  static const int one = 1;
  int fd;
  struct ifreq ifr;
  clib_file_t template;
  u32 tap_fd;
  u8 * name;

  memset (&ifr, 0, sizeof (ifr));
  memset (&template, 0, sizeof (template));

  ASSERT (hw->hw_if_index == sw->sw_if_index);

  /* Create the tap. */
  tap_fd = open ("/dev/net/tun", O_RDWR);

  if ((int)tap_fd < 0)
    return clib_error_return (0, "failed to open tun device");

  name = format (0, TAP_INJECT_TAP_BASE_NAME "%u%c", hw->hw_instance, 0);

  strncpy (ifr.ifr_name, (char *) name, sizeof (ifr.ifr_name) - 1);
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;

  if (ioctl (tap_fd, TUNSETIFF, (void *)&ifr) < 0)
    {
      close (tap_fd);
      return clib_error_return (0, "failed to create tap");
    }

  if (ioctl (tap_fd, FIONBIO, &one) < 0)
    {
      close (tap_fd);
      return clib_error_return (0, "failed to set tap to non-blocking io");
    }

  /* Open a socket to configure the device. */
  fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));

  if (fd < 0)
    {
      close (tap_fd);
      return clib_error_return (0, "failed to configure tap");
    }

  if (hw->hw_address)
    clib_memcpy (ifr.ifr_hwaddr.sa_data, hw->hw_address, ETHER_ADDR_LEN);

  ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;

  /* Set the hardware address. */
  if (ioctl (fd, SIOCSIFHWADDR, &ifr) < 0)
    {
      close (tap_fd);
      close (fd);
      return clib_error_return (0, "failed to set tap hardware address");
    }

  /* Get the tap if index. */
  if (ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
    {
      close (tap_fd);
      close (fd);
      return clib_error_return (0, "failed to procure tap if index");
    }

  close (fd);

  /* Get notified when the tap needs to be read. */
  template.read_function = tap_inject_tap_read;
  template.file_descriptor = tap_fd;

  clib_file_add (&file_main, &template);

  tap_inject_insert_tap (sw->sw_if_index, tap_fd, ifr.ifr_ifindex);

  return 0;
}

clib_error_t *
tap_inject_tap_disconnect (u32 sw_if_index)
{
  u32 tap_fd;

  tap_fd = tap_inject_lookup_tap_fd (sw_if_index);
  if (tap_fd == ~0)
    return clib_error_return (0, "failed to disconnect tap");

  tap_inject_delete_tap (sw_if_index);

  close (tap_fd);
  return 0;
}


u8 *
format_tap_inject_tap_name (u8 * s, va_list * args)
{
  int fd;
  struct ifreq ifr;

  fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));

  if (fd < 0)
    return 0;

  memset (&ifr, 0, sizeof (ifr));

  ifr.ifr_ifindex = va_arg (*args, u32);

  if (ioctl (fd, SIOCGIFNAME, &ifr) < 0)
    {
      close (fd);
      return 0;
    }

  close (fd);

  return format (s, "%s", ifr.ifr_name);
}