diff options
author | Tetsuya Murakami <tetsuya.mrk@gmail.com> | 2019-12-04 18:57:46 -0800 |
---|---|---|
committer | Ole Trøan <otroan@employees.org> | 2019-12-05 15:11:03 +0000 |
commit | 70d8ef89a5d9e38d4f35517c0f64009c47b76c4d (patch) | |
tree | 4dbd294a57d34dd82d755ae465847f5ede7244d7 | |
parent | 66cea092c7a584bb7b82000fbfad349e6f0af7e1 (diff) |
srv6-mobile
Type: feature
Plug-in for IPv6 Segment Routing Mobile
Support the plug-in function in SR Policy.
Support GTP4.D plug-in as SR Policy plug-in.
Signed-off-by: Tetsuya Murakami <tetsuya.mrk@gmail.com>
Change-Id: I306160b7203921d879940193868944158867c482
-rw-r--r-- | src/plugins/srv6-mobile/CMakeLists.txt | 1 | ||||
-rwxr-xr-x | src/plugins/srv6-mobile/extra/runner.py | 53 | ||||
-rw-r--r-- | src/plugins/srv6-mobile/gtp4_d.c | 240 | ||||
-rw-r--r-- | src/plugins/srv6-mobile/mobile.h | 13 | ||||
-rw-r--r-- | src/plugins/srv6-mobile/node.c | 476 | ||||
-rw-r--r-- | src/plugins/srv6-mobile/test/test_srv6_end.py | 5 | ||||
-rwxr-xr-x | src/vnet/srv6/sr.h | 55 | ||||
-rw-r--r-- | src/vnet/srv6/sr_api.c | 5 | ||||
-rwxr-xr-x | src/vnet/srv6/sr_policy_rewrite.c | 216 |
9 files changed, 997 insertions, 67 deletions
diff --git a/src/plugins/srv6-mobile/CMakeLists.txt b/src/plugins/srv6-mobile/CMakeLists.txt index 612d626df91..ebd03f7fbf4 100644 --- a/src/plugins/srv6-mobile/CMakeLists.txt +++ b/src/plugins/srv6-mobile/CMakeLists.txt @@ -14,6 +14,7 @@ add_vpp_plugin(srv6mobile SOURCES gtp4_e.c + gtp4_d.c gtp6_e.c gtp6_d.c gtp6_d_di.c diff --git a/src/plugins/srv6-mobile/extra/runner.py b/src/plugins/srv6-mobile/extra/runner.py index 710252d5ce1..9fffe419029 100755 --- a/src/plugins/srv6-mobile/extra/runner.py +++ b/src/plugins/srv6-mobile/extra/runner.py @@ -527,6 +527,7 @@ class Program(object): for p in c4.pg_read_packets(): p.show2() + ''' T.Map is obsolete def test_tmap(self): # TESTS: # trace add af-packet-input 10 @@ -809,6 +810,7 @@ class Program(object): print("Receiving packet on {}:".format(c4.name)) for p in c4.pg_read_packets(): p.show2() + ''' def test_gtp4(self): # TESTS: @@ -836,9 +838,8 @@ class Program(object): c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix 172.20.0.1/32 " - "behavior end.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -906,10 +907,8 @@ class Program(object): c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix ::ffff:ac14:0001/128 " - "behavior end.m.gtp4.d D4::/32 v6src_prefix C1::/64 " - "nhtype ipv4") + c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -978,10 +977,8 @@ class Program(object): c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix 172.20.0.1/32 " - "behavior end.m.gtp4.d D4::/32 v6src_prefix C1::/64 " - "nhtype ipv4") + c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1047,9 +1044,8 @@ class Program(object): c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix ::ffff:ac14:0001/128 " - "behavior end.m.gtp4.d D4::/32 v6src_prefix C1::/64") + c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64") + c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1117,9 +1113,8 @@ class Program(object): c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix ::ffff:ac14:0001/128 " - "behavior end.m.gtp4.d D4::/32 v6src_prefix C1::/64") + c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64") + c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1935,10 +1930,10 @@ def get_args(): choices=[ "ping", "srv6", - "tmap", - "tmap_5g", - "tmap_ipv6", - "tmap_ipv6_5g", + # "tmap", + # "tmap_5g", + # "tmap_ipv6", + # "tmap_ipv6_5g", "gtp4", "gtp4_5g", "gtp4_echo", @@ -1996,14 +1991,14 @@ def main(op=None, prefix=None, verbose=None, program.test_ping() elif op == 'srv6': program.test_srv6() - elif op == 'tmap': - program.test_tmap() - elif op == 'tmap_5g': - program.test_tmap_5g() - elif op == 'tmap_ipv6': - program.test_tmap_ipv6() - elif op == 'tmap_ipv6_5g': - program.test_tmap_ipv6_5g() + # elif op == 'tmap': + # program.test_tmap() + # elif op == 'tmap_5g': + # program.test_tmap_5g() + # elif op == 'tmap_ipv6': + # program.test_tmap_ipv6() + # elif op == 'tmap_ipv6_5g': + # program.test_tmap_ipv6_5g() elif op == 'gtp4': program.test_gtp4() elif op == 'gtp4_5g': diff --git a/src/plugins/srv6-mobile/gtp4_d.c b/src/plugins/srv6-mobile/gtp4_d.c new file mode 100644 index 00000000000..2160a90260f --- /dev/null +++ b/src/plugins/srv6-mobile/gtp4_d.c @@ -0,0 +1,240 @@ +/* + * srv6_t_m_gtp4_d.c + * + * Copyright (c) 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. + */ + +#include <vnet/vnet.h> +#include <vnet/adj/adj.h> +#include <vnet/plugin/plugin.h> +#include <vpp/app/version.h> +#include <srv6-mobile/mobile.h> + +srv6_t_main_v4_decap_t srv6_t_main_v4_decap; + +static void +clb_dpo_lock_srv6_t_m_gtp4_d (dpo_id_t * dpo) +{ +} + +static void +clb_dpo_unlock_srv6_t_m_gtp4_d (dpo_id_t * dpo) +{ +} + +static u8 * +clb_dpo_format_srv6_t_m_gtp4_d (u8 * s, va_list * args) +{ + index_t index = va_arg (*args, index_t); + CLIB_UNUSED (u32 indent) = va_arg (*args, u32); + + return (format (s, "SR: dynamic_proxy_index:[%u]", index)); +} + +const static dpo_vft_t dpo_vft = { + .dv_lock = clb_dpo_lock_srv6_t_m_gtp4_d, + .dv_unlock = clb_dpo_unlock_srv6_t_m_gtp4_d, + .dv_format = clb_dpo_format_srv6_t_m_gtp4_d, +}; + +const static char *const srv6_t_m_gtp4_d_nodes[] = { + "srv6-t-m-gtp4-d", + NULL, +}; + +const static char *const srv6_t_m_gtp4_d_v6_nodes[] = { + "error-drop", + NULL, +}; + +const static char *const *const dpo_nodes[DPO_PROTO_NUM] = { + [DPO_PROTO_IP6] = srv6_t_m_gtp4_d_v6_nodes, + [DPO_PROTO_IP4] = srv6_t_m_gtp4_d_nodes, +}; + +static u8 fn_name[] = "SRv6-T.M.GTP4.D-plugin"; +static u8 keyword_str[] = "t.m.gtp4.d"; +static u8 def_str[] = + "Transit function with decapsulation for IPv4/GTP tunnel"; +static u8 param_str[] = + "<sr-prefix>/<sr-prefixlen> v6src_prefix <v6src_prefix>/<prefixlen> [nhtype <nhtype>]"; + +static u8 * +clb_format_srv6_t_m_gtp4_d (u8 * s, va_list * args) +{ + srv6_end_gtp4_param_t *ls_mem = va_arg (*args, void *); + + s = format (s, "SRv6 T.M.GTP4.D\n\t"); + + s = + format (s, "SR Prefix: %U/%d, ", format_ip6_address, &ls_mem->sr_prefix, + ls_mem->sr_prefixlen); + + s = + format (s, "v6src Prefix: %U/%d", format_ip6_address, + &ls_mem->v6src_prefix, ls_mem->v6src_prefixlen); + + if (ls_mem->nhtype != SRV6_NHTYPE_NONE) + { + if (ls_mem->nhtype == SRV6_NHTYPE_IPV4) + s = format (s, ", NHType IPv4\n"); + else if (ls_mem->nhtype == SRV6_NHTYPE_IPV6) + s = format (s, ", NHType IPv6\n"); + else if (ls_mem->nhtype == SRV6_NHTYPE_NON_IP) + s = format (s, ", NHType Non-IP\n"); + else + s = format (s, ", NHType Unknow(%d)\n", ls_mem->nhtype); + } + else + s = format (s, "\n"); + + return s; +} + +static uword +clb_unformat_srv6_t_m_gtp4_d (unformat_input_t * input, va_list * args) +{ + void **plugin_mem_p = va_arg (*args, void **); + srv6_end_gtp4_param_t *ls_mem; + ip6_address_t sr_prefix; + u32 sr_prefixlen; + ip6_address_t v6src_prefix; + u32 v6src_prefixlen; + u8 nhtype; + + if (unformat (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype ipv4", + unformat_ip6_address, &sr_prefix, &sr_prefixlen, + unformat_ip6_address, &v6src_prefix, &v6src_prefixlen)) + { + nhtype = SRV6_NHTYPE_IPV4; + } + else + if (unformat + (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype ipv6", + unformat_ip6_address, &sr_prefix, &sr_prefixlen, + unformat_ip6_address, &v6src_prefix, &v6src_prefixlen)) + { + nhtype = SRV6_NHTYPE_IPV6; + } + else + if (unformat + (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype non-ip", + unformat_ip6_address, &sr_prefix, &sr_prefixlen, + unformat_ip6_address, &v6src_prefix, &v6src_prefixlen)) + { + nhtype = SRV6_NHTYPE_NON_IP; + } + else if (unformat (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d", + unformat_ip6_address, &sr_prefix, &sr_prefixlen, + unformat_ip6_address, &v6src_prefix, &v6src_prefixlen)) + { + nhtype = SRV6_NHTYPE_NONE; + } + else + { + return 0; + } + + ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1); + clib_memset (ls_mem, 0, sizeof *ls_mem); + *plugin_mem_p = ls_mem; + + ls_mem->sr_prefix = sr_prefix; + ls_mem->sr_prefixlen = sr_prefixlen; + + ls_mem->v6src_prefix = v6src_prefix; + ls_mem->v6src_prefixlen = v6src_prefixlen; + + ls_mem->nhtype = nhtype; + + return 1; +} + +static int +clb_creation_srv6_t_m_gtp4_d (ip6_sr_policy_t * sr_policy) +{ + return 0; +} + +static int +clb_removal_srv6_t_m_gtp4_d (ip6_sr_policy_t * sr_policy) +{ + srv6_end_gtp4_param_t *ls_mem; + + ls_mem = (srv6_end_gtp4_param_t *) sr_policy->plugin_mem; + + clib_mem_free (ls_mem); + + return 0; +} + +static clib_error_t * +srv6_t_m_gtp4_d_init (vlib_main_t * vm) +{ + srv6_t_main_v4_decap_t *sm = &srv6_t_main_v4_decap; + ip6_header_t *ip6; + dpo_type_t dpo_type; + vlib_node_t *node; + u32 rc; + + sm->vlib_main = vm; + sm->vnet_main = vnet_get_main (); + + node = vlib_get_node_by_name (vm, (u8 *) "srv6-t-m-gtp4-d"); + sm->t_m_gtp4_d_node_index = node->index; + + node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); + sm->error_node_index = node->index; + + ip6 = &sm->cache_hdr; + + clib_memset_u8 (ip6, 0, sizeof (ip6_header_t)); + + // IPv6 header (default) + ip6->ip_version_traffic_class_and_flow_label = 0x60; + ip6->hop_limit = 64; + ip6->protocol = IP_PROTOCOL_IPV6; + + dpo_type = dpo_register_new_type (&dpo_vft, dpo_nodes); + + rc = sr_policy_register_function (vm, fn_name, keyword_str, def_str, param_str, 128, //prefix len + &dpo_type, + clb_format_srv6_t_m_gtp4_d, + clb_unformat_srv6_t_m_gtp4_d, + clb_creation_srv6_t_m_gtp4_d, + clb_removal_srv6_t_m_gtp4_d); + if (rc < 0) + clib_error_return (0, "SRv6 Transit GTP4.D Policy function" + "couldn't be registered"); + return 0; +} + +/* *INDENT-OFF* */ +VNET_FEATURE_INIT (srv6_t_m_gtp4_d, static) = +{ + .arc_name = "ip4-unicast", + .node_name = "srv6-t-m-gtp4-d", + .runs_before = 0, +}; + +VLIB_INIT_FUNCTION (srv6_t_m_gtp4_d_init); +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/srv6-mobile/mobile.h b/src/plugins/srv6-mobile/mobile.h index 8be9dc2b944..f1de99a306e 100644 --- a/src/plugins/srv6-mobile/mobile.h +++ b/src/plugins/srv6-mobile/mobile.h @@ -37,6 +37,11 @@ #define IP_PROTOCOL_NONE 59 #endif +#define SRV6_GTP6_UNKNOW 0 +#define SRV6_GTP6_DT4 1 +#define SRV6_GTP6_DT6 2 +#define SRV6_GTP6_DT46 3 + #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #define BITALIGN2(A,B) A; B #define BITALIGN3(A,B,C) A; B; C @@ -175,19 +180,19 @@ typedef struct srv6_end_main_v4_s } srv6_end_main_v4_t; -typedef struct srv6_end_main_v4_decap_s +typedef struct srv6_t_main_v4_decap_s { vlib_main_t *vlib_main; vnet_main_t *vnet_main; - u32 end_m_gtp4_d_node_index; + u32 t_m_gtp4_d_node_index; u32 error_node_index; ip6_header_t cache_hdr; -} srv6_end_main_v4_decap_t; +} srv6_t_main_v4_decap_t; extern srv6_end_main_v4_t srv6_end_main_v4; -extern srv6_end_main_v4_decap_t srv6_end_main_v4_decap; +extern srv6_t_main_v4_decap_t srv6_t_main_v4_decap; extern vlib_node_registration_t srv6_end_m_gtp4_e; typedef struct srv6_end_main_v6_s diff --git a/src/plugins/srv6-mobile/node.c b/src/plugins/srv6-mobile/node.c index 0c2c04e612e..eb9a20e3c53 100644 --- a/src/plugins/srv6-mobile/node.c +++ b/src/plugins/srv6-mobile/node.c @@ -113,9 +113,9 @@ format_srv6_end_rewrite_trace6 (u8 * s, va_list * args) _(M_GTP4_E_PACKETS, "srv6 End.M.GTP4.E packets") \ _(M_GTP4_E_BAD_PACKETS, "srv6 End.M.GTP4.E bad packets") -#define foreach_srv6_end_v4_d_error \ - _(M_GTP4_D_PACKETS, "srv6 End.M.GTP4.D packets") \ - _(M_GTP4_D_BAD_PACKETS, "srv6 End.M.GTP4.D bad packets") +#define foreach_srv6_t_v4_d_error \ + _(M_GTP4_D_PACKETS, "srv6 T.M.GTP4.D packets") \ + _(M_GTP4_D_BAD_PACKETS, "srv6 T.M.GTP4.D bad packets") #define foreach_srv6_end_v6_e_error \ _(M_GTP6_E_PACKETS, "srv6 End.M.GTP6.E packets") \ @@ -129,6 +129,10 @@ format_srv6_end_rewrite_trace6 (u8 * s, va_list * args) _(M_GTP6_D_DI_PACKETS, "srv6 End.M.GTP6.D.DI packets") \ _(M_GTP6_D_DI_BAD_PACKETS, "srv6 End.M.GTP6.D.DI bad packets") +#define foreach_srv6_end_v6_dt_error \ + _(M_GTP6_DT_PACKETS, "srv6 End.M.GTP6.DT packets") \ + _(M_GTP6_DT_BAD_PACKETS, "srv6 End.M.GTP6.DT bad packets") + typedef enum { #define _(sym,str) SRV6_END_ERROR_##sym, @@ -139,11 +143,11 @@ typedef enum typedef enum { -#define _(sym,str) SRV6_END_ERROR_##sym, - foreach_srv6_end_v4_d_error +#define _(sym,str) SRV6_T_ERROR_##sym, + foreach_srv6_t_v4_d_error #undef _ - SRV6_END_N_V4_D_ERROR, -} srv6_end_error_v4_d_t; + SRV6_T_N_V4_D_ERROR, +} srv6_t_error_v4_d_t; typedef enum { @@ -169,12 +173,26 @@ typedef enum SRV6_END_N_V6_D_DI_ERROR, } srv6_end_error_v6_d_di_t; +typedef enum +{ +#define _(sym,str) SRV6_END_ERROR_##sym, + foreach_srv6_end_v6_dt_error +#undef _ + SRV6_END_N_V6_DT_ERROR, +} srv6_end_error_v6_dt_t; + static char *srv6_end_error_v4_strings[] = { #define _(sym,string) string, foreach_srv6_end_v4_error #undef _ }; +static char *srv6_t_error_v4_d_strings[] = { +#define _(sym,string) string, + foreach_srv6_t_v4_d_error +#undef _ +}; + static char *srv6_end_error_v6_e_strings[] = { #define _(sym,string) string, foreach_srv6_end_v6_e_error @@ -202,6 +220,13 @@ typedef enum typedef enum { + SRV6_T_M_GTP4_D_NEXT_DROP, + SRV6_T_M_GTP4_D_NEXT_LOOKUP, + SRV6_T_M_GTP4_D_N_NEXT, +} srv6_T_m_gtp4_d_next_t; + +typedef enum +{ SRV6_END_M_GTP6_E_NEXT_DROP, SRV6_END_M_GTP6_E_NEXT_LOOKUP, SRV6_END_M_GTP6_E_N_NEXT, @@ -514,6 +539,419 @@ VLIB_NODE_FN (srv6_end_m_gtp4_e) (vlib_main_t * vm, return frame->n_vectors; } +// Function for SRv6 GTP4.D function. +VLIB_NODE_FN (srv6_t_m_gtp4_d) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + srv6_t_main_v4_decap_t *sm = &srv6_t_main_v4_decap; + ip6_sr_main_t *sm2 = &sr_main; + u32 n_left_from, next_index, *from, *to_next; + + u32 good_n = 0, bad_n = 0; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t *b0; + ip6_sr_sl_t *sl0; + srv6_end_gtp4_param_t *ls_param; + ip4_header_t *ip4; + + uword len0; + + u32 next0 = SRV6_T_M_GTP4_D_NEXT_LOOKUP; + + // defaults + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + + sl0 = + pool_elt_at_index (sm2->sid_lists, + vnet_buffer (b0)->ip.adj_index[VLIB_TX]); + + ls_param = (srv6_end_gtp4_param_t *) sl0->plugin_mem; + + len0 = vlib_buffer_length_in_chain (vm, b0); + + ip4 = vlib_buffer_get_current (b0); + + if (ip4->protocol != IP_PROTOCOL_UDP + || len0 < sizeof (ip4_gtpu_header_t)) + { + next0 = SRV6_T_M_GTP4_D_NEXT_DROP; + + bad_n++; + } + else + { + uword *p; + ip6_sr_policy_t *sr_policy = NULL; + ip6_sr_sl_t *sl = NULL; + u32 *sl_index; + u32 hdr_len; + + ip4_gtpu_header_t *hdr; + ip4_address_t src, dst; + u8 *srcp, *dstp; + ip6_header_t *encap = NULL; + ip6_address_t seg; + ip6_address_t src6; + u8 gtpu_type; + u32 teid; + u8 *teidp; + u8 qfi = 0; + u8 *qfip = NULL; + u32 offset, shift, index; + ip6srv_combo_header_t *ip6srv; + gtpu_pdu_session_t *sess = NULL; + + // Decap from GTP-U. + hdr = (ip4_gtpu_header_t *) ip4; + + hdr_len = sizeof (ip4_gtpu_header_t); + + teid = hdr->gtpu.teid; + teidp = (u8 *) & teid; + + gtpu_type = hdr->gtpu.type; + + if (hdr->gtpu.ver_flags & GTPU_EXTHDR_FLAG) + { + // Extention header. + hdr_len += sizeof (gtpu_exthdr_t); + if (hdr->gtpu.ext->nextexthdr == GTPU_EXTHDR_PDU_SESSION) + { + // PDU Session Container. + sess = + (gtpu_pdu_session_t *) (((char *) hdr) + hdr_len); + qfi = sess->u.val & ~GTPU_PDU_SESSION_P_BIT_MASK; + qfip = (u8 *) & qfi; + + hdr_len += sizeof (gtpu_pdu_session_t); + + if (sess->u.val & GTPU_PDU_SESSION_P_BIT_MASK) + { + hdr_len += sizeof (gtpu_paging_policy_t); + } + } + } + + src = hdr->ip4.src_address; + srcp = (u8 *) & src; + + dst = hdr->ip4.dst_address; + dstp = (u8 *) & dst; + + seg = ls_param->sr_prefix; + + offset = ls_param->sr_prefixlen / 8; + shift = ls_param->sr_prefixlen % 8; + + if (PREDICT_TRUE (shift == 0)) + { + clib_memcpy_fast (&seg.as_u8[offset], dstp, 4); + + if (qfip) + { + qfi = + ((qfi & GTPU_PDU_SESSION_QFI_MASK) << 2) | + ((qfi & GTPU_PDU_SESSION_R_BIT_MASK) >> 5); + + if (sess->type) + { + qfi |= SRV6_PDU_SESSION_U_BIT_MASK; + } + + seg.as_u8[offset + 4] = qfi; + } + + clib_memcpy_fast (&seg.as_u8[offset + 5], teidp, 4); + } + else + { + for (index = 0; index < 4; index++) + { + seg.as_u8[offset + index] |= dstp[index] >> shift; + seg.as_u8[offset + index + 1] |= + dstp[index] << (8 - shift); + + seg.as_u8[offset + index + 5] |= teidp[index] >> shift; + seg.as_u8[offset + index + 6] |= + teidp[index] << (8 - shift); + } + + if (qfip) + { + qfi = + ((qfi & GTPU_PDU_SESSION_QFI_MASK) << 2) | + ((qfi & GTPU_PDU_SESSION_R_BIT_MASK) >> 5); + + if (sess->type) + { + qfi |= SRV6_PDU_SESSION_U_BIT_MASK; + } + + seg.as_u8[offset + 4] |= qfi >> shift; + seg.as_u8[offset + 5] |= qfi << (8 - shift); + } + } + + src6 = ls_param->v6src_prefix; + + offset = ls_param->v6src_prefixlen / 8; + shift = ls_param->v6src_prefixlen % 8; + + if (PREDICT_TRUE (shift == 0)) + { + clib_memcpy_fast (&src6.as_u8[offset], srcp, 4); + } + else + { + for (index = 0; index < 4; index++) + { + src6.as_u8[offset + index] |= srcp[offset] >> shift; + src6.as_u8[offset + index + 1] |= + srcp[offset] << (8 - shift); + } + } + + vlib_buffer_advance (b0, (word) hdr_len); + + // Encap to SRv6. + if (PREDICT_TRUE (gtpu_type == GTPU_TYPE_GTPU)) + { + encap = vlib_buffer_get_current (b0); + } + + len0 = vlib_buffer_length_in_chain (vm, b0); + + p = + mhash_get (&sm2->sr_policies_index_hash, + &ls_param->sr_prefix); + if (p) + { + sr_policy = pool_elt_at_index (sm2->sr_policies, p[0]); + } + + if (sr_policy) + { + vec_foreach (sl_index, sr_policy->segments_lists) + { + sl = pool_elt_at_index (sm2->sid_lists, *sl_index); + if (sl != NULL) + break; + } + } + + if (sl) + { + hdr_len = sizeof (ip6srv_combo_header_t); + hdr_len += vec_len (sl->segments) * sizeof (ip6_address_t); + hdr_len += sizeof (ip6_address_t); + } + else + { + hdr_len = sizeof (ip6_header_t); + + if (PREDICT_FALSE (gtpu_type != GTPU_TYPE_GTPU)) + { + hdr_len += sizeof (ip6_sr_header_t); + hdr_len += sizeof (ip6_address_t); + } + } + + vlib_buffer_advance (b0, -(word) hdr_len); + ip6srv = vlib_buffer_get_current (b0); + + if (sl) + { + clib_memcpy_fast (ip6srv, sl->rewrite, + vec_len (sl->rewrite)); + + ip6srv->ip.protocol = IP_PROTOCOL_IPV6_ROUTE; + + ip6srv->sr.tag = + clib_host_to_net_u16 (srh_tagfield[gtpu_type]); + + ip6srv->sr.segments_left += 1; + ip6srv->sr.last_entry += 1; + + ip6srv->sr.length += sizeof (ip6_address_t) / 8; + ip6srv->sr.segments[0] = seg; + + if (PREDICT_TRUE (encap != NULL)) + { + if (ls_param->nhtype == SRV6_NHTYPE_NONE) + { + if ((clib_net_to_host_u32 + (encap->ip_version_traffic_class_and_flow_label) + >> 28) == 6) + ip6srv->sr.protocol = IP_PROTOCOL_IPV6; + else + ip6srv->sr.protocol = IP_PROTOCOL_IP_IN_IP; + } + else if (ls_param->nhtype == SRV6_NHTYPE_IPV4) + { + ip6srv->sr.protocol = IP_PROTOCOL_IP_IN_IP; + if ((clib_net_to_host_u32 + (encap->ip_version_traffic_class_and_flow_label) + >> 28) != 4) + { + // Bad encap packet. + next0 = SRV6_T_M_GTP4_D_NEXT_DROP; + bad_n++; + goto DONE; + } + } + else if (ls_param->nhtype == SRV6_NHTYPE_IPV6) + { + ip6srv->sr.protocol = IP_PROTOCOL_IPV6; + if ((clib_net_to_host_u32 + (encap->ip_version_traffic_class_and_flow_label) + >> 28) != 6) + { + // Bad encap packet. + next0 = SRV6_T_M_GTP4_D_NEXT_DROP; + bad_n++; + goto DONE; + } + } + else if (ls_param->nhtype == SRV6_NHTYPE_NON_IP) + { + ip6srv->sr.protocol = IP_PROTOCOL_NONE; + } + } + else + { + ip6srv->sr.protocol = IP_PROTOCOL_NONE; + } + + clib_memcpy_fast (&ip6srv->sr.segments[1], + (u8 *) (sl->rewrite + + sizeof (ip6_header_t) + + sizeof (ip6_sr_header_t)), + vec_len (sl->segments) * + sizeof (ip6_address_t)); + } + else + { + clib_memcpy_fast (ip6srv, &sm->cache_hdr, + sizeof (ip6_header_t)); + + ip6srv->ip.dst_address = seg; + + if (PREDICT_FALSE (gtpu_type != GTPU_TYPE_GTPU)) + { + ip6srv->ip.protocol = IP_PROTOCOL_IPV6_ROUTE; + + ip6srv->sr.protocol = IP_PROTOCOL_NONE; + + ip6srv->sr.tag = + clib_host_to_net_u16 (srh_tagfield[gtpu_type]); + + ip6srv->sr.segments_left = 0; + ip6srv->sr.last_entry = 0; + + ip6srv->sr.length = sizeof (ip6_address_t) / 8; + ip6srv->sr.segments[0] = seg; + } + else + { + if (ls_param->nhtype == SRV6_NHTYPE_NONE) + { + if ((clib_net_to_host_u32 + (encap->ip_version_traffic_class_and_flow_label) + >> 28) == 6) + ip6srv->ip.protocol = IP_PROTOCOL_IPV6; + else + ip6srv->ip.protocol = IP_PROTOCOL_IP_IN_IP; + } + else if (ls_param->nhtype == SRV6_NHTYPE_IPV4) + { + ip6srv->ip.protocol = IP_PROTOCOL_IP_IN_IP; + if ((clib_net_to_host_u32 + (encap->ip_version_traffic_class_and_flow_label) + >> 28) != 4) + { + // Bad encap packet. + next0 = SRV6_T_M_GTP4_D_NEXT_DROP; + bad_n++; + goto DONE; + } + } + else if (ls_param->nhtype == SRV6_NHTYPE_IPV6) + { + ip6srv->ip.protocol = IP_PROTOCOL_IPV6; + if ((clib_net_to_host_u32 + (encap->ip_version_traffic_class_and_flow_label) + >> 28) != 6) + { + // Bad encap packet. + next0 = SRV6_T_M_GTP4_D_NEXT_DROP; + bad_n++; + goto DONE; + } + } + else if (ls_param->nhtype == SRV6_NHTYPE_NON_IP) + { + ip6srv->ip.protocol = IP_PROTOCOL_NONE; + } + } + } + + ip6srv->ip.src_address = src6; + + ip6srv->ip.payload_length = + clib_host_to_net_u16 (len0 + hdr_len - sizeof (ip6_header_t)); + + good_n++; + + if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) && + PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) + { + srv6_end_rewrite_trace_t *tr = + vlib_add_trace (vm, node, b0, sizeof (*tr)); + clib_memcpy (tr->src.as_u8, ip6srv->ip.src_address.as_u8, + sizeof (tr->src.as_u8)); + clib_memcpy (tr->dst.as_u8, ip6srv->ip.dst_address.as_u8, + sizeof (tr->dst.as_u8)); + } + } + + DONE: + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, + n_left_to_next, bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + vlib_node_increment_counter (vm, sm->t_m_gtp4_d_node_index, + SRV6_T_ERROR_M_GTP4_D_BAD_PACKETS, bad_n); + + vlib_node_increment_counter (vm, sm->t_m_gtp4_d_node_index, + SRV6_T_ERROR_M_GTP4_D_PACKETS, good_n); + + return frame->n_vectors; +} + VLIB_REGISTER_NODE (srv6_end_m_gtp4_e) = { .name = "srv6-end-m-gtp4-e",.vector_size = sizeof (u32),.format_trace = @@ -522,8 +960,20 @@ VLIB_REGISTER_NODE (srv6_end_m_gtp4_e) = srv6_end_error_v4_strings,.n_next_nodes = SRV6_END_M_GTP4_E_N_NEXT,.next_nodes = { - [SRV6_END_M_GTP4_E_NEXT_DROP] = - "error-drop",[SRV6_END_M_GTP4_E_NEXT_LOOKUP] = "ip4-lookup",} + [SRV6_END_M_GTP4_E_NEXT_DROP] = "error-drop", + [SRV6_END_M_GTP4_E_NEXT_LOOKUP] = "ip4-lookup",} +,}; + +VLIB_REGISTER_NODE (srv6_t_m_gtp4_d) = +{ + .name = "srv6-t-m-gtp4-d",.vector_size = sizeof (u32),.format_trace = + format_srv6_end_rewrite_trace,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors = + ARRAY_LEN (srv6_t_error_v4_d_strings),.error_strings = + srv6_t_error_v4_d_strings,.n_next_nodes = + SRV6_T_M_GTP4_D_N_NEXT,.next_nodes = + { + [SRV6_T_M_GTP4_D_NEXT_DROP] = "error-drop", + [SRV6_T_M_GTP4_D_NEXT_LOOKUP] = "ip6-lookup",} ,}; // Function for SRv6 GTP6.E function @@ -1496,8 +1946,8 @@ VLIB_REGISTER_NODE (srv6_end_m_gtp6_e) = srv6_end_error_v6_e_strings,.n_next_nodes = SRV6_END_M_GTP6_E_N_NEXT,.next_nodes = { - [SRV6_END_M_GTP6_E_NEXT_DROP] = - "error-drop",[SRV6_END_M_GTP6_E_NEXT_LOOKUP] = "ip6-lookup",} + [SRV6_END_M_GTP6_E_NEXT_DROP] = "error-drop", + [SRV6_END_M_GTP6_E_NEXT_LOOKUP] = "ip6-lookup",} ,}; VLIB_REGISTER_NODE (srv6_end_m_gtp6_d) = @@ -1508,8 +1958,8 @@ VLIB_REGISTER_NODE (srv6_end_m_gtp6_d) = srv6_end_error_v6_d_strings,.n_next_nodes = SRV6_END_M_GTP6_D_N_NEXT,.next_nodes = { - [SRV6_END_M_GTP6_D_NEXT_DROP] = - "error-drop",[SRV6_END_M_GTP6_D_NEXT_LOOKUP] = "ip6-lookup",} + [SRV6_END_M_GTP6_D_NEXT_DROP] = "error-drop", + [SRV6_END_M_GTP6_D_NEXT_LOOKUP] = "ip6-lookup",} ,}; VLIB_REGISTER_NODE (srv6_end_m_gtp6_d_di) = diff --git a/src/plugins/srv6-mobile/test/test_srv6_end.py b/src/plugins/srv6-mobile/test/test_srv6_end.py index 4e2a5765bab..9eb62c9ce33 100644 --- a/src/plugins/srv6-mobile/test/test_srv6_end.py +++ b/src/plugins/srv6-mobile/test/test_srv6_end.py @@ -59,8 +59,7 @@ class TestSRv6EndMGTP4E(VppTestCase): IPv6ExtHdrSegmentRouting() / IPv6(dst=d, src=s) / UDP(sport=1000, dport=23)) - - self.logger.info(pkt.show2()) + self.logger.info(pkt.show2(dump=True)) pkts.append(pkt) return pkts @@ -86,7 +85,7 @@ class TestSRv6EndMGTP4E(VppTestCase): capture = self.pg1.get_capture(len(pkts)) for pkt in capture: - self.logger.info(pkt.show2()) + self.logger.info(pkt.show2(dump=True)) self.assertEqual(pkt[IP].dst, self.ip4_dst) self.assertEqual(pkt[IP].src, self.ip4_src) self.assertEqual(pkt[GTP_U_Header].teid, 0xbbbbbbbb) diff --git a/src/vnet/srv6/sr.h b/src/vnet/srv6/sr.h index 11935833394..6a228c8fc23 100755 --- a/src/vnet/srv6/sr.h +++ b/src/vnet/srv6/sr.h @@ -77,6 +77,9 @@ typedef struct dpo_id_t bsid_dpo; /**< DPO for Encaps/Insert for BSID */ dpo_id_t ip6_dpo; /**< DPO for Encaps/Insert IPv6 */ dpo_id_t ip4_dpo; /**< DPO for Encaps IPv6 */ + + u16 plugin; + void *plugin_mem; } ip6_sr_sl_t; /* SR policy types */ @@ -102,8 +105,13 @@ typedef struct u32 fib_table; /**< FIB table */ u8 is_encap; /**< Mode (0 is SRH insert, 1 Encaps) */ + + u16 plugin; + void *plugin_mem; } ip6_sr_policy_t; +typedef int (sr_p_plugin_callback_t) (ip6_sr_policy_t * sr); + /** * @brief SR LocalSID */ @@ -165,6 +173,34 @@ typedef struct } sr_localsid_fn_registration_t; /** + * @brief SR Policy behavior registration + */ +typedef struct +{ + u16 sr_policy_function_number; /**< SR Policy plugin function */ + + u8 *function_name; /**< Function name. (key). */ + + u8 *keyword_str; /**< Behavior keyword (i.e. End.X) */ + + u8 *def_str; /**< Behavior definition (i.e. Endpoint with cross-connect) */ + + u8 *params_str; /**< Behavior parameters (i.e. <oif> <IP46next_hop>) */ + + u8 prefix_length; + + dpo_type_t dpo; /**< DPO type registration */ + + format_function_t *ls_format; /**< LocalSID format function */ + + unformat_function_t *ls_unformat; /**< LocalSID unformat function */ + + sr_p_plugin_callback_t *creation; /**< Function within plugin that will be called after localsid creation*/ + + sr_p_plugin_callback_t *removal; /**< Function within plugin that will be called before localsid removal */ +} sr_policy_fn_registration_t; + +/** * @brief Steering db key * * L3 is IPv4/IPv6 + mask @@ -236,6 +272,12 @@ typedef struct /* Find plugin function by name */ uword *plugin_functions_by_key; + /* Plugin functions for Policy */ + sr_policy_fn_registration_t *policy_plugin_functions; + + /* Find plugin function by name */ + uword *policy_plugin_functions_by_key; + /* Counters */ vlib_combined_counter_main_t sr_ls_valid_counters; vlib_combined_counter_main_t sr_ls_invalid_counters; @@ -270,8 +312,19 @@ sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name, sr_plugin_callback_t * removal_fn); extern int +sr_policy_register_function (vlib_main_t * vm, u8 * fn_name, + u8 * keyword_str, u8 * def_str, + u8 * params_str, u8 prefix_length, + dpo_type_t * dpo, + format_function_t * ls_format, + unformat_function_t * ls_unformat, + sr_p_plugin_callback_t * creation_fn, + sr_p_plugin_callback_t * removal_fn); + +extern int sr_policy_add (ip6_address_t * bsid, ip6_address_t * segments, - u32 weight, u8 behavior, u32 fib_table, u8 is_encap); + u32 weight, u8 behavior, u32 fib_table, u8 is_encap, + u16 plugin, void *plugin_mem); extern int sr_policy_mod (ip6_address_t * bsid, u32 index, u32 fib_table, u8 operation, ip6_address_t * segments, u32 sl_index, u32 weight); diff --git a/src/vnet/srv6/sr_api.c b/src/vnet/srv6/sr_api.c index 940336d9859..5a7d8c5a2ea 100644 --- a/src/vnet/srv6/sr_api.c +++ b/src/vnet/srv6/sr_api.c @@ -108,13 +108,14 @@ vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp) /* * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments, - * u32 weight, u8 behavior, u32 fib_table, u8 is_encap) + * u32 weight, u8 behavior, u32 fib_table, u8 is_encap, + * u16 behavior, void *plugin_mem) */ int rv = 0; rv = sr_policy_add ((ip6_address_t *) & mp->bsid_addr, segments, ntohl (mp->sids.weight), - mp->type, ntohl (mp->fib_table), mp->is_encap); + mp->type, ntohl (mp->fib_table), mp->is_encap, 0, NULL); vec_free (segments); REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY); diff --git a/src/vnet/srv6/sr_policy_rewrite.c b/src/vnet/srv6/sr_policy_rewrite.c index 8d11da67ddf..9c4d0ec493f 100755 --- a/src/vnet/srv6/sr_policy_rewrite.c +++ b/src/vnet/srv6/sr_policy_rewrite.c @@ -340,6 +340,7 @@ create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight, { ip6_sr_main_t *sm = &sr_main; ip6_sr_sl_t *segment_list; + sr_policy_fn_registration_t *plugin = 0; pool_get (sm->sid_lists, segment_list); clib_memset (segment_list, 0, sizeof (*segment_list)); @@ -349,6 +350,7 @@ create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight, /* Fill in segment list */ segment_list->weight = (weight != (u32) ~ 0 ? weight : SR_SEGMENT_LIST_WEIGHT_DEFAULT); + segment_list->segments = vec_dup (sl); if (is_encap) @@ -362,6 +364,18 @@ create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight, segment_list->rewrite_bsid = compute_rewrite_bsid (sl); } + if (sr_policy->plugin) + { + plugin = + pool_elt_at_index (sm->policy_plugin_functions, + sr_policy->plugin - SR_BEHAVIOR_LAST); + + segment_list->plugin = sr_policy->plugin; + segment_list->plugin_mem = sr_policy->plugin_mem; + + plugin->creation (sr_policy); + } + /* Create DPO */ dpo_reset (&segment_list->bsid_dpo); dpo_reset (&segment_list->ip6_dpo); @@ -369,19 +383,41 @@ create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight, if (is_encap) { - dpo_set (&segment_list->ip6_dpo, sr_pr_encaps_dpo_type, DPO_PROTO_IP6, - segment_list - sm->sid_lists); - dpo_set (&segment_list->ip4_dpo, sr_pr_encaps_dpo_type, DPO_PROTO_IP4, - segment_list - sm->sid_lists); - dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_encaps_dpo_type, - DPO_PROTO_IP6, segment_list - sm->sid_lists); + if (!sr_policy->plugin) + { + dpo_set (&segment_list->ip6_dpo, sr_pr_encaps_dpo_type, + DPO_PROTO_IP6, segment_list - sm->sid_lists); + dpo_set (&segment_list->ip4_dpo, sr_pr_encaps_dpo_type, + DPO_PROTO_IP4, segment_list - sm->sid_lists); + dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_encaps_dpo_type, + DPO_PROTO_IP6, segment_list - sm->sid_lists); + } + else + { + dpo_set (&segment_list->ip6_dpo, plugin->dpo, DPO_PROTO_IP6, + segment_list - sm->sid_lists); + dpo_set (&segment_list->ip4_dpo, plugin->dpo, DPO_PROTO_IP4, + segment_list - sm->sid_lists); + dpo_set (&segment_list->bsid_dpo, plugin->dpo, DPO_PROTO_IP6, + segment_list - sm->sid_lists); + } } else { - dpo_set (&segment_list->ip6_dpo, sr_pr_insert_dpo_type, DPO_PROTO_IP6, - segment_list - sm->sid_lists); - dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_insert_dpo_type, - DPO_PROTO_IP6, segment_list - sm->sid_lists); + if (!sr_policy->plugin) + { + dpo_set (&segment_list->ip6_dpo, sr_pr_insert_dpo_type, + DPO_PROTO_IP6, segment_list - sm->sid_lists); + dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_insert_dpo_type, + DPO_PROTO_IP6, segment_list - sm->sid_lists); + } + else + { + dpo_set (&segment_list->ip6_dpo, plugin->dpo, DPO_PROTO_IP6, + segment_list - sm->sid_lists); + dpo_set (&segment_list->bsid_dpo, plugin->dpo, DPO_PROTO_IP6, + segment_list - sm->sid_lists); + } } return segment_list; @@ -454,7 +490,6 @@ update_lb (ip6_sr_policy_t * sr_policy) } /* Create the LB path vector */ - //path_vector = vec_new(load_balance_path_t, vec_len(sr_policy->segments_lists)); vec_foreach (sl_index, sr_policy->segments_lists) { segment_list = pool_elt_at_index (sm->sid_lists, *sl_index); @@ -483,7 +518,6 @@ update_lb (ip6_sr_policy_t * sr_policy) vec_free (b_path_vector); vec_free (ip6_path_vector); vec_free (ip4_path_vector); - } /** @@ -590,7 +624,8 @@ update_replicate (ip6_sr_policy_t * sr_policy) */ int sr_policy_add (ip6_address_t * bsid, ip6_address_t * segments, - u32 weight, u8 behavior, u32 fib_table, u8 is_encap) + u32 weight, u8 behavior, u32 fib_table, u8 is_encap, + u16 plugin, void *ls_plugin_mem) { ip6_sr_main_t *sm = &sr_main; ip6_sr_policy_t *sr_policy = 0; @@ -635,6 +670,12 @@ sr_policy_add (ip6_address_t * bsid, ip6_address_t * segments, sr_policy->fib_table = (fib_table != (u32) ~ 0 ? fib_table : 0); //Is default FIB 0 ? sr_policy->is_encap = is_encap; + if (plugin) + { + sr_policy->plugin = plugin; + sr_policy->plugin_mem = ls_plugin_mem; + } + /* Copy the key */ mhash_set (&sm->sr_policies_index_hash, bsid, sr_policy - sm->sr_policies, NULL); @@ -730,6 +771,19 @@ sr_policy_del (ip6_address_t * bsid, u32 index) pool_put_index (sm->sid_lists, *sl_index); } + if (sr_policy->plugin) + { + sr_policy_fn_registration_t *plugin = 0; + + plugin = + pool_elt_at_index (sm->policy_plugin_functions, + sr_policy->plugin - SR_BEHAVIOR_LAST); + + plugin->removal (sr_policy); + sr_policy->plugin = 0; + sr_policy->plugin_mem = NULL; + } + /* Remove SR policy entry */ mhash_unset (&sm->sr_policies_index_hash, &sr_policy->bsid, NULL); pool_put (sm->sr_policies, sr_policy); @@ -863,6 +917,7 @@ static clib_error_t * sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { + ip6_sr_main_t *sm = &sr_main; int rv = -1; char is_del = 0, is_add = 0, is_mod = 0; char policy_set = 0; @@ -873,6 +928,8 @@ sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, u8 operation = 0; char is_encap = 1; char is_spray = 0; + u16 behavior = 0; + void *ls_plugin_mem = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { @@ -910,6 +967,33 @@ sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, is_encap = 0; else if (unformat (input, "spray")) is_spray = 1; + else if (!behavior && unformat (input, "behavior")) + { + sr_policy_fn_registration_t *plugin = 0, **vec_plugins = 0; + sr_policy_fn_registration_t **plugin_it = 0; + + /* *INDENT-OFF* */ + pool_foreach (plugin, sm->policy_plugin_functions, + { + vec_add1 (vec_plugins, plugin); + }); + /* *INDENT-ON* */ + + vec_foreach (plugin_it, vec_plugins) + { + if (unformat + (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem)) + { + behavior = (*plugin_it)->sr_policy_function_number; + break; + } + } + + if (!behavior) + { + return clib_error_return (0, "Invalid behavior"); + } + } else break; } @@ -922,11 +1006,20 @@ sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, if (is_add) { + if (behavior && vec_len (segments) == 0) + { + vec_add2 (segments, this_seg, 1); + clib_memset (this_seg, 0, sizeof (*this_seg)); + } + if (vec_len (segments) == 0) return clib_error_return (0, "No Segment List specified"); + rv = sr_policy_add (&bsid, segments, weight, (is_spray ? SR_POLICY_TYPE_SPRAY : - SR_POLICY_TYPE_DEFAULT), fib_table, is_encap); + SR_POLICY_TYPE_DEFAULT), fib_table, is_encap, + behavior, ls_plugin_mem); + vec_free (segments); } else if (is_del) @@ -942,10 +1035,13 @@ sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, return clib_error_return (0, "No Segment List specified"); if (operation == 3 && weight == (u32) ~ 0) return clib_error_return (0, "No new weight for the SL specified"); + rv = sr_policy_mod ((sr_policy_index != (u32) ~ 0 ? NULL : &bsid), sr_policy_index, fib_table, operation, segments, sl_index, weight); - vec_free (segments); + + if (segments) + vec_free (segments); } switch (rv) @@ -3200,6 +3296,96 @@ VLIB_REGISTER_NODE (sr_policy_rewrite_b_encaps_node) = { }; /* *INDENT-ON* */ +/*************************** SR Policy plugins ******************************/ +/** + * @brief SR Policy plugin registry + */ +int +sr_policy_register_function (vlib_main_t * vm, u8 * fn_name, + u8 * keyword_str, u8 * def_str, + u8 * params_str, u8 prefix_length, + dpo_type_t * dpo, + format_function_t * ls_format, + unformat_function_t * ls_unformat, + sr_p_plugin_callback_t * creation_fn, + sr_p_plugin_callback_t * removal_fn) +{ + ip6_sr_main_t *sm = &sr_main; + uword *p; + + sr_policy_fn_registration_t *plugin; + + /* Did this function exist? If so update it */ + p = hash_get_mem (sm->policy_plugin_functions_by_key, fn_name); + if (p) + { + plugin = pool_elt_at_index (sm->policy_plugin_functions, p[0]); + } + /* Else create a new one and set hash key */ + else + { + pool_get (sm->policy_plugin_functions, plugin); + hash_set_mem (sm->policy_plugin_functions_by_key, fn_name, + plugin - sm->policy_plugin_functions); + } + + clib_memset (plugin, 0, sizeof (*plugin)); + + plugin->sr_policy_function_number = (plugin - sm->policy_plugin_functions); + plugin->sr_policy_function_number += SR_BEHAVIOR_LAST; + plugin->prefix_length = prefix_length; + plugin->ls_format = ls_format; + plugin->ls_unformat = ls_unformat; + plugin->creation = creation_fn; + plugin->removal = removal_fn; + clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t)); + plugin->function_name = format (0, "%s%c", fn_name, 0); + plugin->keyword_str = format (0, "%s%c", keyword_str, 0); + plugin->def_str = format (0, "%s%c", def_str, 0); + plugin->params_str = format (0, "%s%c", params_str, 0); + + return plugin->sr_policy_function_number; +} + +/** + * @brief CLI function to 'show' all available SR LocalSID behaviors + */ +static clib_error_t * +show_sr_policy_behaviors_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + ip6_sr_main_t *sm = &sr_main; + sr_policy_fn_registration_t *plugin; + sr_policy_fn_registration_t **plugins_vec = 0; + int i; + + vlib_cli_output (vm, "SR Policy behaviors:\n-----------------------\n\n"); + + /* *INDENT-OFF* */ + pool_foreach (plugin, sm->policy_plugin_functions, + ({ vec_add1 (plugins_vec, plugin); })); + /* *INDENT-ON* */ + + vlib_cli_output (vm, "Plugin behaviors:\n"); + for (i = 0; i < vec_len (plugins_vec); i++) + { + plugin = plugins_vec[i]; + vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str, + plugin->def_str); + vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str); + } + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (show_sr_policy_behaviors_command, static) = { + .path = "show sr policy behaviors", + .short_help = "show sr policy behaviors", + .function = show_sr_policy_behaviors_command_fn, +}; +/* *INDENT-ON* */ + /*************************** SR Segment Lists DPOs ****************************/ static u8 * format_sr_segment_list_dpo (u8 * s, va_list * args) |