""" IP Routes object abstractions for representing IP routes in VPP """ from vpp_object import VppObject from socket import inet_pton, inet_ntop, AF_INET, AF_INET6 from vpp_ip import DpoProto, INVALID_INDEX, VppIpAddressUnion, \ VppIpMPrefix from ipaddress import ip_address, IPv4Network, IPv6Network # from vnet/vnet/mpls/mpls_types.h MPLS_IETF_MAX_LABEL = 0xfffff MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1 try: text_type = unicode except NameError: text_type = str class MRouteItfFlags: MFIB_ITF_FLAG_NONE = 0 MFIB_ITF_FLAG_NEGATE_SIGNAL = 1 MFIB_ITF_FLAG_ACCEPT = 2 MFIB_ITF_FLAG_FORWARD = 4 MFIB_ITF_FLAG_SIGNAL_PRESENT = 8 MFIB_ITF_FLAG_INTERNAL_COPY = 16 class MRouteEntryFlags: MFIB_ENTRY_FLAG_NONE = 0 MFIB_ENTRY_FLAG_SIGNAL = 1 MFIB_ENTRY_FLAG_DROP = 2 MFIB_ENTRY_FLAG_CONNECTED = 4 MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8 class FibPathProto: FIB_PATH_NH_PROTO_IP4 = 0 FIB_PATH_NH_PROTO_IP6 = 1 FIB_PATH_NH_PROTO_MPLS = 2 FIB_PATH_NH_PROTO_ETHERNET = 3 FIB_PATH_NH_PROTO_BIER = 4 FIB_PATH_NH_PROTO_NSH = 5 class FibPathType: FIB_PATH_TYPE_NORMAL = 0 FIB_PATH_TYPE_LOCAL = 1 FIB_PATH_TYPE_DROP = 2 FIB_PATH_TYPE_UDP_ENCAP = 3 FIB_PATH_TYPE_BIER_IMP = 4 FIB_PATH_TYPE_ICMP_UNREACH = 5 FIB_PATH_TYPE_ICMP_PROHIBIT = 6 FIB_PATH_TYPE_SOURCE_LOOKUP = 7 FIB_PATH_TYPE_DVR = 8 FIB_PATH_TYPE_INTERFACE_RX = 9 FIB_PATH_TYPE_CLASSIFY = 10 class FibPathFlags: FIB_PATH_FLAG_NONE = 0 FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1 FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2 FIB_PATH_FLAG_POP_PW_CW = 4 class MplsLspMode: PIPE = 0 UNIFORM = 1 def mk_network(addr, len): if ip_address(text_type(addr)).version == 4: return IPv4Network("%s/%d" % (addr, len), strict=False) else: return IPv6Network("%s/%d" % (addr, len), strict=False) def ip_to_dpo_proto(addr): if addr.version == 6: return DpoProto.DPO_PROTO_IP6 else: return DpoProto.DPO_PROTO_IP4 def address_proto(ip_addr): if ip_addr.ip_addr.version == 4: return FibPathProto.FIB_PATH_NH_PROTO_IP4 else: return FibPathProto.FIB_PATH_NH_PROTO_IP6 def find_route(test, addr, len, table_id=0): prefix = mk_network(addr, len) if 4 == prefix.version: routes = test.vapi.ip_route_dump(table_id, False) else: routes = test.vapi.ip_route_dump(table_id, True) for e in routes: if table_id == e.route.table_id \ and str(e.route.prefix) == str(prefix): return True return False def find_route_in_dump(dump, route, table): for r in dump: if table.table_id == r.route.table_id \ and route.prefix == r.route.prefix: if len(route.paths) == r.route.n_paths: return True return False def find_mroute_in_dump(dump, route, table): for r in dump: if table.table_id == r.route.table_id \ and route.prefix == r.route.prefix: return True return False def find_mroute(test, grp_addr, src_addr, grp_addr_len, table_id=0): ip_mprefix = VppIpMPrefix(text_type(src_addr), text_type(grp_addr), grp_addr_len) if 4 == ip_mprefix.version: routes = test.vapi.ip_mroute_dump(table_id, False) else: routes = test.vapi.ip_mroute_dump(table_id, True) for e in routes: if table_id == e.route.table_id and ip_mprefix == e.route.prefix: return True return False def find_mpls_route(test, table_id, label, eos_bit, paths=None): dump = test.vapi.mpls_route_dump(table_id) for e in dump: if label == e.mr_route.mr_label \ and eos_bit == e.mr_route.mr_eos \ and table_id == e.mr_route.mr_table_id: if not paths: return True else: if (len(paths) != len(e.mr_route.mr_paths)): return False for i in range(len(paths)): if (paths[i] != e.mr_route.mr_paths[i]): return False return True return False def fib_interface_ip_prefix(test, addr, len, sw_if_index): # can't use python net here since we need the host bits in the prefix prefix = "%s/%d" % (addr, len) addrs = test.vapi.ip_address_dump( sw_if_index, is_ipv6=(6 == ip_address(addr).version)) for a in addrs: if a.sw_if_index == sw_if_index and \ str(a.prefix) == prefix: return True return False class VppIpTable(VppObject): def __init__(self, test, table_id, is_ip6=0): self._test = test self.table_id = table_id self.is_ip6 = is_ip6 def add_vpp_config(self): self._test.vapi.ip_table_add_del(is_add=1, table={'is_ip6': self.is_ip6, 'table_id': self.table_id}) self._test.registry.register(self, self._test.logger) return self def remove_vpp_config(self): self._test.vapi.ip_table_add_del(is_add=0, table={'is_ip6': self.is_ip6, 'table_id': self.table_id}) def replace_begin(self): self._test.vapi.ip_table_replace_begin( table={'is_ip6': self.is_ip6, 'table_id': self.table_id}) def replace_end(self): self._test.vapi.ip_table_replace_end( table={'is_ip6': self.is_ip6, 'table_id': self.table_id}) def flush(self): self._test.vapi.ip_table_flush(table={'is_ip6': self.is_ip6, 'table_id': self.table_id}) def dump(self): return self._test.vapi.ip_route_dump(self.table_id, self.is_ip6) def mdump(self): return self._test.vapi.ip_mroute_dump(self.table_id, self.is_ip6) def query_vpp_config(self): if self.table_id == 0: # the default table always exists return False # find the default route return find_route(self._test, "::" if self.is_ip6 else "0.0.0.0", 0, self.table_id) def object_id(self): return ("table-%s-%d" % ("v6" if self.is_ip6 == 1 else "v4", self.table_id)) class VppIpInterfaceAddress(VppObject): def __init__(self, test, intf, addr, len): self._test = test self.intf = intf self.addr = addr self.len = len self.prefix = "%s/%d" % (addr, len) def add_vpp_config(self): self._test.vapi.sw_interface_add_del_address( sw_if_index=self.intf.sw_if_index, prefix=self.prefix, is_add=1) self._test.registry.register(self, self._test.logger) return self def remove_vpp_config(self): self._test.vapi.sw_interface_add_del_address( sw_if_index=self.intf.sw_if_index, prefix=self.prefix, is_add=0) def query_vpp_config(self): return fib_interface_ip_prefix(self._test, self.addr, self.len, self.intf.sw_if_index) def object_id(self): return "interface-ip-%s-%s" % (self.intf, self.prefix) class VppIpInterfaceBind(VppObject): def __init__(self
/*
 * Copyright (c) 2015-2019 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.
 */
