aboutsummaryrefslogtreecommitdiffstats
path: root/src/vat/ip_types.c
blob: abcd2eaf648f7e076a03961e993ae50eec7a53ac (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
## Creating VPP Startup Configuration
This document describes how to create the VPP startup configuration
file located at `/etc/vpp/contiv-vswitch.conf`.

### Hardware Interface Configuration
#### Single-NIC Configuration
You need to configure hardware interfaces for use by VPP. First, find out the PCI address of the host's network interface. On 
Debian-based distributions, you can use `lshw`:

```
sudo lshw -class network -businfo
Bus info          Device      Class          Description
========================================================
pci@0000:03:00.0  ens160      network        VMXNET3 Ethernet Controller
```

In our case, it would be the `ens3` interface with the PCI address
`0000:00:03.0`

Now, add or modify the VPP startup config file (`/etc/vpp/contiv-vswitch.conf`)
to contain the proper PCI address:
```
unix {
    nodaemon
    cli-listen /run/vpp/cli.sock
    cli-no-pager
    coredump-size unlimited
    full-coredump
    poll-sleep-usec 100
}
nat {
    endpoint-dependent
}
dpdk {
    dev 0000:00:03.0
}
api-trace {
   on
   nitems 500
}
```
#### Multi-NIC Configuration
Similar to the single-NIC configuration, use command *lshw* to find the PCI
addresses of all the NICs in the system, for example:

```
$ sudo lshw -class network -businfo
Bus info          Device      Class      Description
====================================================
pci@0000:00:03.0  ens3        network    Virtio network device
pci@0000:00:04.0  ens4        network    Virtio network device
```

In the example above, `ens3` would be the primary interface and `ens4` would
be the interface that would be used by VPP. The PCI address of the `ens4`
interface would be `0000:00:04.0`.

Make sure the selected interface is *shut down*, otherwise VPP
will not grab it:
```
sudo ip link set ens4 down
```

Now, add or modify the VPP startup config file in `/etc/vpp/contiv-vswitch.conf`
to contain the proper PCI address:
```
unix {
    nodaemon
    cli-listen /run/vpp/cli.sock
    cli-no-pager
    coredump-size unlimited
    full-coredump
    poll-sleep-usec 100
}
nat {
    endpoint-dependent
}
dpdk {
    dev 0000:00:04.0
}
api-trace {
   on
   nitems 500
}
```
If assigning multiple NICs to VPP you will need to include each NIC's PCI address
in the dpdk stanza in `/etc/vpp/contiv-vswitch.conf`.

##### Assigning all NICs to VPP
On a multi-NIC node, it is also possible to assign all NICs from the kernel for
use by VPP. First, you need to install the STN daemon, as described [here][1],
since you will want the NICs to revert to the kernel if VPP crashes.

You also need to configure the NICs in the VPP startup config file
in `/etc/vpp/contiv-vswitch.conf`. For example, to use both the primary and
secondary NIC, in a two-NIC node, your VPP startup config file would look
something like this:

```
unix {
    nodaemon
    cli-listen /run/vpp/cli.sock
    cli-no-pager
    coredump-size unlimited
    full-coredump
    poll-sleep-usec 100
}
nat {
    endpoint-dependent
}
dpdk {
    dev 0000:00:03.0
    dev 0000:00:04.0
}
api-trace {
   on
   nitems 500
}
```

#### Installing `lshw` on CentOS/RedHat/Fedora
Note: On CentOS/RedHat/Fedora distributions, `lshw` may not be available
by default, install it by
```
sudo yum -y install lshw
```

### Power-saving Mode
In regular operation, VPP takes 100% of one CPU core at all times (poll loop).
If high performance and low latency is not required you can "slow-down"
the poll-loop and drastically reduce CPU utilization by adding the following 
stanza to the `unix` section of the VPP startup config file:
```
unix {
    ...
    poll-sleep-usec 100
    ...
}
```
The power-saving mode is especially useful in VM-based development environments 
running on laptops or less powerful servers. 

### VPP API Trace
To troubleshoot VPP configuration issues in production environments, it is 
strongly recommended to configure VPP API trace. This is done by adding the
following stanza to the VPP startup config file:
```
api-trace {
    on
    nitems 500
}
```
You can set the size of the trace buffer with the <nitems> attribute. 
='#n481'>481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
/*
 * Copyright (c) 2016 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 <vnet/ip/ip_types.h>
#include <vnet/ip/format.h>
#include <vnet/ip/ip.h>

u8 *
format_ip_address (u8 * s, va_list * args)
{
  ip_address_t *a = va_arg (*args, ip_address_t *);
  u8 ver = ip_addr_version (a);
  if (ver == AF_IP4)
    {
      return format (s, "%U", format_ip4_address, &ip_addr_v4 (a));
    }
  else if (ver == AF_IP6)
    {
      return format (s, "%U", format_ip6_address, &ip_addr_v6 (a));
    }
  else
    {
      clib_warning ("Can't format IP version %d!", ver);
      return 0;
    }
}

uword
unformat_ip_address (unformat_input_t * input, va_list * args)
{
  ip_address_t *a = va_arg (*args, ip_address_t *);
  ip_address_t tmp, *p_tmp = &tmp;

  clib_memset (p_tmp, 0, sizeof (*p_tmp));
  if (unformat (input, "%U", unformat_ip4_address, &ip_addr_v4 (p_tmp)))
    ip_addr_version (p_tmp) = AF_IP4;
  else if (unformat_user (input, unformat_ip6_address, &ip_addr_v6 (p_tmp)))
    ip_addr_version (p_tmp) = AF_IP6;
  else
    return 0;
  *a = *p_tmp;
  return 1;
}

u8 *
format_ip_prefix (u8 * s, va_list * args)
{
  ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
  return format (s, "%U/%d", format_ip_address, &ip_prefix_addr (a),
		 ip_prefix_len (a));
}

uword
unformat_ip_prefix (unformat_input_t * input, va_list * args)
{
  ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
  /* %d writes more than a u8 */
  int plen;
  if (unformat (input, "%U/%d", unformat_ip_address, &ip_prefix_addr (a),
		&plen))
    {
      ip_prefix_len (a) = plen;
      if ((ip_prefix_version (a) == AF_IP4 && 32 < ip_prefix_len (a)) ||
	  (ip_prefix_version (a) == AF_IP6 && 128 < ip_prefix_len (a)))
	{
	  clib_warning ("Prefix length to big: %d!", ip_prefix_len (a));
	  return 0;
	}
      ip_prefix_normalize (a);
    }
  else
    return 0;
  return 1;
}

