aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_l2xc.py
blob: 2ec4af9288ee348e5c3ce588f5a9bfdf7b020c6c (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
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
#!/usr/bin/env python

import unittest
import random

from scapy.packet import Raw
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP

from framework import VppTestCase, VppTestRunner
from util import Host, ppp


class TestL2xc(VppTestCase):
    """ L2XC Test Case """

    @classmethod
    def setUpClass(cls):
        """
        Perform standard class setup (defined by class method setUpClass in
        class VppTestCase) before running the test case, set test case related
        variables and configure VPP.

        :var int hosts_nr: Number of hosts to be created.
        :var int dl_pkts_per_burst: Number of packets in burst for dual-loop
            test.
        :var int sl_pkts_per_burst: Number of packets in burst for single-loop
            test.
        """
        super(TestL2xc, cls).setUpClass()

        # Test variables
        cls.hosts_nr = 10
        cls.dl_pkts_per_burst = 257
        cls.sl_pkts_per_burst = 2

        try:
            # create 4 pg interfaces
            cls.create_pg_interfaces(range(4))

            # packet flows mapping pg0 -> pg1, pg2 -> pg3, etc.
            cls.flows = dict()
            cls.flows[cls.pg0] = [cls.pg1]
            cls.flows[cls.pg1] = [cls.pg0]
            cls.flows[cls.pg2] = [cls.pg3]
            cls.flows[cls.pg3] = [cls.pg2]

            # packet sizes
            cls.pg_if_packet_sizes = [64, 512, 1518, 9018]

            cls.interfaces = list(cls.pg_interfaces)

            # Create bi-directional cross-connects between pg0 and pg1
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg0.sw_if_index, cls.pg1.sw_if_index, enable=1)
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg1.sw_if_index, cls.pg0.sw_if_index, enable=1)

            # Create bi-directional cross-connects between pg2 and pg3
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg2.sw_if_index, cls.pg3.sw_if_index, enable=1)
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg3.sw_if_index, cls.pg2.sw_if_index, enable=1)

            # mapping between packet-generator index and lists of test hosts
            cls.hosts_by_pg_idx = dict()

            # Create host MAC and IPv4 lists
            cls.create_host_lists(cls.hosts_nr)

            # setup all interfaces
            for i in cls.interfaces:
                i.admin_up()

        except Exception:
            super(TestL2xc, cls).tearDownClass()
            raise

    def setUp(self):
        super(TestL2xc, self).setUp()
        self.reset_packet_infos()

    def tearDown(self):
        """
        Show various debug prints after each test.
        """
        super(TestL2xc, self).tearDown()
        if not self.vpp_dead:
            self.logger.info(self.vapi.ppcli("show l2patch"))

    @classmethod
    def create_host_lists(cls, count):
        """
        Method to create required number of MAC and IPv4 addresses.
        Create required number of host MAC addresses and distribute them among
        interfaces. Create host IPv4 address for every host MAC address too.

        :param count: Number of hosts to create MAC and IPv4 addresses for.
        """
        for pg_if in cls.pg_interfaces:
            cls.hosts_by_pg_idx[pg_if.sw_if_index] = []
            hosts = cls.hosts_by_pg_idx[pg_if.sw_if_index]
            for j in range(0, count):
                host = Host(
                    "00:00:00:ff:%02x:%02x" % (pg_if.sw_if_index, j),
                    "172.17.1%02x.%u" % (pg_if.sw_if_index, j))
                hosts.append(host)

    def create_stream(self, src_if, packet_sizes, packets_per_burst):
        """
        Create input packet stream for defined interface.

        :param object src_if: Interface to create packet stream for.
        :param list packet_sizes: List of required packet sizes.
        :param int packets_per_burst: Number of packets in burst.
        :return: Stream of packets.
        """
        pkts = []
        for i in range(0, packets_per_burst):
            dst_if = self.flows[src_if][0]
            dst_host = random.choice(self.hosts_by_pg_idx[dst_if.sw_if_index])
            src_host = random.choice(self.hosts_by_pg_idx[src_if.sw_if_index])
            pkt_info = self.create_packet_info(src_if, dst_if)
            payload = self.info_to_payload(pkt_info)
            p = (Ether(dst=dst_host.mac, src=src_host.mac) /
                 IP(src=src_host.ip4, dst=dst_host.ip4) /
                 UDP(sport=1234, dport=1234) /
                 Raw(payload))
            pkt_info.data = p.copy()
            size = random.choice(packet_sizes)
            self.extend_packet(p, size)
            pkts.append(p)
        return pkts

    def verify_capture(self, pg_if, capture):
        """
        Verify captured input packet stream for defined interface.

        :param object pg_if: Interface to verify captured packet stream for.
        :param list capture: Captured packet stream.
        """
        last_info = dict()
        for i in self.interfaces:
            last_info[i.sw_if_index] = None
        dst_sw_if_index = pg_if.sw_if_index
        for packet in capture:
            try:
                ip = packet[IP]
                udp = packet[UDP]
                payload_info = self.payload_to_info(str(packet[Raw]))
                packet_index = payload_info.index
                self.assertEqual(payload_info.dst, dst_sw_if_index)
                self.logger.debug("Got packet on port %s: src=%u (id=%u)" %
                                  (pg_if.name, payload_info.src, packet_index))
                next_info = self.get_next_packet_info_for_interface2(
                    payload_info.src, dst_sw_if_index,
                    last_info[payload_info.src])
                last_info[payload_info.src] = next_info
                self.assertTrue(next_info is not None)
                self.assertEqual(packet_index, next_info.index)
                saved_packet = next_info.data
                # Check standard fields
                self.assertEqual(ip.src, saved_packet[IP].src)
                self.assertEqual(ip.dst, saved_packet[IP].dst)
                self.assertEqual(udp.sport, saved_packet[UDP].sport)
                self.assertEqual(udp.dport, saved_packet[UDP].dport)
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise
        for i in self.interfaces:
            remaining_packet = self.get_next_packet_info_for_interface2(
                i, dst_sw_if_index, last_info[i.sw_if_index])
            self.assertTrue(remaining_packet is None,
                            "Port %u: Packet expected from source %u didn't"
                            " arrive" % (dst_sw_if_index, i.sw_if_index))

    def run_l2xc_test(self, pkts_per_burst):
        """ L2XC test """

        # Create incoming packet streams for packet-generator interfaces
        for i in self.interfaces:
            pkts = self.create_stream(i, self.pg_if_packet_sizes,
                                      pkts_per_burst)
            i.add_stream(pkts)

        # Enable packet capturing and start packet sending
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        # Verify outgoing packet streams per packet-generator interface
        for i in self.pg_interfaces:
            capture = i.get_capture()
            self.logger.info("Verifying capture on interface %s" % i.name)
            self.verify_capture(i, capture)

    def test_l2xc_sl(self):
        """ L2XC single-loop test

        Test scenario:
            1. config
                2 pairs of 2 interfaces, l2xconnected

            2. sending l2 eth packets between 4 interfaces
                64B, 512B, 1518B, 9018B (ether_size)
                burst of 2 packets per interface
        """

        self.run_l2xc_test(self.sl_pkts_per_burst)

    def test_l2xc_dl(self):
        """ L2XC dual-loop test

        Test scenario:
            1. config
                2 pairs of 2 interfaces, l2xconnected

            2. sending l2 eth packets between 4 interfaces
                64B, 512B, 1518B, 9018B (ether_size)
                burst of 257 packets per interface
        """

        self.run_l2xc_test(self.dl_pkts_per_burst)


