mpls: add mpls_interface_dump 22/39022/4
authorPim van Pelt <pim@ipng.nl>
Sat, 10 Jun 2023 14:46:34 +0000 (16:46 +0200)
committerNeale Ranns <neale@graphiant.com>
Tue, 29 Aug 2023 12:42:13 +0000 (12:42 +0000)
Add an API call mpls_interface_dump() which returns a list of mpls_interface_details:
- If no sw_if_index is given, all MPLS enabled sw_if_index are returned.
- If a particular sw_if_index is given, and it doesn't exist, an empty list is returned.
- If a sw_if_index exists and has MPLS enabled, a list of that one sw_if_index is returned.

Tested:
- Create 3 loopback interfaces
- Call for ~0 and for sw_if_index 0..5 all return empty lists
- set int mpls loop0 enable
- set int mpls loop1 enable
- Call for ~0 returns 2, and the call for sw_if_index=1 and =2 (the loopbacks) returns
  each a list of one sw_if_index 1 resp 2, the other values of sw_if_index return empty list
- set int mpls loop0 disable
- Call for ~0 returns 1, and the call for sw_if_index=2 (loop1) returns both a list of one
  sw_if_index=2, the other values of sw_if_index return empty list
- set int mpls loop1 disable
- Call for ~0 and for sw_if_index 0..5 all return empty lists

Example Python3 API program:
```
api_response = vpp.api.mpls_interface_dump()
print(f"Response is {api_response}")

for i in [ 0, 1, 2, 3, 4, 5 ]:
  api_response = vpp.api.mpls_interface_dump(sw_if_index=i)
  print(f"Response[{i}] = {api_response}")
```

Type: improvement
Change-Id: If87f7d7f8972d99260e859757dbcb251c6fa54a8
Signed-off-by: Pim van Pelt <pim@ipng.nl>
src/vnet/mpls/mpls.api
src/vnet/mpls/mpls_api.c

index 4c164bb..5d775da 100644 (file)
@@ -92,6 +92,26 @@ define mpls_tunnel_details
   vl_api_mpls_tunnel_t mt_tunnel;
 };
 
+/** \brief Dump mpls enabled interface(s)
+    @param client_index - opaque cookie to identify the sender
+    @param sw_if_index - sw_if_index of a specific interface, or -1 (default)
+                         to return all MPLS enabled interfaces
+*/
+define mpls_interface_dump
+{
+  u32 client_index;
+  u32 context;
+  vl_api_interface_index_t sw_if_index [default=0xffffffff];
+};
+
+/** \brief mpls enabled interface details
+*/
+define mpls_interface_details
+{
+  u32 context;
+  vl_api_interface_index_t sw_if_index;
+};
+
 /** \brief MPLS Route Add / del route
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request
index fac5282..dcda0a6 100644 (file)
@@ -409,6 +409,54 @@ vl_api_mpls_table_dump_t_handler (vl_api_mpls_table_dump_t * mp)
   /* *INDENT-ON* */
 }
 
+static void
+send_mpls_interface_details (vpe_api_main_t *am, vl_api_registration_t *reg,
+                            u32 context, const u32 sw_if_index)
+{
+  vl_api_mpls_interface_details_t *mp;
+
+  mp = vl_msg_api_alloc_zero (sizeof (*mp));
+  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_MPLS_INTERFACE_DETAILS);
+  mp->context = context;
+
+  mp->sw_if_index = htonl (sw_if_index);
+  vl_api_send_msg (reg, (u8 *) mp);
+}
+
+static void
+vl_api_mpls_interface_dump_t_handler (vl_api_mpls_interface_dump_t *mp)
+{
+  vpe_api_main_t *am = &vpe_api_main;
+  vl_api_registration_t *reg;
+  vnet_interface_main_t *im = &vnet_main.interface_main;
+  vnet_sw_interface_t *si;
+  u32 sw_if_index = ~0;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+  sw_if_index = ntohl (mp->sw_if_index);
+
+  if (sw_if_index == ~0)
+    {
+      pool_foreach (si, im->sw_interfaces)
+       {
+         if (mpls_sw_interface_is_enabled (si->sw_if_index))
+           {
+             send_mpls_interface_details (am, reg, mp->context,
+                                          si->sw_if_index);
+           }
+       }
+    }
+  else
+    {
+      if (mpls_sw_interface_is_enabled (sw_if_index))
+       {
+         send_mpls_interface_details (am, reg, mp->context, sw_if_index);
+       }
+    }
+}
+
 static void
 send_mpls_route_details (vpe_api_main_t * am,
                         vl_api_registration_t * reg,