#!/usr/bin/env python3 import unittest from framework import VppTestCase, VppTestRunner from vpp_sub_interface import VppDot1QSubint from vpp_ip import DpoProto from vpp_ip_route import VppIpRoute, VppRoutePath, VppMplsRoute, \ VppMplsLabel, VppMplsTable, FibPathProto import scapy.compat from scapy.packet import Raw from scapy.layers.l2 import Ether, Dot1Q from scapy.layers.inet import IP, UDP from scapy.layers.inet6 import IPv6 from scapy.contrib.mpls import MPLS from vpp_papi import VppEnum from vpp_qos import VppQosRecord, VppQosEgressMap, VppQosMark, VppQosStore NUM_PKTS = 67 class TestQOS(VppTestCase): """ QOS Test Case """ # Note: Since the enums aren't created dynamically until after # the papi client attaches to VPP, we put it in a property to # ensure it is the value at runtime, not at module load time. @property def QOS_SOURCE(self): return VppEnum.vl_api_qos_source_t @classmethod def setUpClass(cls): super(TestQOS, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestQOS, cls).tearDownClass() def setUp(self): super(TestQOS, self).setUp() self.create_pg_interfaces(range(5)) tbl = VppMplsTable(self, 0) tbl.add_vpp_config() for i in self.pg_interfaces: i.admin_up() i.config_ip4() i.resolve_arp() i.config_ip6() i.resolve_ndp() i.enable_mpls() def tearDown(self): for i in self.pg_interfaces: i.unconfig_ip4() i.unconfig_ip6() i.disable_mpls() super(TestQOS, self).tearDown() def test_qos_ip(self): """ QoS Mark/Record/Store IP """ # # for table 1 map the n=0xff possible values of input QoS mark, # n to 1-n # output = [scapy.compat.chb(0)] * 256 for i in range(0, 255): output[i] = scapy.compat.chb(255 - i) os = b''.join(output) rows = [{'outputs': os}, {'outputs': os}, {'outputs': os}, {'outputs': os}] qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config() # # For table 2 (and up) use the value n for everything # output = [scapy.compat.chb(2)] * 256 os = b''.join(output) rows = [{'outputs': os}, {'outputs': os}, {'outputs': os}, {'outputs': os}] qem2 = VppQosEgressMap(self, 2, rows).add_vpp_config() output = [scapy.compat.chb(3)] * 256 os = b''.join(output) rows = [{'outputs': os}, {'outputs': os}, {'outputs': os}, {'outputs': os}] qem3 = VppQosEgressMap(self, 3, rows).add_vpp_config() output = [scapy.compat.chb(4)] * 256 os = b''.join(output) rows = [{'outputs': os}, {'outputs': os}, {'outputs': os}, {'outputs': os}] qem4 = VppQosEgressMap(self, 4, rows).add_vpp_config() qem5 = VppQosEgressMap(self, 5, rows).add_vpp_config() qem6 = VppQosEgressMap(self, 6, rows).add_vpp_config() qem7 = VppQosEgressMap(self, 7, rows).add_vpp_config() self.assertTrue(qem7.query_vpp_config()) self.logger.info(self.vapi.cli("sh qos eg map")) # # Bind interface pgN to table n # qm1 = VppQosMark(self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_IP).add_vpp_config() qm2 = VppQosMark(self, self.pg2, qem2, self.QOS_SOURCE.QOS_API_SOURCE_IP).add_vpp_config() qm3 = VppQosMark(self, self.pg3, qem3, self.QOS_SOURCE.QOS_API_SOURCE_IP).add_vpp_config() qm4 = VppQosMark(self, self.pg4, qem4, self.QOS_SOURCE.QOS_API_SOURCE_IP).add_vpp_config() self.assertTrue(qm3.query_vpp_config()) self.logger.info(self.vapi.cli("sh qos mark")) # # packets ingress on Pg0 # p_v4 = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, tos=1) / UDP(sport=1234, dport=1234) / Raw(scapy.compat.chb(100) * NUM_PKTS)) p_v6 = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6, tc=1) / UDP(sport=1234, dport=1234) / Raw(scapy.compat.chb(100) * NUM_PKTS)) # # Since we have not yet enabled the recording of the input QoS # from the input iP header, the egress packet's ToS will be unchanged # rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1) for p in rx: self.assertEqual(p[IP].tos, 1) rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1) for p in rx: self.assertEqual(p[IPv6].tc, 1) # # Enable QoS recording on IP input for pg0 # qr1 = VppQosRecord(self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_IP) qr1.add_vpp_config() self.logger.info(self.vapi.cli("sh qos record")) # # send the same packets, this time expect the input TOS of 1 # to be mapped to pg1's egress value of 254 # rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1) for p in rx: self.assertEqual(p[IP].tos, 254) rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1) for p in rx: self.assertEqual(p[IPv6].tc, 254) # # different input ToS to test the mapping # p_v4[IP].tos = 127 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1) for p in rx: self.assertEqual(p[IP].tos, 128) p_v6[IPv6].tc = 127 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1) for p in rx: self.assertEqual(p[IPv6].tc, 128) p_v4[IP].tos = 254 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1) for p in rx: self.assertEqual(p[IP].tos, 1) p_v6[IPv6].tc = 254 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1) for p in rx: self.assertEqual(p[IPv6].tc, 1) # # send packets out the other interfaces to test the maps are # correctly applied # p_v4[IP].dst = self.pg2.remote_ip4 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg2) for p in rx: self.assertEqual(p[IP].tos, 2) p_v4[IP].dst = self
/*
 * Copyright (c) 2015 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.
 */
#ifndef included_vnet_p2p_ethernet_h
#define included_vnet_p2p_ethernet_h

#include <vnet/vnet.h>
#include <vnet/ethernet/ethernet.h>


typedef struct {
  /**
   * Hash mapping parent sw_if_index and client mac address to p2p_ethernet sub-interface
   */
  uword * p2p_ethernet_by_key;

  u32 *p2p_ethernet_by_sw_if_index;

  // Pool of p2p subifs;
  subint_config_t *p2p_subif_pool;

  /* convenience */
  vlib_main_t * vlib_main;
  vnet_main_t * vnet_main;
} p2p_ethernet_main_t;

extern p2p_ethernet_main_t p2p_main;

typedef struct
{
  u32 sw_if_index;
  u32 p2pe_sw_if_index;
  u8  client_mac[6];
} p2p_ethernet_trace_t;

/**
 * @brief Key struct for P2P Ethernet
 * Key fields: parent sw_if_index and client mac address
 * all fields in NET byte order
 */

typedef struct {
  u8 mac[6];
  u16 pad1;         // padding for u64 mac address
  u32 hw_if_index;
  u32 pad2;         // padding for u64
} p2p_key_t;

u32 p2p_ethernet_lookup (u32 parent_sw_if_index, u8