u16
ip_address_size (const ip_address_t * a)
{
  switch (ip_addr_version (a))
    {
    case AF_IP4:
      return sizeof (ip4_address_t);
      break;
    case AF_IP6:
      return sizeof (ip6_address_t);
      break;
    }
  return 0;
}

bool
ip_address_is_zero (const ip_address_t * ip)
{
  switch (ip_addr_version (ip))
    {
    case AF_IP4:
      return (ip_addr_v4 (ip).as_u32 == 0);
    case AF_IP6:
      return (ip_addr_v6 (ip).as_u64[0] == 0 &&
	      ip_addr_v6 (ip).as_u64[1] == 0);
      break;
    }
  return false;
}

int
ip_address_cmp (const ip_address_t * ip1, const ip_address_t * ip2)
{
  int res = 0;
  if (ip_addr_version (ip1) != ip_addr_version (ip2))
    return -1;
  res = ip46_address_cmp (&ip_addr_46 (ip1), &ip_addr_46 (ip2));

  if (res < 0)
    res = 2;
  else if (res > 0)
    res = 1;

  return res;
}

void
ip_address_copy (ip_address_t * dst, const ip_address_t * src)
{
  if (AF_IP4 == ip_addr_version (src))
    {
      /* don't copy any garbage from the union */
      clib_memset (dst, 0, sizeof (*dst));
      ip_addr_v4 (dst) = ip_addr_v4 (src);
      dst->version = AF_IP4;
    }
  else
    {
      clib_memcpy (dst, src, sizeof (ip_address_t));
    }
}

u8 *
ip_addr_bytes (ip_address_t * ip)
{
  switch (ip->version)
    {
    case AF_IP4:
      return (u8 *) & ip_addr_v4 (ip);
    case AF_IP6:
      return (u8 *) & ip_addr_v6 (ip);
      break;
    }
  ASSERT (0);
  return (NULL);
}

void
ip_address_copy_addr (void *dst, const ip_address_t * src)
{
  switch (src->version)
    {
    case AF_IP4:
      clib_memcpy (dst, &ip_addr_v4 (src), ip_address_size (src));
      break;
    case AF_IP6:
      clib_memcpy (dst, &ip_addr_v6 (src), ip_address_size (src));
      break;
    }
}

u16
ip_version_to_size (ip_address_family_t af)
{
  switch (af)
    {
    case AF_IP4:
      return sizeof (ip4_address_t);
      break;
    case AF_IP6:
      return sizeof (ip6_address_t);
      break;
    }
  return 0;
}

vnet_link_t
ip_address_family_to_link_type (ip_address_family_t af)
{
  switch (af)
    {
    case AF_IP4:
      return (VNET_LINK_IP4);
    case AF_IP6:
      return (VNET_LINK_IP6);
    }
  ASSERT (0);
  return (VNET_LINK_IP4);
}


void
ip_address_set (ip_address_t * dst, const void *src, u8 version)
{
  ip_addr_version (dst) = version;

  switch (version)
    {
    case AF_IP4:
      ip_addr_v4 (dst) = *(ip4_address_t *) src;
      break;
    case AF_IP6:
      ip_addr_v6 (dst) = *(ip6_address_t *) src;
      break;
    }
}

fib_protocol_t
ip_address_family_to_fib_proto (ip_address_family_t af)
{
  switch (af)
    {
    case AF_IP4:
      return (FIB_PROTOCOL_IP4);
    case AF_IP6:
      return (FIB_PROTOCOL_IP6);
    }
  ASSERT (0);
  return (FIB_PROTOCOL_IP4);
}

ip_address_family_t
ip_address_family_from_fib_proto (fib_protocol_t fp)
{
  switch (fp)
    {
    case FIB_PROTOCOL_IP4:
      return (AF_IP4);
    case FIB_PROTOCOL_IP6:
      return (AF_IP6);
    case FIB_PROTOCOL_MPLS:
      ASSERT (0);
    }
  return (AF_IP4);
}

fib_protocol_t
ip_address_to_46 (const ip_address_t * addr, ip46_address_t * a)
{
  *a = ip_addr_46 (addr);
  return (ip_address_family_to_fib_proto (ip_addr_version (addr)));
}

void
ip_address_from_46 (const ip46_address_t * nh,
		    fib_protocol_t fproto, ip_address_t * ip)
{
  ip_addr_46 (ip) = *nh;
  ip_addr_version (ip) = ip_address_family_from_fib_proto (fproto);
}

static void
ip_prefix_normalize_ip4 (ip4_address_t * ip4, u8 preflen)
{
  u32 mask = ~0;

  ASSERT (ip4);

  if (32 <= preflen)
    {
      return;
    }

  mask = pow2_mask (preflen) << (32 - preflen);
  mask = clib_host_to_net_u32 (mask);
  ip4->data_u32 &= mask;
}