if __name__ == '__main__':
    unittest.main(testRunner=VppTestRunner)
lass="o">== FIB_PROTOCOL_IP6) { hash_set_mem_alloc (&gm->tunnel_by_key6, &key->gtk_v6, t->dev_instance); } else { hash_set_mem_alloc (&gm->tunnel_by_key4, &key->gtk_v4, t->dev_instance); } } static void gre_tunnel_db_remove (gre_tunnel_t * t, gre_tunnel_key_t * key) { gre_main_t *gm = &gre_main; if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6) { hash_unset_mem_free (&gm->tunnel_by_key6, &key->gtk_v6); } else { hash_unset_mem_free (&gm->tunnel_by_key4, &key->gtk_v4); } } /** * gre_tunnel_stack * * 'stack' (resolve the recursion for) the tunnel's midchain adjacency */ void gre_tunnel_stack (adj_index_t ai) { gre_main_t *gm = &gre_main; ip_adjacency_t *adj; gre_tunnel_t *gt; u32 sw_if_index; adj = adj_get (ai); sw_if_index = adj->rewrite_header.sw_if_index; if ((vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index) || (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) return; gt = pool_elt_at_index (gm->tunnels, gm->tunnel_index_by_sw_if_index[sw_if_index]); if ((vnet_hw_interface_get_flags (vnet_get_main (), gt->hw_if_index) & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0) { adj_midchain_delegate_unstack (ai); } else { adj_midchain_delegate_stack (ai, gt->outer_fib_index, &gt->tunnel_dst); } } /** * mgre_tunnel_stack * * 'stack' (resolve the recursion for) the tunnel's midchain adjacency */ static void mgre_tunnel_stack (adj_index_t ai) { gre_main_t *gm = &gre_main; const ip_adjacency_t *adj; const gre_tunnel_t *gt; u32 sw_if_index; adj = adj_get (ai); sw_if_index = adj->rewrite_header.sw_if_index; if ((vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index) || (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) return; gt = pool_elt_at_index (gm->tunnels, gm->tunnel_index_by_sw_if_index[sw_if_index]); if ((vnet_hw_interface_get_flags (vnet_get_main (), gt->hw_if_index) & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0) { adj_midchain_delegate_unstack (ai); } else { const teib_entry_t *ne; ne = teib_entry_find_46 (sw_if_index, adj->ia_nh_proto, &adj->sub_type.nbr.next_hop); if (NULL != ne) teib_entry_adj_stack (ne, ai); } } /** * @brief Call back when restacking all adjacencies on a GRE interface */ static adj_walk_rc_t gre_adj_walk_cb (adj_index_t ai, void *ctx) { gre_tunnel_stack (ai); return (ADJ_WALK_RC_CONTINUE); } static adj_walk_rc_t mgre_adj_walk_cb (adj_index_t ai, void *ctx) { mgre_tunnel_stack (ai); return (ADJ_WALK_RC_CONTINUE); } static void gre_tunnel_restack (gre_tunnel_t * gt) { fib_protocol_t proto; /* * walk all the adjacencies on th GRE interface and restack them */ FOR_EACH_FIB_IP_PROTOCOL (proto) { switch (gt->mode) { case TUNNEL_MODE_P2P: adj_nbr_walk (gt->sw_if_index, proto, gre_adj_walk_cb, NULL); break; case TUNNEL_MODE_MP: adj_nbr_walk (gt->sw_if_index, proto, mgre_adj_walk_cb, NULL); break; } } } static void gre_teib_mk_key (const gre_tunnel_t * t, const teib_entry_t * ne, gre_tunnel_key_t * key) { const fib_prefix_t *nh; nh = teib_entry_get_nh (ne); /* construct the key using mode P2P so it can be found in the DP */ if (FIB_PROTOCOL_IP4 == nh->fp_proto) gre_mk_key4 (t->tunnel_src.ip4, nh->fp_addr.ip4, teib_entry_get_fib_index (ne), t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v4); else gre_mk_key6 (&t->tunnel_src.ip6, &nh->fp_addr.ip6, teib_entry_get_fib_index (ne), t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v6); } /** * An TEIB entry has been added */ static void gre_teib_entry_added (const teib_entry_t * ne) { gre_main_t *gm = &gre_main; const ip_address_t *nh; gre_tunnel_key_t key; gre_tunnel_t *t; u32 sw_if_index; u32 t_idx; sw_if_index = teib_entry_get_sw_if_index (ne); if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index) return; t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index]; if (INDEX_INVALID == t_idx) return; /* entry has been added on an interface for which there is a GRE tunnel */ t = pool_elt_at_index (gm->tunnels, t_idx); if (t->mode != TUNNEL_MODE_MP) return; /* the next-hop (underlay) of the NHRP entry will form part of the key for * ingress lookup to match packets to this interface */ gre_teib_mk_key (t, ne, &key); gre_tunnel_db_add (t, &key); /* update the rewrites for each of the adjacencies for this peer (overlay) * using the next-hop (underlay) */ mgre_walk_ctx_t ctx = { .t = t, .ne = ne }; nh = teib_entry_get_peer (ne); adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne), (AF_IP4 == ip_addr_version (nh) ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &ip_addr_46 (nh), mgre_mk_complete_walk, &ctx); } static void gre_teib_entry_deleted (const teib_entry_t * ne) { gre_main_t *gm = &gre_main; const ip_address_t *nh; gre_tunnel_key_t key; gre_tunnel_t *t; u32 sw_if_index; u32 t_idx; sw_if_index = teib_entry_get_sw_if_index (ne); if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index) return; t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index]; if (INDEX_INVALID == t_idx) return; t = pool_elt_at_index (gm->tunnels, t_idx); /* remove the next-hop as an ingress lookup key */ gre_teib_mk_key (t, ne, &key); gre_tunnel_db_remove (t, &key); nh = teib_entry_get_peer (ne); /* make all the adjacencies incomplete */ adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne), (AF_IP4 == ip_addr_version (nh) ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &ip_addr_46 (nh), mgre_mk_incomplete_walk, t); } static walk_rc_t gre_tunnel_delete_teib_walk (index_t nei, void *ctx) { gre_tunnel_t *t = ctx; gre_tunnel_key_t key; gre_teib_mk_key (t, teib_entry_get (nei), &key); gre_tunnel_db_remove (t, &key); return (WALK_CONTINUE); } static walk_rc_t gre_tunnel_add_teib_walk (index_t nei, void *ctx) { gre_tunnel_t *t = ctx; gre_tunnel_key_t key = {}; gre_teib_mk_key (t, teib_entry_get (nei), &key); gre_tunnel_db_add (t, &key); return (WALK_CONTINUE); } static int vnet_gre_tunnel_add (vnet_gre_tunnel_add_del_args_t * a, u32 outer_fib_index, u32 * sw_if_indexp) { gre_main_t *gm = &gre_main; vnet_main_t *vnm = gm->vnet_main; gre_tunnel_t *t; vnet_hw_interface_t *hi; u32 hw_if_index, sw_if_index; u8 is_ipv6 = a->is_ipv6; gre_tunnel_key_t key; t = gre_tunnel_db_find (a, outer_fib_index, &key); if (NULL != t) return VNET_API_ERROR_IF_ALREADY_EXISTS; pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES); clib_memset (t, 0, sizeof (*t)); /* Reconcile the real dev_instance and a possible requested instance */ u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */ u32 u_idx = a->instance; /* user specified instance */ if (u_idx == ~0) u_idx = t_idx; if (hash_get (gm->instance_used, u_idx)) { pool_put (gm->tunnels, t); return VNET_API_ERROR_INSTANCE_IN_USE; } hash_set (gm->instance_used, u_idx, 1); t->dev_instance = t_idx; /* actual */ t->user_instance = u_idx; /* name */ t->type = a->type; t->mode = a->mode; t->flags = a->flags; if (t->type == GRE_TUNNEL_TYPE_ERSPAN) t->session_id = a->session_id; if (t->type == GRE_TUNNEL_TYPE_L3) { if (t->mode == TUNNEL_MODE_P2P) hw_if_index = vnet_register_interface (vnm, gre_device_class.index, t_idx, gre_hw_interface_class.index, t_idx); else hw_if_index = vnet_register_interface (vnm, gre_device_class.index, t_idx, mgre_hw_interface_class.index, t_idx); } else { vnet_eth_interface_registration_t eir = {}; /* Default MAC address (d00b:eed0:0000 + sw_if_index) */ u8 address[6] = { 0xd0, 0x0b, 0xee, 0xd0, (u8) (t_idx >> 8), (u8) t_idx }; eir.dev_class_index = gre_device_class.index; eir.dev_instance = t_idx; eir.address = address; hw_if_index = vnet_eth_register_interface (vnm, &eir); } /* Set GRE tunnel interface output node (not used for L3 payload) */ if (GRE_TUNNEL_TYPE_ERSPAN == t->type) vnet_set_interface_output_node (vnm, hw_if_index, gre_erspan_encap_node.index); else vnet_set_interface_output_node (vnm, hw_if_index, gre_teb_encap_node.index); hi = vnet_get_hw_interface (vnm, hw_if_index); sw_if_index = hi->sw_if_index; t->hw_if_index = hw_if_index; t->outer_fib_index = outer_fib_index; t->sw_if_index = sw_if_index; t->l2_adj_index = ADJ_INDEX_INVALID; vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0); gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx; if (!is_ipv6) { hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t); } else { hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip6_header_t); } /* Standard default gre MTU. */ vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000); /* * source the FIB entry for the tunnel's destination * and become a child thereof. The tunnel will then get poked * when the forwarding for the entry updates, and the tunnel can * re-stack accordingly */ clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src)); t->tunnel_dst.fp_len = !is_ipv6 ? 32 : 128; t->tunnel_dst.fp_proto = !is_ipv6 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; t->tunnel_dst.fp_addr = a->dst; gre_tunnel_db_add (t, &key); if (t->mode == TUNNEL_MODE_MP) teib_walk_itf (t->sw_if_index, gre_tunnel_add_teib_walk, t); if (t->type == GRE_TUNNEL_TYPE_ERSPAN) { gre_sn_key_t skey; gre_sn_t *gre_sn; gre_mk_sn_key (t, &skey); gre_sn = (gre_sn_t *) hash_get_mem (gm->seq_num_by_key, &skey); if (gre_sn != NULL) { gre_sn->ref_count++; t->gre_sn = gre_sn; } else { gre_sn = clib_mem_alloc (sizeof (gre_sn_t)); gre_sn->seq_num = 0; gre_sn->ref_count = 1; t->gre_sn = gre_sn; hash_set_mem_alloc (&gm->seq_num_by_key, &skey, (uword) gre_sn); } } if (t->type != GRE_TUNNEL_TYPE_L3) { t->l2_adj_index = adj_nbr_add_or_lock (t->tunnel_dst.fp_proto, VNET_LINK_ETHERNET, &zero_addr, sw_if_index); vnet_set_interface_l3_output_node (gm->vlib_main, sw_if_index, (u8 *) "tunnel-output-no-count"); gre_update_adj (vnm, t->sw_if_index, t->l2_adj_index); } else { vnet_set_interface_l3_output_node (gm->vlib_main, sw_if_index, (u8 *) "tunnel-output"); } if (sw_if_indexp) *sw_if_indexp = sw_if_index; /* register gre46-input nodes */ ip4_register_protocol (IP_PROTOCOL_GRE, gre4_input_node.index); ip6_register_protocol (IP_PROTOCOL_GRE, gre6_input_node.index); return 0; } static int vnet_gre_tunnel_delete (vnet_gre_tunnel_add_del_args_t * a, u32 outer_fib_index, u32 * sw_if_indexp) { gre_main_t *gm = &gre_main; vnet_main_t *vnm = gm->vnet_main; gre_tunnel_t *t; gre_tunnel_key_t key; u32 sw_if_index; t = gre_tunnel_db_find (a, outer_fib_index, &key); if (NULL == t) return VNET_API_ERROR_NO_SUCH_ENTRY; if (t->mode == TUNNEL_MODE_MP) teib_walk_itf (t->sw_if_index, gre_tunnel_delete_teib_walk, t); sw_if_index = t->sw_if_index; vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ ); /* make sure tunnel is removed from l2 bd or xconnect */ set_int_l2_mode (gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, L2_BD_PORT_TYPE_NORMAL, 0, 0); gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0; if (t->type == GRE_TUNNEL_TYPE_L3) vnet_delete_hw_interface (vnm, t->hw_if_index); else ethernet_delete_interface (vnm, t->hw_if_index); if (t->l2_adj_index != ADJ_INDEX_INVALID) { adj_midchain_delegate_unstack (t->l2_adj_index); adj_unlock (t->l2_adj_index); } ASSERT ((t->type != GRE_TUNNEL_TYPE_ERSPAN) || (t->gre_sn != NULL)); if ((t->type == GRE_TUNNEL_TYPE_ERSPAN) && (t->gre_sn->ref_count-- == 1)) { gre_sn_key_t skey; gre_mk_sn_key (t, &skey); hash_unset_mem_free (&gm->seq_num_by_key, &skey); clib_mem_free (t->gre_sn); } vnet_reset_interface_l3_output_node (gm->vlib_main, sw_if_index); hash_unset (gm->instance_used, t->user_instance); gre_tunnel_db_remove (t, &key); pool_put (gm->tunnels, t); if (sw_if_indexp) *sw_if_indexp = sw_if_index; return 0; } int vnet_gre_tunnel_add_del (vnet_gre_tunnel_add_del_args_t * a, u32 * sw_if_indexp) { u32 outer_fib_index; outer_fib_index = fib_table_find ((a->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), a->outer_table_id); if (~0 == outer_fib_index) return VNET_API_ERROR_NO_SUCH_FIB; if (a->session_id > GTK_SESSION_ID_MAX) return VNET_API_ERROR_INVALID_SESSION_ID; if (a->mode == TUNNEL_MODE_MP && !ip46_address_is_zero (&a->dst)) return (VNET_API_ERROR_INVALID_DST_ADDRESS); if (a->is_add) return (vnet_gre_tunnel_add (a, outer_fib_index, sw_if_indexp)); else return (vnet_gre_tunnel_delete (a, outer_fib_index, sw_if_indexp)); } clib_error_t * gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) { gre_main_t *gm = &gre_main; vnet_hw_interface_t *hi; gre_tunnel_t *t; u32 ti; hi = vnet_get_hw_interface (vnm, hw_if_index); if (NULL == gm->tunnel_index_by_sw_if_index || hi->sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) return (NULL); ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index]; if (~0 == ti) /* not one of ours */ return (NULL); t = pool_elt_at_index (gm->tunnels, ti); if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP); else vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ ); gre_tunnel_restack (t); return /* no error */ 0; } static clib_error_t * create_gre_tunnel_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; vnet_gre_tunnel_add_del_args_t _a, *a = &_a; ip46_address_t src = ip46_address_initializer, dst = ip46_address_initializer; u32 instance = ~0; u32 outer_table_id = 0; gre_tunnel_type_t t_type = GRE_TUNNEL_TYPE_L3; tunnel_mode_t t_mode = TUNNEL_MODE_P2P; tunnel_encap_decap_flags_t flags = TUNNEL_ENCAP_DECAP_FLAG_NONE; u32 session_id = 0; int rv; u8 is_add = 1; u32 sw_if_index; clib_error_t *error = NULL; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "del")) is_add = 0; else if (unformat (line_input, "instance %d", &instance)) ; else if (unformat (line_input, "src %U", unformat_ip46_address, &src)) ; else if (unformat (line_input, "dst %U", unformat_ip46_address, &dst)) ; else if (unformat (line_input, "outer-table-id %d", &outer_table_id)) ; else if (unformat (line_input, "multipoint")) t_mode = TUNNEL_MODE_MP; else if (unformat (line_input, "teb")) t_type = GRE_TUNNEL_TYPE_TEB; else if (unformat (line_input, "erspan %d", &session_id)) t_type = GRE_TUNNEL_TYPE_ERSPAN; else if (unformat (line_input, "flags %U", unformat_tunnel_encap_decap_flags, &flags)) ; else { error = clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); goto done; } } if (ip46_address_is_equal (&src, &dst)) { error = clib_error_return (0, "src and dst are identical"); goto done; } if (t_mode != TUNNEL_MODE_MP && ip46_address_is_zero (&dst)) { error = clib_error_return (0, "destination address not specified"); goto done; } if (ip46_address_is_zero (&src)) { error = clib_error_return (0, "source address not specified"); goto done; } if (ip46_address_is_ip4 (&src) != ip46_address_is_ip4 (&dst)) { error = clib_error_return (0, "src and dst address must be the same AF"); goto done; } clib_memset (a, 0, sizeof (*a)); a->is_add = is_add; a->outer_table_id = outer_table_id; a->type = t_type; a->mode = t_mode; a->session_id = session_id; a->is_ipv6 = !ip46_address_is_ip4 (&src); a->instance = instance; a->flags = flags; clib_memcpy (&a->src, &src, sizeof (a->src)); clib_memcpy (&a->dst, &dst, sizeof (a->dst)); rv = vnet_gre_tunnel_add_del (a, &sw_if_index); switch (rv) { case 0: vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index); break; case VNET_API_ERROR_IF_ALREADY_EXISTS: error = clib_error_return (0, "GRE tunnel already exists..."); goto done; case VNET_API_ERROR_NO_SUCH_FIB: error = clib_error_return (0, "outer table ID %d doesn't exist\n", outer_table_id); goto done; case VNET_API_ERROR_NO_SUCH_ENTRY: error = clib_error_return (0, "GRE tunnel doesn't exist"); goto done; case VNET_API_ERROR_INVALID_SESSION_ID: error = clib_error_return (0, "session ID %d out of range\n", session_id); goto done; case VNET_API_ERROR_INSTANCE_IN_USE: error = clib_error_return (0, "Instance is in use"); goto done; default: error = clib_error_return (0, "vnet_gre_tunnel_add_del returned %d", rv); goto done; } done: unformat_free (line_input); return error; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = { .path = "create gre tunnel", .short_help = "create gre tunnel src <addr> dst <addr> [instance <n>] " "[outer-fib-id <fib>] [teb | erspan <session-id>] [del] " "[multipoint]", .function = create_gre_tunnel_command_fn, }; /* *INDENT-ON* */ static clib_error_t * show_gre_tunnel_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { gre_main_t *gm = &gre_main; gre_tunnel_t *t; u32 ti = ~0; if (pool_elts (gm->tunnels) == 0) vlib_cli_output (vm, "No GRE tunnels configured..."); while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "%d", &ti)) ; else break; } if (~0 == ti) { /* *INDENT-OFF* */ pool_foreach (t, gm->tunnels) { vlib_cli_output (vm, "%U", format_gre_tunnel, t); } /* *INDENT-ON* */ } else { t = pool_elt_at_index (gm->tunnels, ti); vlib_cli_output (vm, "%U", format_gre_tunnel, t); } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = { .path = "show gre tunnel", .function = show_gre_tunnel_command_fn, }; /* *INDENT-ON* */ const static teib_vft_t gre_teib_vft = { .nv_added = gre_teib_entry_added, .nv_deleted = gre_teib_entry_deleted, }; /* force inclusion from application's main.c */ clib_error_t * gre_interface_init (vlib_main_t * vm) { teib_register (&gre_teib_vft); return (NULL); } VLIB_INIT_FUNCTION (gre_interface_init); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */