summaryrefslogtreecommitdiffstats
path: root/src/plugins/gbp/gbp_subnet.c
blob: 2daf0ddc684d96cfc8502da8018b5df9d87b961b (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

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: 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 */
}
#!/usr/bin/env python

import os
import subprocess

from framework import VppTestCase

# Api files path
API_FILES_PATH = "vpp/vpp-api/java"

# Registry jar file name prefix
REGISTRY_JAR_PREFIX = "jvpp-registry"


class TestJVpp(VppTestCase):
    """ JVPP Core Test Case """

    def invoke_for_jvpp_core(self, api_jar_name, test_class_name):
        self.jvpp_connection_test(api_jar_name=api_jar_name,
                                  test_class_name=test_class_name,
                                  timeout=10)

    def test_vpp_core_callback_api(self):
        """ JVPP Core Callback Api Test Case """
        self.invoke_for_jvpp_core(api_jar_name="jvpp-core",
                                  test_class_name="io.fd.vpp.jvpp.core.test."
                                                  "CallbackApiTest")

    def test_vpp_core_future_api(self):
        """JVPP Core Future Api Test Case"""
        self.invoke_for_jvpp_core(api_jar_name="jvpp-core",
                                  test_class_name="io.fd.vpp.jvpp.core.test."
                                                  "FutureApiTest")

    def test_vpp_acl_callback_api(self):
        """ JVPP Acl Callback Api Test Case """
        self.invoke_for_jvpp_core(api_jar_name="jvpp-acl",
                                  test_class_name="io.fd.vpp.jvpp.acl.test."
                                                  "CallbackApiTest")

    def test_vpp_acl_future_api(self):
        """JVPP Acl Future Api Test Case"""
        self.invoke_for_jvpp_core(api_jar_name="jvpp-acl",
                                  test_class_name="io.fd.vpp.jvpp.acl.test."
                                                  "FutureApiTest")

    def test_vpp_ioamexport_callback_api(self):
        """ JVPP Ioamexport Callback Api Test Case """
        self.invoke_for_jvpp_core(api_jar_name="jvpp-ioamexport",
                                  test_class_name="io.fd.vpp.jvpp.ioamexport."
                                                  "test.CallbackApiTest")

    def test_vpp_ioamexport_future_api(self):
/*
 * Copyright (c) 2018 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 <plugins/gbp/gbp.h>
#include <plugins/gbp/gbp_fwd_dpo.h>
#include <plugins/gbp/gbp_policy_dpo.h>
#include <plugins/gbp/gbp_route_domain.h>

#include <vnet/fib/fib_table.h>
#include <vnet/dpo/load_balance.h>

/**
 * a key for the DB
 */
typedef struct gbp_subnet_key_t_
{
  fib_prefix_t gsk_pfx;
  u32 gsk_fib_index;
} gbp_subnet_key_t;

/**
 * Subnet
 */
typedef struct gbp_subnet_t_
{
  gbp_subnet_key_t *gs_key;
  gbp_subnet_type_t gs_type;
  index_t gs_rd;

  union
  {
    struct
    {
      sclass_t gs_sclass;
      u32 gs_sw_if_index;
    } gs_stitched_external;
    struct
    {
      sclass_t gs_sclass;
    } gs_l3_out;
  };

  fib_node_index_t gs_fei;
} gbp_subnet_t;

/**
 * A DB of the subnets; key={pfx,fib-index}
 */
uword *gbp_subnet_db;

/**
 * pool of subnets
 */
gbp_subnet_t *gbp_subnet_pool;

static index_t
gbp_subnet_db_find (u32 fib_index, const fib_prefix_t * pfx)
{
  gbp_subnet_key_t key = {
    .gsk_pfx = *pfx,
    .gsk_fib_index = fib_index,
  };
  uword *p;

  p = hash_get_mem (gbp_subnet_db, &key);

  if (NULL != p)
    return p[0];

  return (INDEX_INVALID);
}

static void
gbp_subnet_db_add (u32 fib_index, const fib_prefix_t * pfx, gbp_subnet_t * gs)
{
  gbp_subnet_key_t *key;

  key = clib_mem_alloc (sizeof (*key));

  clib_memcpy (&(key->gsk_pfx), pfx, sizeof (*pfx));
  key->gsk_fib_index = fib_index;

  hash_set_mem (gbp_subnet_db, key, (gs - gbp_subnet_pool));

  gs->gs_key = key;
}

static void
gbp_subnet_db_del (gbp_subnet_t * gs)
{
  hash_unset_mem (gbp_subnet_db, gs->gs_key);

  clib_mem_free (gs->gs_key);
  gs->gs_key = NULL;
}


static int
gbp_subnet_transport_add (gbp_subnet_t * gs)
{
  dpo_id_t gfd = DPO_INVALID;
  gbp_route_domain_t *grd;
  fib_protocol_t fproto;

  fproto = gs->gs_key->gsk_pfx.fp_proto;
  grd = gbp_route_domain_get (gs->gs_rd);

  if (~0 == grd->grd_uu_sw_if_index[fproto])
    return (VNET_API_ERROR_INVALID_SW_IF_INDEX);

  gs->gs_fei = fib_table_entry_update_one_path (gs->gs_key->gsk_fib_index,
						&gs->gs_key->gsk_pfx,
						FIB_SOURCE_PLUGIN_HI,
						FIB_ENTRY_FLAG_NONE,
						fib_proto_to_dpo (fproto),
						&ADJ_BCAST_ADDR,
						grd->grd_uu_sw_if_index
						[fproto], ~0, 1, NULL,
						FIB_ROUTE_PATH_FLAG_NONE);

  dpo_reset (&gfd);

  return (0);
}

static int
gbp_subnet_internal_add (gbp_subnet_t * gs)
{
  dpo_id_t gfd = DPO_INVALID;

  gbp_fwd_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
			   &gfd);

  gs->gs_fei = fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
						   &gs->gs_key->gsk_pfx,
						   FIB_SOURCE_PLUGIN_HI,
						   FIB_ENTRY_FLAG_EXCLUSIVE,
						   &gfd);

  dpo_reset (&gfd);

  return (0);
}

static int
gbp_subnet_external_add (gbp_subnet_t * gs, u32 sw_if_index, sclass_t sclass)
{
  dpo_id_t gpd = DPO_INVALID;

  gs->gs_stitched_external.gs_sclass = sclass;
  gs->gs_stitched_external.gs_sw_if_index = sw_if_index;

  gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
			      gs->gs_stitched_external.gs_sclass,
			      gs->gs_stitched_external.gs_sw_if_index, &gpd);

  gs->gs_fei = fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
						   &gs->gs_key->gsk_pfx,
						   FIB_SOURCE_PLUGIN_HI,
						   (FIB_ENTRY_FLAG_EXCLUSIVE |
						    FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT),
						   &gpd);

  dpo_reset (&gpd);

  return (0);
}

static int
gbp_subnet_l3_out_add (gbp_subnet_t * gs, sclass_t sclass)
{
  dpo_id_t gpd = DPO_INVALID;

  gs->gs_l3_out.gs_sclass = sclass;

  gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
			      gs->gs_l3_out.gs_sclass, ~0, &gpd);

  gs->gs_fei = fib_table_entry_special_dpo_add (gs->gs_key->gsk_fib_index,
						&gs->gs_key->gsk_pfx,
						FIB_SOURCE_SPECIAL,
						FIB_ENTRY_FLAG_INTERPOSE,
						&gpd);

  dpo_reset (&gpd);

  return (0);
}

static void
gbp_subnet_del_i (index_t gsi)
{
  gbp_subnet_t *gs;

  gs = pool_elt_at_index (gbp_subnet_pool, gsi);

  if (GBP_SUBNET_L3_OUT == gs->gs_type)
    fib_table_entry_delete_index (gs->gs_fei, FIB_SOURCE_SPECIAL);
  else
    fib_table_entry_delete_index (gs->gs_fei, FIB_SOURCE_PLUGIN_HI);

  gbp_subnet_db_del (gs);
  gbp_route_domain_unlock (gs->gs_rd);

  pool_put (gbp_subnet_pool, gs);
}

int
gbp_subnet_del (u32 rd_id, const fib_prefix_t * pfx)
{
  gbp_route_domain_t *grd;
  index_t gsi, grdi;
  u32 fib_index;

  grdi = gbp_route_domain_find (rd_id);

  if (~0 == grdi)
    return (VNET_API_ERROR_NO_SUCH_FIB);

  grd = gbp_route_domain_get (grdi);
  fib_index = grd->grd_fib_index[pfx->fp_proto];

  gsi = gbp_subnet_db_find (fib_index, pfx);

  if (INDEX_INVALID == gsi)
    return (VNET_API_ERROR_NO_SUCH_ENTRY);

  gbp_subnet_del_i (gsi);

  return (0);
}

int
gbp_subnet_add (u32 rd_id,
		const fib_prefix_t * pfx,
		gbp_subnet_type_t type, u32 sw_if_index, sclass_t sclass)
{
  gbp_route_domain_t *grd;
  index_t grdi, gsi;
  gbp_subnet_t *gs;
  u32 fib_index;
  int rv;

  grdi = gbp_route_domain_find_and_lock (rd_id);

  if (~0 == grdi)
    return (VNET_API_ERROR_NO_SUCH_FIB);

  grd = gbp_route_domain_get (grdi);
  fib_index = grd->grd_fib_index[pfx->fp_proto];

  gsi = gbp_subnet_db_find (fib_index, pfx);

  /*
   * this is an update if the subnet already exists, so remove the old
   */
  if (INDEX_INVALID != gsi)
    gbp_subnet_del_i (gsi);

  rv = -2;

  pool_get (gbp_subnet_pool, gs);

  gs->gs_type = type;
  gs->gs_rd = grdi;
  gbp_subnet_db_add (fib_index, pfx, gs);

  switch (type)
    {
    case GBP_SUBNET_STITCHED_INTERNAL:
      rv = gbp_subnet_internal_add (gs);
      break;
    case GBP_SUBNET_STITCHED_EXTERNAL:
      rv = gbp_subnet_external_add (gs, sw_if_index, sclass);
      break;
    case GBP_SUBNET_TRANSPORT:
      rv = gbp_subnet_transport_add (gs);
      break;
    case GBP_SUBNET_L3_OUT:
      rv = gbp_subnet_l3_out_add (gs, sclass);
      break;
    }

  return (rv);
}

static clib_error_t *
gbp_subnet_add_del_cli (vlib_main_t * vm,
			unformat_input_t * input, vlib_cli_command_t * cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  fib_prefix_t pfx = {.fp_addr = ip46_address_initializer };
  int length;
  u32 rd_id = ~0;
  u32 sw_if_index = ~0;
  gbp_subnet_type_t type = ~0;
  u32 sclass = ~0;
  int is_add = 1;
  int rv;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "del"))
	is_add = 0;
      else if (unformat (input, "rd %d", &rd_id))
	;
      else
	if (unformat
	    (input, "prefix %U/%d", unformat_ip4_address, &pfx.fp_addr.ip4,
	     &length))
	pfx.fp_proto = FIB_PROTOCOL_IP4;
      else
	if (unformat
	    (input, "prefix %U/%d", unformat_ip6_address, &pfx.fp_addr.ip6,
	     &length))
	pfx.fp_proto = FIB_PROTOCOL_IP6;
      else if (unformat (input, "type transport"))
	type = GBP_SUBNET_TRANSPORT;
      else if (unformat (input, "type internal"))
	type = GBP_SUBNET_STITCHED_INTERNAL;
      else if (unformat (input, "type external"))
	type = GBP_SUBNET_STITCHED_EXTERNAL;
      else if (unformat (input, "type l3out"))
	type = GBP_SUBNET_L3_OUT;
      else
	if (unformat_user
	    (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
	;
      else if (unformat (input, "sclass %u", &sclass))
	;
      else
	break;
    }

  pfx.fp_len = length;

  if (is_add)
    rv = gbp_subnet_add (rd_id, &pfx, type, sw_if_index, sclass);
  else
    rv = gbp_subnet_del (rd_id, &pfx);

  switch (rv)
    {
    case 0:
      return 0;
    case VNET_API_ERROR_NO_SUCH_FIB:
      return clib_error_return (0, "no such FIB");
    }

  return clib_error_return (0, "unknown error %d", rv);
}

/*?
 * Add Group Based Policy Subnets
 *
 * @cliexpar
 * @cliexstart{gbp subnet [del] rd <ID> prefix <prefix> type <type> [<interface>] [sclass <sclass>]}
 * @cliexend
 ?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (gbp_subnet_add_del, static) = {
  .path = "gbp subnet",
  .short_help = "gbp subnet [del] rd <ID> prefix <prefix> type <type> [<interface>] [sclass <sclass>]\n",
  .function = gbp_subnet_add_del_cli,
};
/* *INDENT-ON* */