/*
 * ip/udp_format.c: udp formatting
 *
 * Copyright (c) 2008 Eliot Dresselhaus
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <vnet/ip/ip.h>

/* Format UDP header. */
u8 *
format_udp_header (u8 * s, va_list * args)
{
  udp_header_t *udp = va_arg (*args, udp_header_t *);
  u32 max_header_bytes = va_arg (*args, u32);
  u32 indent;
  u32 header_bytes = sizeof (udp[0]);

  /* Nothing to do. */
  if (max_header_bytes < sizeof (udp[0]))
    return format (s, "UDP header truncated");

  indent = format_get_indent (s);
  indent += 2;

  s = format (s, "UDP: %d -> %d",
	      clib_net_to_host_u16 (udp->src_port),
	      clib_net_to_host_u16 (udp->dst_port));

  s = format (s, "\n%Ulength %d, checksum 0x%04x",
	      format_white_space, indent,
	      clib_net_to_host_u16 (udp->length),
	      clib_net_to_host_u16 (udp->checksum));

  /* Recurse into next protocol layer. */
  if (max_header_bytes != 0 && header_bytes < max_header_bytes)
    {
      ip_main_t *im = &ip_main;
      tcp_udp_port_info_t *pi;

      pi = ip_get_tcp_udp_port_info (im, udp->dst_port);

      if (pi && pi->format_header)
	s = format (s, "\n%U%U",
		    format_white_space, indent - 2, pi->format_header,
		    /* next protocol header */ (udp + 1),
		    max_header_bytes - sizeof (udp[0]));
    }

  return s;
}

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