/*
* Copyright (c) 2016 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/session/application_interface.h>
#include <vnet/session/session.h>
#include <vlibmemory/api.h>
#include <vnet/dpo/load_balance.h>
/** @file
VPP's application/session API bind/unbind/connect/disconnect calls
*/
/*
* TLS server cert and keys to be used for testing only
*/
const char test_srv_crt_rsa[] =
"-----BEGIN CERTIFICATE-----\r\n"
"MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
"VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
"DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n"
"IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n"
"NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n"
"MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n"
"LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n"
"ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n"
"4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n"
"cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n"
"Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n"
"ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n"
"+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n"
"Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n"
"aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n"
"BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n"
"3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
"rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
"3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
"twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
"wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
"-----END CERTIFICATE-----\r\n";
const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
const char test_srv_key_rsa[] =
"-----BEGIN PRIVATE KEY-----\r\n"
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
"CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
"WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
"XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
"VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
"/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
"bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
"ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
"iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
"LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n"
"BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n"
"CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n"
"Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n"
"nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n"
"5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n"
"ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n"
"sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n"
"gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n"
"Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n"
"CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n"
"K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n"
"VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n"
"EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n"
"PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
"Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
"oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n";
const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
static u8
session_endpoint_is_local (session_endpoint_t * sep)
{
return (ip_is_zero (&sep->ip, sep->is_ip4)
|| ip_is_local_host (&sep->ip, sep->is_ip4));
}
static u8
session_endpoint_is_zero (session_endpoint_t * sep)
{
return ip_is_zero (&sep->ip, sep->is_ip4);
}
u8
session_endpoint_in_ns (session_endpoint_t * sep)
{
u8 is_lep = session_endpoint_is_local (sep);
if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
&& !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
{
clib_warning ("sw_if_index %u not configured with ip %U",
sep->sw_if_index, format_ip46_address, &sep->ip,
sep->is_ip4);
return 0;
}
return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
}
int
api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
{
session_manager_main_t *smm = vnet_get_session_manager_main ();
stream_session_t *pool;
*thread_index = handle & 0xFFFFFFFF;
*session_index = handle >> 32;
if (*thread_index >= vec_len (smm->sessions))
return VNET_API_ERROR_INVALID_VALUE;
pool = smm->sessions[*thread_index];
if (pool_is_free_index (pool, *session_index))
return VNET_API_ERROR_INVALID_VALUE_2;
return 0;
}
static void
session_endpoint_update_for_app (session_endpoint_t * sep,
application_t * app)
{
app_namespace_t *app_ns;
app_ns = app_namespace_get (app->ns_index);
if (app_ns)
{
/* Ask transport and network to bind to/connect using local interface
* that "supports" app's namespace. This will fix our local connection
* endpoint.
*/
sep->sw_if_index = app_ns->sw_if_index;
sep->fib_index =
sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
}
}
static int
vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
{
u64 lh, ll_handle = SESSION_INVALID_HANDLE;
application_t *app;
u32 table_index;
int rv;
app = application_get_if_valid (app_index);
if (!app)
{
SESSION_DBG ("app not attached");
return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
}
session_endpoint_update_for_app (sep, app);
if (!session_endpoint_in_ns (sep))
return VNET_API_ERROR_INVALID_VALUE_2;
table_index = application_session_table (app,
session_endpoint_fib_proto (sep));
lh = session_lookup_endpoint_listener (table_index, sep, 1);
if (lh != SESSION_INVALID_HANDLE)
return VNET_API_ERROR_ADDRESS_IN_USE;
/*
* Add session endpoint to local session table. Only binds to "inaddr_any"
* (i.e., zero address) are added to local scope table.
*/
if (application_has_local_scope (app) && session_endpoint_is_local (sep))
{
if ((rv = application_start_local_listen (app, sep, handle)))
return rv;
ll_handle = *handle;
}
if (!application_has_global_scope (app))
return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0);
/*
* Add session endpoint to global session table
*/
/* Setup listen path down to transport */
rv = application_start_listen (app, sep, handle);
if (rv && ll_handle != SESSION_INVALID_HANDLE)
session_lookup_del_session_endpoint (table_index, sep);
/*
* Store in local table listener the index of the transport layer
* listener. We'll need local listeners are hit and we need to
* return global handle
*/
if (ll_handle != SESSION_INVALID_HANDLE)
{
local_session_t *ll;
stream_session_t *tl;
ll = application_get_local_listener_w_handle (ll_handle);
tl = listen_session_get_from_handle (*handle);
ll->transport_listener_index = tl->session_index;
}
return rv;
}
int
vnet_unbind_i (u32 app_index, session_handle_t handle)
{
application_t *app;
int rv;
if (!(app = application_get_if_valid (app_index)))
{
SESSION_DBG ("app (%d) not attached", app_index);
return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
}
if (application_has_local_scope (app))
{
if ((rv = application_stop_local_listen (app, handle)))
return rv;
}
/*
* Clear the global scope table of the listener
*/
if (application_has_global_scope (app))
return application_stop_listen (app, handle);
return 0;
}
int
application_connect (u32 client_index, u32 api_context,
session_endpoint_t * sep)
{
application_t *server, *client;
u32 table_index, server_index, li;
stream_session_t *listener;
local_session_t *ll;
u64 lh;
if (session_endpoint_is_zero (sep))
return VNET_API_ERROR_INVALID_VALUE;
client = application_get (client_index);
session_endpoint_update_for_app (sep, client);
/*
* First check the local scope for locally attached destinations.
* If we have local scope, we pass *all* connects through it since we may
* have special policy rules even for non-local destinations, think proxy.
*/
if (application_has_local_scope (client))
{
table_index = application_local_session_table (client);
lh = session_lookup_local_endpoint (table_index, sep);
if (lh == SESSION_DROP_HANDLE)
return VNET_API_ERROR_APP_CONNECT_FILTERED;
if (lh == SESSION_INVALID_HANDLE)
@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 */
}
/*
* Copyright (c) 2016 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 <vlib/vlib.h>
#include <vnet/ip/format.h>
#include <vnet/ip/lookup.h>
#include <vnet/adj/adj.h>
#include <vnet/dpo/load_balance.h>
#include <vnet/dpo/drop_dpo.h>
#include <vnet/fib/fib_entry.h>
#include <vnet/fib/fib_walk.h>
#include <vnet/fib/fib_entry_src.h>
#include <vnet/fib/fib_entry_cover.h>
#include <vnet/fib/fib_table.h>
#include <vnet/fib/fib_internal.h>
#include <vnet/fib/fib_attached_export.h>
#include <vnet/fib/fib_path_ext.h>
#include <vnet/fib/fib_entry_delegate.h>
#include <vnet/fib/fib_entry_track.h>
/*
* Array of strings/names for the FIB sources
*/
static const char *fib_source_names[] = FIB_SOURCES;
static const char *fib_attribute_names[] = FIB_ENTRY_ATTRIBUTES;
static const char *fib_src_attribute_names[] = FIB_ENTRY_SRC_ATTRIBUTES;
/*
* Pool for all fib_entries
*/
static fib_entry_t *fib_entry_pool;
/**
* the logger
*/
vlib_log_class_t fib_entry_logger;
fib_entry_t *
fib_entry_get (fib_node_index_t index)
{
return (pool_elt_at_index(fib_entry_pool, index));
}
static fib_node_t *
fib_entry_get_node (fib_node_index_t index)
{
return ((fib_node_t*)fib_entry_get(index));
}
fib_node_index_t
fib_entry_get_index (const fib_entry_t * fib_entry)
{
return (fib_entry - fib_entry_pool);
}
fib_protocol_t
fib_entry_get_proto (const fib_entry_t * fib_entry)
{
return (fib_entry->fe_prefix.fp_proto);
}
dpo_proto_t
fib_entry_get_dpo_proto (const fib_entry_t * fib_entry)
{
return (fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto));
}
fib_forward_chain_type_t
fib_entry_get_default_chain_type (const fib_entry_t *fib_entry)
{
switch (fib_entry->fe_prefix.fp_proto)
{
case FIB_PROTOCOL_IP4:
return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
case FIB_PROTOCOL_IP6:
return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
case FIB_PROTOCOL_MPLS:
if (MPLS_EOS == fib_entry->fe_prefix.fp_eos)
return (FIB_FORW_CHAIN_TYPE_MPLS_EOS);
else
return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
}
return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
}
u8 *
format_fib_source (u8 * s, va_list * args)
{
fib_source_t source = va_arg (*args, int);
s = format (s, "src:%s", fib_source_names[source]);
return (s);
}
u8 *
format_fib_entry_flags (u8 *s, va_list *args)
{
fib_entry_attribute_t attr;
fib_entry_flag_t flag = va_arg(*args, int);
FOR_EACH_FIB_ATTRIBUTE(attr) {
if ((1<<attr) & flag) {
s = format (s, "%s,", fib_attribute_names[attr]);
}
}
return (s);
}
u8 *
format_fib_entry_src_flags (u8 *s, va_list *args)
{
fib_entry_src_attribute_t sattr;
fib_entry_src_flag_t flag = va_arg(*args, int);
FOR_EACH_FIB_SRC_ATTRIBUTE(sattr) {
if ((1<<sattr) & flag) {
s = format (s, "%s,", fib_src_attribute_names[sattr]);
}
}
return (s);
}
u8 *
format_fib_entry (u8 * s, va_list * args)
{
fib_forward_chain_type_t fct;
fib_entry_t *fib_entry;
fib_entry_src_t *src;
fib_node_index_t fei;
fib_source_t source;
int level;
fei = va_arg (*args, fib_node_index_t);
level = va_arg (*args, int);
fib_entry = fib_entry_get(fei);
s = format (s, "%U", format_fib_prefix, &fib_entry->fe_prefix);
if (level >= FIB_ENTRY_FORMAT_DETAIL)
{
s = format (s, " fib:%d", fib_entry->fe_fib_index);
s = format (s, " index:%d", fib_entry_get_index(fib_entry));
s = format (s, " locks:%d", fib_entry->fe_node.fn_locks);
FOR_EACH_SRC_ADDED(fib_entry, src, source,
({
s = format (s, "\n %U", format_fib_source, source);
s = format (s, " refs:%d", src->fes_ref_count);
if (FIB_ENTRY_FLAG_NONE != src->fes_entry_flags) {
s = format(s, " entry-flags:%U",
format_fib_entry_flags, src->fes_entry_flags);
}
if (FIB_ENTRY_SRC_FLAG_NONE != src->fes_flags) {
s = format(s, " src-flags:%U",
format_fib_entry_src_flags, src->fes_flags);
}
s = fib_entry_src_format(fib_entry, source, s);
s = format (s, "\n");
if (FIB_NODE_INDEX_INVALID != src->fes_pl)
{
s = fib_path_list_format(src->fes_pl, s);
}
s = format(s, "%U", format_fib_path_ext_list, &src->fes_path_exts);
}));
s = format (s, "\n forwarding: ");
}
else
{
s = format (s, "\n");
}
fct = fib_entry_get_default_chain_type(fib_entry);
if (!dpo_id_is_valid(&fib_entry->fe_lb))
{
s = format (s, " UNRESOLVED\n");
return (s);
}
else
{
s = format(s, " %U-chain\n %U",
format_fib_forw_chain_type, fct,
format_dpo_id,
&fib_entry->fe_lb,
2);
s = format(s, "\n");
if (level >= FIB_ENTRY_FORMAT_DETAIL2)
{
index_t *fedi;
s = format (s, " Delegates:\n");
vec_foreach(fedi, fib_entry->fe_delegates)
{
s = format(s, " %U\n", format_fib_entry_delegate, *fedi);
}
}
}
if (level >= FIB_ENTRY_FORMAT_DETAIL2)
{
s = format(s, " Children:");
s = fib_node_children_format(fib_entry->fe_node.fn_children, s);
}
return (s);
}
static fib_entry_t*
fib_entry_from_fib_node (fib_node_t *node)
{
ASSERT(FIB_NODE_TYPE_ENTRY == node->fn_type);
return ((fib_entry_t*)node);
}
static void
fib_entry_last_lock_gone (fib_node_t *node)
{
fib_entry_delegate_type_t fdt;
fib_entry_delegate_t *fed;
fib_entry_t *fib_entry;
fib_entry = fib_entry_from_fib_node(node);
ASSERT(!dpo_id_is_valid(&fib_entry->fe_lb));
FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
{
dpo_reset(&fed->fd_dpo);
fib_entry_delegate_remove(fib_entry, fdt);
});
FIB_ENTRY_DBG(fib_entry, "last-lock");
fib_node_deinit(&fib_entry->fe_node);
ASSERT(0 == vec_len(fib_entry->fe_delegates));
vec_free(fib_entry->fe_delegates);
vec_free(fib_entry->fe_srcs);
pool_put(fib_entry_pool, fib_entry);
}
static fib_entry_src_t*
fib_entry_get_best_src_i (const fib_entry_t *fib_entry)
{
fib_entry_src_t *bsrc;
/*
* the enum of sources is deliberately arranged in priority order
*/
if (0 == vec_len(fib_entry->fe_srcs))
{
bsrc = NULL;
}
else
{
bsrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
}
return (bsrc);
}
static fib_source_t
fib_entry_src_get_source (const fib_entry_src_t *esrc)
{
if (NULL != esrc)
{
return (esrc->fes_src);
}
return (FIB_SOURCE_MAX);
}
static fib_entry_flag_t
fib_entry_src_get_flags (const fib_entry_src_t *esrc)
{
if (NULL != esrc)
{
return (esrc->fes_entry_flags);
}
return (FIB_ENTRY_FLAG_NONE);
}
fib_entry_flag_t
fib_entry_get_flags (fib_node_index_t fib_entry_index)
{
return (fib_entry_get_flags_i(fib_entry_get(fib_entry_index)));
}
/*
* fib_entry_back_walk_notify
*
* A back walk has reach this entry.
*/
static fib_node_back_walk_rc_t
fib_entry_back_walk_notify (fib_node_t *node,
fib_node_back_walk_ctx_t *ctx)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_from_fib_node(node);
if (FIB_NODE_BW_REASON_FLAG_EVALUATE & ctx->fnbw_reason ||
FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE & ctx->fnbw_reason ||
FIB_NODE_BW_REASON_FLAG_ADJ_DOWN & ctx->fnbw_reason ||
FIB_NODE_BW_REASON_FLAG_INTERFACE_UP & ctx->fnbw_reason ||
FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN & ctx->fnbw_reason ||
FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE & ctx->fnbw_reason)
{
fib_entry_src_action_reactivate(fib_entry,
fib_entry_get_best_source(
fib_entry_get_index(fib_entry)));
}
/*
* all other walk types can be reclassifed to a re-evaluate to
* all recursive dependents.
* By reclassifying we ensure that should any of these walk types meet
* they can be merged.
*/
ctx->fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
/*
* ... and nothing is forced sync from now on.
*/
ctx->fnbw_flags &= ~FIB_NODE_BW_FLAG_FORCE_SYNC;
FIB_ENTRY_DBG(fib_entry, "bw:%U",
format_fib_node_bw_reason, ctx->fnbw_reason);
/*
* propagate the backwalk further if we haven't already reached the
* maximum depth.
*/
fib_walk_sync(FIB_NODE_TYPE_ENTRY,
fib_entry_get_index(fib_entry),
ctx);
return (FIB_NODE_BACK_WALK_CONTINUE);
}
static void
fib_entry_show_memory (void)
{
u32 n_srcs = 0, n_exts = 0;
fib_entry_src_t *esrc;
fib_entry_t *entry;
fib_show_memory_usage("Entry",
pool_elts(fib_entry_pool),
pool_len(fib_entry_pool),
sizeof(fib_entry_t));
pool_foreach(entry, fib_entry_pool,
({
n_srcs += vec_len(entry->fe_srcs);
vec_foreach(esrc, entry->fe_srcs)
{
n_exts += fib_path_ext_list_length(&esrc->fes_path_exts);
}
}));
fib_show_memory_usage("Entry Source",
n_srcs, n_srcs, sizeof(fib_entry_src_t));
fib_show_memory_usage("Entry Path-Extensions",
n_exts, n_exts,
sizeof(fib_path_ext_t));
}
/*
* The FIB path-list's graph node virtual function table
*/
static const fib_node_vft_t fib_entry_vft = {
.fnv_get = fib_entry_get_node,
.fnv_last_lock = fib_entry_last_lock_gone,
.fnv_back_walk = fib_entry_back_walk_notify,
.fnv_mem_show = fib_entry_show_memory,
};
/**
* @brief Contribute the set of Adjacencies that this entry forwards with
* to build the uRPF list of its children
*/
void
fib_entry_contribute_urpf (fib_node_index_t entry_index,
index_t urpf)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(entry_index);
return (fib_path_list_contribute_urpf(fib_entry->fe_parent, urpf));
}
/*
* If the client is request a chain for multicast forwarding then swap
* the chain type to one that can provide such transport.
*/
static fib_forward_chain_type_t
fib_entry_chain_type_mcast_to_ucast (fib_forward_chain_type_t fct)
{
switch (fct)
{
case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
/*
* we can only transport IP multicast packets if there is an
* LSP.
*/
fct = FIB_FORW_CHAIN_TYPE_MPLS_EOS;
break;
case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
case FIB_FORW_CHAIN_TYPE_ETHERNET:
case FIB_FORW_CHAIN_TYPE_NSH:
case FIB_FORW_CHAIN_TYPE_BIER:
break;
}
return (fct);
}
/*
* fib_entry_contribute_forwarding
*
* Get an lock the forwarding information (DPO) contributed by the FIB entry.
*/
void
fib_entry_contribute_forwarding (fib_node_index_t fib_entry_index,
fib_forward_chain_type_t fct,
dpo_id_t *dpo)
{
fib_entry_delegate_t *fed;
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
/*
* mfib children ask for mcast chains. fix these to the appropriate ucast types.
*/
fct = fib_entry_chain_type_mcast_to_ucast(fct);
if (fct == fib_entry_get_default_chain_type(fib_entry))
{
dpo_copy(dpo, &fib_entry->fe_lb);
}
else
{
fed = fib_entry_delegate_find(fib_entry,
fib_entry_chain_type_to_delegate_type(fct));
if (NULL == fed)
{
fed = fib_entry_delegate_find_or_add(
fib_entry,
fib_entry_chain_type_to_delegate_type(fct));
/*
* on-demand create eos/non-eos.
* There is no on-demand delete because:
* - memory versus complexity & reliability:
* leaving unrequired [n]eos LB arounds wastes memory, cleaning
* then up on the right trigger is more code. i favour the latter.
*/
fib_entry_src_mk_lb(fib_entry,
fib_entry_get_best_src_i(fib_entry),
fct,
&fed->fd_dpo);
}
dpo_copy(dpo, &fed->fd_dpo);
}
/*
* use the drop DPO is nothing else is present
*/
if (!dpo_id_is_valid(dpo))
{
dpo_copy(dpo, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
}
/*
* don't allow the special index indicating replicate.vs.load-balance
* to escape to the clients
*/
dpo->dpoi_index &= ~MPLS_IS_REPLICATE;
}
const dpo_id_t *
fib_entry_contribute_ip_forwarding (fib_node_index_t fib_entry_index)
{
fib_forward_chain_type_t fct;
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
fct = fib_entry_get_default_chain_type(fib_entry);
ASSERT((fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP4 ||
fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP6));
if (dpo_id_is_valid(&fib_entry->fe_lb))
{
return (&fib_entry->fe_lb);
}
return (drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
}
adj_index_t
fib_entry_get_adj (fib_node_index_t fib_entry_index)
{
const dpo_id_t *dpo;
dpo = fib_entry_contribute_ip_forwarding(fib_entry_index);
if (dpo_id_is_valid(dpo))
{
dpo = load_balance_get_bucket(dpo->dpoi_index, 0);
if (dpo_is_adj(dpo))
{
return (dpo->dpoi_index);
}
}
return (ADJ_INDEX_INVALID);
}
fib_node_index_t
fib_entry_get_path_list (fib_node_index_t fib_entry_index)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
return (fib_entry->fe_parent);
}
u32
fib_entry_child_add (fib_node_index_t fib_entry_index,
fib_node_type_t child_type,
fib_node_index_t child_index)
{
return (fib_node_child_add(FIB_NODE_TYPE_ENTRY,
fib_entry_index,
child_type,
child_index));
};
void
fib_entry_child_remove (fib_node_index_t fib_entry_index,
u32 sibling_index)
{
fib_node_child_remove(FIB_NODE_TYPE_ENTRY,
fib_entry_index,
sibling_index);
if (0 == fib_node_get_n_children(FIB_NODE_TYPE_ENTRY,
fib_entry_index))
{
/*
* if there are no children left then there is no reason to keep
* the non-default forwarding chains. those chains are built only
* because the children want them.
*/
fib_entry_delegate_type_t fdt;
fib_entry_delegate_t *fed;
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
{
dpo_reset(&fed->fd_dpo);
fib_entry_delegate_remove(fib_entry, fdt);
});
}
}
static fib_entry_t *
fib_entry_alloc (u32 fib_index,
const fib_prefix_t *prefix,
fib_node_index_t *fib_entry_index)
{
fib_entry_t *fib_entry;
fib_prefix_t *fep;
pool_get(fib_entry_pool, fib_entry);
clib_memset(fib_entry, 0, sizeof(*fib_entry));
fib_node_init(&fib_entry->fe_node,
FIB_NODE_TYPE_ENTRY);
fib_entry->fe_fib_index = fib_index;
/*
* the one time we need to update the const prefix is when
* the entry is first created
*/
fep = (fib_prefix_t*)&(fib_entry->fe_prefix);
*fep = *prefix;
if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
{
fep->fp_len = 21;
if (MPLS_NON_EOS == fep->fp_eos)
{
fep->fp_payload_proto = DPO_PROTO_MPLS;
}
ASSERT(DPO_PROTO_NONE != fib_entry->fe_prefix.fp_payload_proto);
}
dpo_reset(&fib_entry->fe_lb);
*fib_entry_index = fib_entry_get_index(fib_entry);
return (fib_entry);
}
static fib_entry_t*
fib_entry_post_flag_update_actions (fib_entry_t *fib_entry,
fib_entry_flag_t old_flags)
{
fib_node_index_t fei;
/*
* save the index so we can recover from pool reallocs
*/
fei = fib_entry_get_index(fib_entry);
/*
* handle changes to attached export for import entries
*/
int is_import = (FIB_ENTRY_FLAG_IMPORT & fib_entry_get_flags_i(fib_entry));
int was_import = (FIB_ENTRY_FLAG_IMPORT & old_flags);
if (!was_import && is_import)
{
/*
* transition from not exported to exported
*/
/*
* there is an assumption here that the entry resolves via only
* one interface and that it is the cross VRF interface.
*/
u32 sw_if_index = fib_path_list_get_resolving_interface(fib_entry->fe_parent);
fib_attached_export_import(fib_entry,
fib_table_get_index_for_sw_if_index(
fib_entry_get_proto(fib_entry),
sw_if_index));
}
else if (was_import && !is_import)
{
/*
* transition from exported to not exported
*/
fib_attached_export_purge(fib_entry);
}
/*
* else
* no change. nothing to do.
*/
/*
* reload the entry address post possible pool realloc
*/
fib_entry = fib_entry_get(fei);
/*
* handle changes to attached export for export entries
*/
int is_attached = (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(fib_entry));
int was_attached = (FIB_ENTRY_FLAG_ATTACHED & old_flags);
if (!was_attached && is_attached)
{
/*
* transition to attached. time to export
*/
// FIXME
}
// else FIXME
return (fib_entry);
}
static void
fib_entry_post_install_actions (fib_entry_t *fib_entry,
fib_source_t source,
fib_entry_flag_t old_flags)
{
fib_entry = fib_entry_post_flag_update_actions(fib_entry,
old_flags);
fib_entry_src_action_installed(fib_entry, source);
}
fib_node_index_t
fib_entry_create (u32 fib_index,
const fib_prefix_t *prefix,
fib_source_t source,
fib_entry_flag_t flags,
const fib_route_path_t *paths)
{
fib_node_index_t fib_entry_index;
fib_entry_t *fib_entry;
ASSERT(0 < vec_len(paths));
fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index);
/*
* since this is a new entry create, we don't need to check for winning
* sources - there is only one.
*/
fib_entry = fib_entry_src_action_add(fib_entry, source, flags,
drop_dpo_get(
fib_proto_to_dpo(
fib_entry_get_proto(fib_entry))));
fib_entry_src_action_path_swap(fib_entry,
source,
flags,
paths);
/*
* handle possible realloc's by refetching the pointer
*/
fib_entry = fib_entry_get(fib_entry_index);
fib_entry_src_action_activate(fib_entry, source);
fib_entry_post_install_actions(fib_entry, source, FIB_ENTRY_FLAG_NONE);
FIB_ENTRY_DBG(fib_entry, "create");
return (fib_entry_index);
}
fib_node_index_t
fib_entry_create_special (u32 fib_index,
const fib_prefix_t *prefix,
fib_source_t source,
fib_entry_flag_t flags,
const dpo_id_t *dpo)
{
fib_node_index_t fib_entry_index;
fib_entry_t *fib_entry;
/*
* create and initialize the new enty
*/
fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index);
/*
* create the path-list
*/
fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo);
fib_entry_src_action_activate(fib_entry, source);
fib_entry_post_install_actions(fib_entry, source, FIB_ENTRY_FLAG_NONE);
FIB_ENTRY_DBG(fib_entry, "create-special");
return (fib_entry_index);
}
static void
fib_entry_post_update_actions (fib_entry_t *fib_entry,
fib_source_t source,
fib_entry_flag_t old_flags)
{
/*
* backwalk to children to inform then of the change to forwarding.
*/
fib_node_back_walk_ctx_t bw_ctx = {
.fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
};
fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_get_index(fib_entry), &bw_ctx);
/*
* then inform any covered prefixes
*/
fib_entry_cover_update_notify(fib_entry);
fib_entry_post_install_actions(fib_entry, source, old_flags);
}
void
fib_entry_recalculate_forwarding (fib_node_index_t fib_entry_index)
{
fib_source_t best_source;
fib_entry_t *fib_entry;
fib_entry_src_t *bsrc;
fib_entry = fib_entry_get(fib_entry_index);
bsrc = fib_entry_get_best_src_i(fib_entry);
best_source = fib_entry_src_get_source(bsrc);
fib_entry_src_action_reactivate(fib_entry, best_source);
}
static void
fib_entry_source_change_w_flags (fib_entry_t *fib_entry,
fib_source_t old_source,
fib_entry_flag_t old_flags,
fib_source_t new_source)
{
if (new_source < old_source)
{
/*
* we have a new winning source.
*/
fib_entry_src_action_deactivate(fib_entry, old_source);
fib_entry_src_action_activate(fib_entry, new_source);
}
else if (new_source > old_source)
{
/*
* the new source loses. Re-activate the winning sources
* in case it is an interposer and hence relied on the losing
* source's path-list.
*/
fib_entry_src_action_reactivate(fib_entry, old_source);
return;
}
else
{
/*
* the new source is one this entry already has.
* But the path-list was updated, which will contribute new forwarding,
* so install it.
*/
fib_entry_src_action_reactivate(fib_entry, new_source);
}
fib_entry_post_update_actions(fib_entry, new_source, old_flags);
}
void
fib_entry_source_change (fib_entry_t *fib_entry,
fib_source_t old_source,
fib_source_t new_source)
{
fib_entry_flag_t old_flags;
old_flags = fib_entry_get_flags_for_source(
fib_entry_get_index(fib_entry), old_source);
return (fib_entry_source_change_w_flags(fib_entry, old_source,
old_flags, new_source));
}
void
fib_entry_special_add (fib_node_index_t fib_entry_index,
fib_source_t source,
fib_entry_flag_t flags,
const dpo_id_t *dpo)
{
fib_source_t best_source;
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
best_source = fib_entry_get_best_source(fib_entry_index);
fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo);
fib_entry_source_change(fib_entry, best_source, source);
FIB_ENTRY_DBG(fib_entry, "special-add:%U", format_fib_source, source);
}
void
fib_entry_special_update (fib_node_index_t fib_entry_index,
fib_source_t source,
fib_entry_flag_t flags,
const dpo_id_t *dpo)
{
fib_source_t best_source;
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
best_source = fib_entry_get_best_source(fib_entry_index);
fib_entry = fib_entry_src_action_update(fib_entry, source, flags, dpo);
fib_entry_source_change(fib_entry, best_source, source);
FIB_ENTRY_DBG(fib_entry, "special-updated:%U", format_fib_source, source);
}
void
fib_entry_path_add (fib_node_index_t fib_entry_index,
fib_source_t source,
fib_entry_flag_t flags,
const fib_route_path_t *rpaths)
{
fib_source_t best_source;
fib_entry_t *fib_entry;
fib_entry_src_t *bsrc;
fib_entry = fib_entry_get(fib_entry_index);
ASSERT(NULL != fib_entry);
bsrc = fib_entry_get_best_src_i(fib_entry);
best_source = fib_entry_src_get_source(bsrc);
fib_entry = fib_entry_src_action_path_add(fib_entry, source, flags, rpaths);
fib_entry_source_change(fib_entry, best_source, source);
FIB_ENTRY_DBG(fib_entry, "path add:%U", format_fib_source, source);
}
static fib_entry_src_flag_t
fib_entry_src_burn_only_inherited (fib_entry_t *fib_entry)
{
fib_entry_src_t *src;
fib_source_t source;
int has_only_inherited_sources = 1;
FOR_EACH_SRC_ADDED(fib_entry, src, source,
({
if (!(src->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED))
{
has_only_inherited_sources = 0;
break;
}
}));
if (has_only_inherited_sources)
{
FOR_EACH_SRC_ADDED(fib_entry, src, source,
({
fib_entry_src_action_remove(fib_entry, source);
}));
return (FIB_ENTRY_SRC_FLAG_NONE);
}
else
{
return (FIB_ENTRY_SRC_FLAG_ADDED);
}
}
static fib_entry_src_flag_t
fib_entry_source_removed (fib_entry_t *fib_entry,
fib_entry_flag_t old_flags)
{
const fib_entry_src_t *bsrc;
fib_source_t best_source;
/*
* if all that is left are inherited sources, then burn them
*/
fib_entry_src_burn_only_inherited(fib_entry);
bsrc = fib_entry_get_best_src_i(fib_entry);
best_source = fib_entry_src_get_source(bsrc);
if (FIB_SOURCE_MAX == best_source)
{
/*
* no more sources left. this entry is toast.
*/
fib_entry = fib_entry_post_flag_update_actions(fib_entry, old_flags);
fib_entry_src_action_uninstall(fib_entry);
return (FIB_ENTRY_SRC_FLAG_NONE);
}
else
{
fib_entry_src_action_activate(fib_entry, best_source);
}
fib_entry_post_update_actions(fib_entry, best_source, old_flags);
/*
* still have sources
*/
return (FIB_ENTRY_SRC_FLAG_ADDED);
}
/*
* fib_entry_path_remove
*
* remove a path from the entry.
* return the fib_entry's index if it is still present, INVALID otherwise.
*/
fib_entry_src_flag_t
fib_entry_path_remove (fib_node_index_t fib_entry_index,
fib_source_t source,
const fib_route_path_t *rpaths)
{
fib_entry_src_flag_t sflag;
fib_source_t best_source;
fib_entry_flag_t bflags;
fib_entry_t *fib_entry;
fib_entry_src_t *bsrc;
fib_entry = fib_entry_get(fib_entry_index);
ASSERT(NULL != fib_entry);
bsrc = fib_entry_get_best_src_i(fib_entry);
best_source = fib_entry_src_get_source(bsrc);
bflags = fib_entry_src_get_flags(bsrc);
sflag = fib_entry_src_action_path_remove(fib_entry, source, rpaths);
FIB_ENTRY_DBG(fib_entry, "path remove:%U", format_fib_source, source);
/*
* if the path list for the source passed is invalid,
* then we need to create a new one. else we are updating
* an existing.
*/
if (source < best_source)
{
/*
* Que! removing a path from a source that is better than the
* one this entry is using.
*/
ASSERT(0);
}
else if (source > best_source )
{
/*
* the source is not the best. no need to update forwarding
*/
if (FIB_ENTRY_SRC_FLAG_ADDED & sflag)
{
/*
* the source being removed still has paths
*/
return (FIB_ENTRY_SRC_FLAG_ADDED);
}
else
{
/*
* that was the last path from this source, check if those
* that remain are non-inherited
*/
return (fib_entry_src_burn_only_inherited(fib_entry));
}
}
else
{
/*
* removing a path from the path-list we were using.
*/
if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag))
{
/*
* the last path from the source was removed.
* fallback to lower source
*/
return (fib_entry_source_removed(fib_entry, bflags));
}
else
{
/*
* re-install the new forwarding information
*/
fib_entry_src_action_reactivate(fib_entry, source);
}
}
fib_entry_post_update_actions(fib_entry, source, bflags);
/*
* still have sources
*/
return (FIB_ENTRY_SRC_FLAG_ADDED);
}
/*
* fib_entry_special_remove
*
* remove a special source from the entry.
* return the fib_entry's index if it is still present, INVALID otherwise.
*/
fib_entry_src_flag_t
fib_entry_special_remove (fib_node_index_t fib_entry_index,
fib_source_t source)
{
fib_entry_src_flag_t sflag;
fib_source_t best_source;
fib_entry_flag_t bflags;
fib_entry_t *fib_entry;
fib_entry_src_t *bsrc;
fib_entry = fib_entry_get(fib_entry_index);
ASSERT(NULL != fib_entry);
bsrc = fib_entry_get_best_src_i(fib_entry);
best_source = fib_entry_src_get_source(bsrc);
bflags = fib_entry_src_get_flags(bsrc);
FIB_ENTRY_DBG(fib_entry, "special remove:%U", format_fib_source, source);
sflag = fib_entry_src_action_remove_or_update_inherit(fib_entry, source);
/*
* if the path list for the source passed is invalid,
* then we need to create a new one. else we are updating
* an existing.
*/
if (source < best_source )
{
/*
* Que! removing a path from a source that is better than the
* one this entry is using. This can only mean it is a source
* this prefix does not have.
*/
return (FIB_ENTRY_SRC_FLAG_ADDED);
}
else if (source > best_source ) {
/*
* the source is not the best. no need to update forwarding
*/
if (FIB_ENTRY_SRC_FLAG_ADDED & sflag)
{
/*
* the source being removed still has paths
*/
return (FIB_ENTRY_SRC_FLAG_ADDED);
}
else
{
/*
* that was the last path from this source, check if those
* that remain are non-inherited
*/
if (FIB_ENTRY_SRC_FLAG_NONE == fib_entry_src_burn_only_inherited(fib_entry))
{
/*
* no more sources left. this entry is toast.
*/
fib_entry = fib_entry_post_flag_update_actions(fib_entry, bflags);
fib_entry_src_action_uninstall(fib_entry);
return (FIB_ENTRY_SRC_FLAG_NONE);
}
/*
* reactivate the best source so the interposer gets restacked
*/
fib_entry_src_action_reactivate(fib_entry, best_source);
return (FIB_ENTRY_SRC_FLAG_ADDED);
}
}
else
{
if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag))
{
/*
* the source was removed. use the next best.
*/
return (fib_entry_source_removed(fib_entry, bflags));
}
else
{
/*
* re-install the new forwarding information
*/
fib_entry_src_action_reactivate(fib_entry, source);
}
}
fib_entry_post_update_actions(fib_entry, source, bflags);
/*
* still have sources
*/
return (FIB_ENTRY_SRC_FLAG_ADDED);
}
/**
* fib_entry_inherit
*
* If the source on the cover is inheriting then push this source
* down to the covered.
*/
void
fib_entry_inherit (fib_node_index_t cover,
fib_node_index_t covered)
{
fib_entry_src_inherit(fib_entry_get(cover),
fib_entry_get(covered));
}
/**
* fib_entry_delete
*
* The source is withdrawing all the paths it provided
*/
fib_entry_src_flag_t
fib_entry_delete (fib_node_index_t fib_entry_index,
fib_source_t source)
{
return (fib_entry_special_remove(fib_entry_index, source));
}
/**
* fib_entry_update
*
* The source has provided a new set of paths that will replace the old.
*/
void
fib_entry_update (fib_node_index_t fib_entry_index,
fib_source_t source,
fib_entry_flag_t flags,
const fib_route_path_t *paths)
{
fib_source_t best_source;
fib_entry_flag_t bflags;
fib_entry_t *fib_entry;
fib_entry_src_t *bsrc;
fib_entry = fib_entry_get(fib_entry_index);
ASSERT(NULL != fib_entry);
bsrc = fib_entry_get_best_src_i(fib_entry);
best_source = fib_entry_src_get_source(bsrc);
bflags = fib_entry_get_flags_i(fib_entry);
fib_entry = fib_entry_src_action_path_swap(fib_entry,
source,
flags,
paths);
fib_entry_source_change_w_flags(fib_entry, best_source, bflags, source);
FIB_ENTRY_DBG(fib_entry, "update");
}
/*
* fib_entry_cover_changed
*
* this entry is tracking its cover and that cover has changed.
*/
void
fib_entry_cover_changed (fib_node_index_t fib_entry_index)
{
fib_entry_src_cover_res_t res = {
.install = !0,
.bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
};
CLIB_UNUSED(fib_source_t source);
fib_source_t best_source;
fib_entry_flag_t bflags;
fib_entry_t *fib_entry;
fib_entry_src_t *esrc;
u32 index;
bflags = FIB_ENTRY_FLAG_NONE;
best_source = FIB_SOURCE_FIRST;
fib_entry = fib_entry_get(fib_entry_index);
fib_attached_export_cover_change(fib_entry);
/*
* propagate the notification to each of the added sources
*/
index = 0;
FOR_EACH_SRC_ADDED(fib_entry, esrc, source,
({
if (0 == index)
{
/*
* only the best source gets to set the back walk flags
*/
res = fib_entry_src_action_cover_change(fib_entry, esrc);
bflags = fib_entry_src_get_flags(esrc);
best_source = fib_entry_src_get_source(esrc);
}
else
{
fib_entry_src_action_cover_change(fib_entry, esrc);
}
index++;
}));
if (res.install)
{
fib_entry_src_action_reactivate(fib_entry,
fib_entry_src_get_source(
fib_entry_get_best_src_i(fib_entry)));
fib_entry_post_install_actions(fib_entry, best_source, bflags);
}
else
{
fib_entry_src_action_uninstall(fib_entry);
}
if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason)
{
/*
* time for walkies fido.
*/
fib_node_back_walk_ctx_t bw_ctx = {
.fnbw_reason = res.bw_reason,
};
fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx);
}
FIB_ENTRY_DBG(fib_entry, "cover-changed");
}
/*
* fib_entry_cover_updated
*
* this entry is tracking its cover and that cover has been updated
* (i.e. its forwarding information has changed).
*/
void
fib_entry_cover_updated (fib_node_index_t fib_entry_index)
{
fib_entry_src_cover_res_t res = {
.install = !0,
.bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
};
CLIB_UNUSED(fib_source_t source);
fib_source_t best_source;
fib_entry_flag_t bflags;
fib_entry_t *fib_entry;
fib_entry_src_t *esrc;
u32 index;
bflags = FIB_ENTRY_FLAG_NONE;
best_source = FIB_SOURCE_FIRST;
fib_entry = fib_entry_get(fib_entry_index);
fib_attached_export_cover_update(fib_entry);
/*
* propagate the notification to each of the added sources
*/
index = 0;
FOR_EACH_SRC_ADDED(fib_entry, esrc, source,
({
if (0 == index)
{
/*
* only the best source gets to set the back walk flags
*/
res = fib_entry_src_action_cover_update(fib_entry, esrc);
bflags = fib_entry_src_get_flags(esrc);
best_source = fib_entry_src_get_source(esrc);
}
else
{
fib_entry_src_action_cover_update(fib_entry, esrc);
}
index++;
}));
if (res.install)
{
fib_entry_src_action_reactivate(fib_entry,
fib_entry_src_get_source(
fib_entry_get_best_src_i(fib_entry)));
fib_entry_post_install_actions(fib_entry, best_source, bflags);
}
else
{
fib_entry_src_action_uninstall(fib_entry);
}
if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason)
{
/*
* time for walkies fido.
*/
fib_node_back_walk_ctx_t bw_ctx = {
.fnbw_reason = res.bw_reason,
};
fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx);
}
FIB_ENTRY_DBG(fib_entry, "cover-updated");
}
int
fib_entry_recursive_loop_detect (fib_node_index_t entry_index,
fib_node_index_t **entry_indicies)
{
fib_entry_t *fib_entry;
int was_looped, is_looped;
fib_entry = fib_entry_get(entry_index);
if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent)
{
fib_node_index_t *entries = *entry_indicies;
vec_add1(entries, entry_index);
was_looped = fib_path_list_is_looped(fib_entry->fe_parent);
is_looped = fib_path_list_recursive_loop_detect(fib_entry->fe_parent,
&entries);
*entry_indicies = entries;
if (!!was_looped != !!is_looped)
{
/*
* re-evaluate all the entry's forwarding
* NOTE: this is an inplace modify
*/
fib_entry_delegate_type_t fdt;
fib_entry_delegate_t *fed;
FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
{
fib_entry_src_mk_lb(fib_entry,
fib_entry_get_best_src_i(fib_entry),
fib_entry_delegate_type_to_chain_type(fdt),
&fed->fd_dpo);
});
}
}
else
{
/*
* the entry is currently not linked to a path-list. this happens
* when it is this entry that is re-linking path-lists and has thus
* broken the loop
*/
is_looped = 0;
}
return (is_looped);
}
u32
fib_entry_get_resolving_interface (fib_node_index_t entry_index)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(entry_index);
return (fib_path_list_get_resolving_interface(fib_entry->fe_parent));
}
fib_source_t
fib_entry_get_best_source (fib_node_index_t entry_index)
{
fib_entry_t *fib_entry;
fib_entry_src_t *bsrc;
fib_entry = fib_entry_get(entry_index);
bsrc = fib_entry_get_best_src_i(fib_entry);
return (fib_entry_src_get_source(bsrc));
}
/**
* Return !0 is the entry represents a host prefix
*/
int
fib_entry_is_host (fib_node_index_t fib_entry_index)
{
return (fib_prefix_is_host(fib_entry_get_prefix(fib_entry_index)));
}
/**
* Return !0 is the entry is resolved, i.e. will return a valid forwarding
* chain
*/
int
fib_entry_is_resolved (fib_node_index_t fib_entry_index)
{
fib_entry_delegate_t *fed;
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
fed = fib_entry_delegate_find(fib_entry, FIB_ENTRY_DELEGATE_BFD);
if (NULL == fed)
{
/*
* no BFD tracking - consider it resolved.
*/
return (!0);
}
else
{
/*
* defer to the state of the BFD tracking
*/
return (FIB_BFD_STATE_UP == fed->fd_bfd_state);
}
}
void
fib_entry_set_flow_hash_config (fib_node_index_t fib_entry_index,
flow_hash_config_t hash_config)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
/*
* pass the hash-config on to the load-balance object where it is cached.
* we can ignore LBs in the delegate chains, since they will not be of the
* correct protocol type (i.e. they are not IP)
* There's no way, nor need, to change the hash config for MPLS.
*/
if (dpo_id_is_valid(&fib_entry->fe_lb))
{
load_balance_t *lb;
ASSERT(DPO_LOAD_BALANCE == fib_entry->fe_lb.dpoi_type);
lb = load_balance_get(fib_entry->fe_lb.dpoi_index);
/*
* atomic update for packets in flight
*/
lb->lb_hash_config = hash_config;
}
}
u32
fib_entry_get_stats_index (fib_node_index_t fib_entry_index)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
return (fib_entry->fe_lb.dpoi_index);
}
static int
fib_ip4_address_compare (const ip4_address_t * a1,
const ip4_address_t * a2)
{
/*
* IP addresses are unsigned ints. the return value here needs to be signed
* a simple subtraction won't cut it.
* If the addresses are the same, the sort order is undefined, so phoey.
*/
return ((clib_net_to_host_u32(a1->data_u32) >
clib_net_to_host_u32(a2->data_u32) ) ?
1 : -1);
}
static int
fib_ip6_address_compare (const ip6_address_t * a1,
const ip6_address_t * a2)
{
int i;
for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
{
int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
clib_net_to_host_u16 (a2->as_u16[i]));
if (cmp != 0)
return cmp;
}
return 0;
}
static int
fib_entry_cmp (fib_node_index_t fib_entry_index1,
fib_node_index_t fib_entry_index2)
{
fib_entry_t *fib_entry1, *fib_entry2;
int cmp = 0;
fib_entry1 = fib_entry_get(fib_entry_index1);
fib_entry2 = fib_entry_get(fib_entry_index2);
switch (fib_entry1->fe_prefix.fp_proto)
{
case FIB_PROTOCOL_IP4:
cmp = fib_ip4_address_compare(&fib_entry1->fe_prefix.fp_addr.ip4,
&fib_entry2->fe_prefix.fp_addr.ip4);
break;
case FIB_PROTOCOL_IP6:
cmp = fib_ip6_address_compare(&fib_entry1->fe_prefix.fp_addr.ip6,
&fib_entry2->fe_prefix.fp_addr.ip6);
break;
case FIB_PROTOCOL_MPLS:
cmp = (fib_entry1->fe_prefix.fp_label - fib_entry2->fe_prefix.fp_label);
if (0 == cmp)
{
cmp = (fib_entry1->fe_prefix.fp_eos - fib_entry2->fe_prefix.fp_eos);
}
break;
}
if (0 == cmp) {
cmp = (fib_entry1->fe_prefix.fp_len - fib_entry2->fe_prefix.fp_len);
}
return (cmp);
}
int
fib_entry_cmp_for_sort (void *i1, void *i2)
{
fib_node_index_t *fib_entry_index1 = i1, *fib_entry_index2 = i2;
return (fib_entry_cmp(*fib_entry_index1,
*fib_entry_index2));
}
void
fib_entry_lock (fib_node_index_t fib_entry_index)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
fib_node_lock(&fib_entry->fe_node);
}
void
fib_entry_unlock (fib_node_index_t fib_entry_index)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
fib_node_unlock(&fib_entry->fe_node);
}
void
fib_entry_module_init (void)
{
fib_node_register_type(FIB_NODE_TYPE_ENTRY, &fib_entry_vft);
fib_entry_logger = vlib_log_register_class("fib", "entry");
fib_entry_track_module_init();
}
fib_route_path_t *
fib_entry_encode (fib_node_index_t fib_entry_index)
{
fib_path_ext_list_t *ext_list;
fib_path_encode_ctx_t ctx = {
.rpaths = NULL,
};
fib_entry_t *fib_entry;
fib_entry_src_t *bsrc;
ext_list = NULL;
fib_entry = fib_entry_get(fib_entry_index);
bsrc = fib_entry_get_best_src_i(fib_entry);
if (bsrc)
{
ext_list = &bsrc->fes_path_exts;
}
if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent)
{
fib_path_list_walk_w_ext(fib_entry->fe_parent,
ext_list,
fib_path_encode,
&ctx);
}
return (ctx.rpaths);
}
const fib_prefix_t *
fib_entry_get_prefix (fib_node_index_t fib_entry_index)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
return (&fib_entry->fe_prefix);
}
u32
fib_entry_get_fib_index (fib_node_index_t fib_entry_index)
{
fib_entry_t *fib_entry;
fib_entry = fib_entry_get(fib_entry_index);
return (fib_entry->fe_fib_index);
}
u32
fib_entry_pool_size (void)
{
return (pool_elts(fib_entry_pool));
}
static clib_error_t *
show_fib_entry_command (vlib_main_t * vm,
unformat_input_t * input,
vlib_cli_command_t * cmd)
{
fib_node_index_t fei;
if (unformat (input, "%d", &fei))
{
/*
* show one in detail
*/
if (!pool_is_free_index(fib_entry_pool, fei))
{
vlib_cli_output (vm, "%d@%U",
fei,
format_fib_entry, fei,
FIB_ENTRY_FORMAT_DETAIL2);
}
else
{
vlib_cli_output (vm, "entry %d invalid", fei);
}
}
else
{
/*
* show all
*/
vlib_cli_output (vm, "FIB Entries:");
pool_foreach_index(fei, fib_entry_pool,
({
vlib_cli_output (vm, "%d@%U",
fei,
format_fib_entry, fei,
FIB_ENTRY_FORMAT_BRIEF);
}));
}
return (NULL);
}
VLIB_CLI_COMMAND (show_fib_entry, static) = {
.path = "show fib entry",
.function = show_fib_entry_command,
.short_help = "show fib entry",
};