void
gbp_subnet_walk (gbp_subnet_cb_t cb, void *ctx)
{
  gbp_route_domain_t *grd;
  gbp_subnet_t *gs;
  u32 sw_if_index;
  sclass_t sclass;

  sclass = SCLASS_INVALID;
  sw_if_index = ~0;

  /* *INDENT-OFF* */
  pool_foreach (gs, gbp_subnet_pool,
  ({
    grd = gbp_route_domain_get(gs->gs_rd);

    switch (gs->gs_type)
      {
      case GBP_SUBNET_STITCHED_INTERNAL:
      case GBP_SUBNET_TRANSPORT:
        /* use defaults above */
        break;
      case GBP_SUBNET_STITCHED_EXTERNAL:
        sw_if_index = gs->gs_stitched_external.gs_sw_if_index;
        sclass = gs->gs_stitched_external.gs_sclass;
        break;
      case GBP_SUBNET_L3_OUT:
        sclass = gs->gs_l3_out.gs_sclass;
        break;
      }

    if (WALK_STOP == cb (grd->grd_id, &gs->gs_key->gsk_pfx,
                         gs->gs_type, sw_if_index, sclass, ctx))
      break;
  }));
  /* *INDENT-ON* */
}

typedef enum gsb_subnet_show_flags_t_
{
  GBP_SUBNET_SHOW_BRIEF,
  GBP_SUBNET_SHOW_DETAILS,
} gsb_subnet_show_flags_t;

static u8 *
format_gbp_subnet_type (u8 * s, va_list * args)
{
  gbp_subnet_type_t type = va_arg (*args, gbp_subnet_type_t);

  switch (type)
    {
    case GBP_SUBNET_STITCHED_INTERNAL:
      return (format (s, "stitched-internal"));
    case GBP_SUBNET_STITCHED_EXTERNAL:
      return (format (s, "stitched-external"));
    case GBP_SUBNET_TRANSPORT:
      return (format (s, "transport"));
    case GBP_SUBNET_L3_OUT:
      return (format (s, "l3-out"));
    }

  return (format (s, "unknown"));
}

u8 *
format_gbp_subnet (u8 * s, va_list * args)
{
  index_t gsi = va_arg (*args, index_t);
  gsb_subnet_show_flags_t flags = va_arg (*args, gsb_subnet_show_flags_t);
  gbp_subnet_t *gs;
  u32 table_id;

  gs = pool_elt_at_index (gbp_subnet_pool, gsi);

  table_id = fib_table_get_table_id (gs->gs_key->gsk_fib_index,
				     gs->gs_key->gsk_pfx.fp_proto);

  s = format (s, "[%d] tbl:%d %U %U", gsi, table_id,
	      format_fib_prefix, &gs->gs_key->gsk_pfx,
	      format_gbp_subnet_type, gs->gs_type);

  switch (gs->gs_type)
    {
    case GBP_SUBNET_STITCHED_INTERNAL:
    case GBP_SUBNET_TRANSPORT:
      break;
    case GBP_SUBNET_STITCHED_EXTERNAL:
      s = format (s, " {sclass:%d %U}", gs->gs_stitched_external.gs_sclass,
		  format_vnet_sw_if_index_name,
		  vnet_get_main (), gs->gs_stitched_external.gs_sw_if_index);
      break;
    case GBP_SUBNET_L3_OUT:
      s = format (s, " {sclass:%d}", gs->gs_l3_out.gs_sclass);
      break;
    }

  switch (flags)
    {
    case GBP_SUBNET_SHOW_DETAILS:
      {
	s = format (s, "\n  %U", format_fib_entry, gs->gs_fei,
		    FIB_ENTRY_FORMAT_DETAIL);
      }
    case GBP_SUBNET_SHOW_BRIEF:
      break;
    }
  return (s);
}

static clib_error_t *
gbp_subnet_show (vlib_main_t * vm,
		 unformat_input_t * input, vlib_cli_command_t * cmd)
{
  u32 gsi;

  gsi = INDEX_INVALID;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%d", &gsi))
	;
      else
	break;
    }

  if (INDEX_INVALID != gsi)
    {
      vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
		       GBP_SUBNET_SHOW_DETAILS);
    }
  else
    {
      /* *INDENT-OFF* */
      pool_foreach_index(gsi, gbp_subnet_pool,
      ({
        vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
                         GBP_SUBNET_SHOW_BRIEF);
      }));
      /* *INDENT-ON* */
    }

  return (NULL);
}

/*?
 * Show Group Based Policy Subnets
 *
 * @cliexpar
 * @cliexstart{show gbp subnet}
 * @cliexend
 ?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (gbp_subnet_show_node, static) = {
  .path = "show gbp subnet",
  .short_help = "show gbp subnet\n",
  .function = gbp_subnet_show,
};
/* *INDENT-ON* */

static clib_error_t *
gbp_subnet_init (vlib_main_t * vm)
{
  gbp_subnet_db = hash_create_mem (0,
				   sizeof (gbp_subnet_key_t), sizeof (u32));

  return (NULL);
}

VLIB_INIT_FUNCTION (gbp_subnet_init);

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