static void
ip_prefix_normalize_ip6 (ip6_address_t * ip6, u8 preflen)
{
  u8 mask_6[16];
  u32 *m;
  u8 j, i0, i1;

  ASSERT (ip6);

  clib_memset (mask_6, 0, sizeof (mask_6));

  if (128 <= preflen)
    {
      return;
    }

  i1 = preflen % 32;
  i0 = preflen / 32;
  m = (u32 *) & mask_6[0];

  for (j = 0; j < i0; j++)
    {
      m[j] = ~0;
    }

  if (i1)
    {
      m[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
    }

  for (j = 0; j < sizeof (mask_6); j++)
    {
      ip6->as_u8[j] &= mask_6[j];
    }
}

void
ip_prefix_normalize (ip_prefix_t * a)
{
  u8 preflen = ip_prefix_len (a);

  switch (ip_prefix_version (a))
    {
    case AF_IP4:
      ip_prefix_normalize_ip4 (&ip_prefix_v4 (a), preflen);
      break;

    case AF_IP6:
      ip_prefix_normalize_ip6 (&ip_prefix_v6 (a), preflen);
      break;

    default:
      ASSERT (0);
    }
}

void
ip_prefix_copy (void *dst, void *src)
{
  clib_memcpy (dst, src, sizeof (ip_prefix_t));
}

int
ip_prefix_cmp (const ip_prefix_t *ipp1, const ip_prefix_t *ipp2)
{
  ip_prefix_t p1 = *ipp1, p2 = *ipp2;
  int cmp = 0;

  ip_prefix_normalize (&p1);
  ip_prefix_normalize (&p2);

  cmp = ip_address_cmp (&ip_prefix_addr (&p1), &ip_prefix_addr (&p2));
  if (cmp == 0)
    {
      if (ip_prefix_len (&p1) < ip_prefix_len (&p2))
	{
	  cmp = 1;
	}
      else
	{
	  if (ip_prefix_len (&p1) > ip_prefix_len (&p2))
	    cmp = 2;
	}
    }
  return cmp;
}

static bool
ip4_prefix_validate (const ip_prefix_t * ip)
{
  ip4_address_t ip4_addr, ip4_mask;

  if (ip_prefix_len (ip) > 32)
    return (false);

  ip4_addr = ip_prefix_v4 (ip);
  ip4_preflen_to_mask (ip_prefix_len (ip), &ip4_mask);

  return ((ip4_addr.as_u32 & ip4_mask.as_u32) == ip4_addr.as_u32);
}

static bool
ip6_prefix_validate (const ip_prefix_t * ip)
{
  ip6_address_t ip6_addr, ip6_mask;

  if (ip_prefix_len (ip) > 128)
    return (false);

  ip6_addr = ip_prefix_v6 (ip);
  ip6_preflen_to_mask (ip_prefix_len (ip), &ip6_mask);

  return (((ip6_addr.as_u64[0] & ip6_mask.as_u64[0]) == ip6_addr.as_u64[0]) &&
	  ((ip6_addr.as_u64[1] & ip6_mask.as_u64[1]) == ip6_addr.as_u64[1]));
}

bool
ip_prefix_validate (const ip_prefix_t * ip)
{
  switch (ip_prefix_version (ip))
    {
    case AF_IP4:
      return (ip4_prefix_validate (ip));
    case AF_IP6:
      return (ip6_prefix_validate (ip));
    }
  ASSERT (0);
  return (false);
}

void
ip4_address_normalize (ip4_address_t * ip4, u8 preflen)
{
  ASSERT (preflen <= 32);
  if (preflen == 0)
    ip4->data_u32 = 0;
  else
    ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
}

void
ip6_address_normalize (ip6_address_t * ip6, u8 preflen)
{
  ASSERT (preflen <= 128);
  if (preflen == 0)
    {
      ip6->as_u64[0] = 0;
      ip6->as_u64[1] = 0;
    }
  else if (preflen <= 64)
    {
      ip6->as_u64[0] &=
	clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
      ip6->as_u64[1] = 0;
    }
  else
    ip6->as_u64[1] &=
      clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
}

void
ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip)
{
  if (pref_len == 0)
    ip->as_u32 = 0;
  else
    ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
}

u32
ip4_mask_to_preflen (ip4_address_t * mask)
{
  if (mask->as_u32 == 0)
    return 0;
  return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
}

void
ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
				   ip4_address_t * res)
{
  u32 not_mask;
  not_mask = (1 << (32 - plen)) - 1;
  res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
}

void
ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask)
{
  if (pref_len == 0)
    {
      mask->as_u64[0] = 0;
      mask->as_u64[1] = 0;
    }
  else if (pref_len <= 64)
    {
      mask->as_u64[0] =
	clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
      mask->as_u64[1] = 0;
    }
  else
    {
      mask->as_u64[0] = 0xffffffffffffffffL;
      mask->as_u64[1] =
	clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
    }
}

void
ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
				   ip6_address_t * res)
{
  u64 not_mask;
  if (plen == 0)
    {
      res->as_u64[0] = 0xffffffffffffffffL;
      res->as_u64[1] = 0xffffffffffffffffL;
    }
  else if (plen <= 64)
    {
      not_mask = ((u64) 1 << (64 - plen)) - 1;
      res->as_u64[0] = clib_net_to_host_u64 (ip->as_u64[0]) + not_mask;
      res->as_u64[1] = 0xffffffffffffffffL;
    }
  else
    {
      not_mask = ((u64) 1 << (128 - plen)) - 1;
      res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
    }
}

u32
ip6_mask_to_preflen (ip6_address_t * mask)
{
  if (mask->as_u64[1] != 0)
    return 128 - log2_first_set (clib_net_to_host_u64 (mask->as_u64[1]));
  if (mask->as_u64[0] != 0)
    return 64 - log2_first_set (clib_net_to_host_u64 (mask->as_u64[0]));
  return 0;
}

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