aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/dpo/ip_null_dpo.c
blob: e8804c067bd3d991b5537d13accf2d80cd7dd87c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

@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: #22bb2
/*
 * 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.
 */
/**
 * @brief
 * The data-path object representing dropping the packet
 */

#include <vnet/dpo/ip_null_dpo.h>
#include <vnet/ip/ip.h>

/**
 * @brief A representation of the IP_NULL DPO
 */
typedef struct ip_null_dpo_t_
{
    /**
     * @brief The action to take on a packet
     */
    ip_null_dpo_action_t ind_action;
    /**
     * @brief The next VLIB node
     */
    u32 ind_next_index;
    /**
     * rate limits
     */
} ip_null_dpo_t;

/**
 * @brief the IP_NULL dpos are shared by all routes, hence they are global.
 * As the neame implies this is only for IP, hence 2.
 */
static ip_null_dpo_t ip_null_dpos[2 * IP_NULL_DPO_ACTION_NUM] = {
    [0] = {
	/* proto ip4, no action */
	.ind_action = IP_NULL_ACTION_NONE,
    },
    [1] = {
	/* proto ip4, action send unreach */
	.ind_action = IP_NULL_ACTION_SEND_ICMP_UNREACH,
    },
    [2] = {
	/* proto ip4, action send unreach */
	.ind_action = IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
    },
    [3] = {
	/* proto ip6, no action */
	.ind_action = IP_NULL_ACTION_NONE,
    },
    [4] = {
	/* proto ip6, action send unreach */
	.ind_action = IP_NULL_ACTION_SEND_ICMP_UNREACH,
    },
    [5] = {
	/* proto ip6, action send unreach */
	.ind_action = IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
    },
};

/**
 * @brief Action strings
 */
const char *ip_null_action_strings[] = IP_NULL_ACTIONS;

void
ip_null_dpo_add_and_lock (dpo_proto_t proto,
			  ip_null_dpo_action_t action,
			  dpo_id_t *dpo)
{
    int i;

    ASSERT((proto == DPO_PROTO_IP4) ||
	   (proto == DPO_PROTO_IP6));
    ASSERT(action < IP_NULL_DPO_ACTION_NUM);

    i = (proto == DPO_PROTO_IP4 ? 0 : 1);

    dpo_set(dpo, DPO_IP_NULL, proto, (i*IP_NULL_DPO_ACTION_NUM) + action);
}

always_inline const ip_null_dpo_t*
ip_null_dpo_get (index_t indi)
{
    return (&ip_null_dpos[indi]);
}

ip_null_dpo_action_t
ip_null_dpo_get_action (index_t indi)
{
    return (ip_null_dpos[indi].ind_action);
}

static void
ip_null_dpo_lock (dpo_id_t *dpo)
{
    /*
     * not maintaining a lock count on the ip_null, they are const global and
     * never die.
     */
}
static void
ip_null_dpo_unlock (dpo_id_t *dpo)
{
}

static u8*
format_ip_null_dpo (u8 *s, va_list *ap)
{
    index_t index = va_arg(*ap, index_t);
    CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
    const ip_null_dpo_t *ind;
    dpo_proto_t proto;

    ind = ip_null_dpo_get(index);
    proto = (index < IP_NULL_DPO_ACTION_NUM ? DPO_PROTO_IP4 : DPO_PROTO_IP6);

    return (format(s, "%U-null action:%s",
		   format_dpo_proto, proto,
		   ip_null_action_strings[ind->ind_action]));
}

const static dpo_vft_t ip_null_vft = {
    .dv_lock   = ip_null_dpo_lock,
    .dv_unlock = ip_null_dpo_unlock,
    .dv_format = format_ip_null_dpo,
};

/**
 * @brief The per-protocol VLIB graph nodes that are assigned to a ip_null
 *        object.
 *
 * this means that these graph nodes are ones from which a ip_null is the
 * parent object in the DPO-graph.
 */
const static char* const ip4_null_nodes[] =
{
    "ip4-null",
    NULL,
};
const static char* const ip6_null_nodes[] =
{
    "ip6-null",
    NULL,
};

const static char* const * const ip_null_nodes[DPO_PROTO_NUM] =
{
    [DPO_PROTO_IP4] = ip4_null_nodes,
    [DPO_PROTO_IP6] = ip6_null_nodes,
};

typedef struct ip_null_dpo_trace_t_
{
    index_t ind_index;
} ip_null_dpo_trace_t;

/**
 * @brief Exit nodes from a IP_NULL
 */
typedef enum ip_null_next_t_
{
    IP_NULL_NEXT_DROP,
    IP_NULL_NEXT_ICMP,
    IP_NULL_NEXT_NUM,
} ip_null_next_t;

always_inline uword
ip_null_dpo_switch (vlib_main_t * vm,
		    vlib_node_runtime_t * node,
		    vlib_frame_t * frame,
		    u8 is_ip4)
{
    u32 n_left_from, next_index, *from, *to_next;
    static f64 time_last_seed_change = -1e100;
    static u32 hash_seeds[3];
    static uword hash_bitmap[256 / BITS (uword)];
    f64 time_now;

    from = vlib_frame_vector_args (frame);
    n_left_from = frame->n_vectors;

    time_now = vlib_time_now (vm);
    if (time_now - time_last_seed_change > 1e-1)
    {
	uword i;
	u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
					       sizeof (hash_seeds));
	for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
	    hash_seeds[i] = r[i];

	/* Mark all hash keys as been not-seen before. */
	for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
	    hash_bitmap[i] = 0;

	time_last_seed_change = time_now;
    }

    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 a0, b0, c0, m0, drop0;
	    vlib_buffer_t *p0;
	    u32 bi0, indi0, next0;
	    const ip_null_dpo_t *ind0;
	    uword bm0;

	    bi0 = from[0];
	    to_next[0] = bi0;
	    from += 1;
	    to_next += 1;
	    n_left_from -= 1;
	    n_left_to_next -= 1;

	    p0 = vlib_get_buffer (vm, bi0);

	    /* lookup dst + src mac */
	    indi0 =  vnet_buffer (p0)->ip.adj_index[VLIB_TX];
	    ind0 = ip_null_dpo_get(indi0);
	    next0 = IP_NULL_NEXT_DROP;

	    /*
	     * rate limit - don't DoS the sender.
	     */
	    a0 = hash_seeds[0];
	    b0 = hash_seeds[1];
	    c0 = hash_seeds[2];

	    if (is_ip4)
	    {
		ip4_header_t *ip0 = vlib_buffer_get_current (p0);

		a0 ^= ip0->dst_address.data_u32;
		b0 ^= ip0->src_address.data_u32;

		hash_v3_finalize32 (a0, b0, c0);
	    }
	    else
	    {
		ip6_header_t *ip0 = vlib_buffer_get_current (p0);

		a0 ^= ip0->dst_address.as_u32[0];
		b0 ^= ip0->src_address.as_u32[0];
		c0 ^= ip0->src_address.as_u32[1];

		hash_v3_mix32 (a0, b0, c0);

		a0 ^= ip0->dst_address.as_u32[1];
		b0 ^= ip0->src_address.as_u32[2];
		c0 ^= ip0->src_address.as_u32[3];

		hash_v3_finalize32 (a0, b0, c0);
	    }

	    c0 &= BITS (hash_bitmap) - 1;
	    c0 = c0 / BITS (uword);
	    m0 = (uword) 1 << (c0 % BITS (uword));

	    bm0 = hash_bitmap[c0];
	    drop0 = (bm0 & m0) != 0;

	    /* Mark it as seen. */
	    hash_bitmap[c0] = bm0 | m0;

	    if (PREDICT_FALSE(!drop0))
	    {
		if (is_ip4)
		{
		    /*
		     * There's a trade-off here. This conditinal statement
		     * versus a graph node per-condition. Given the number
		     * expect number of packets to reach a null route is 0
		     * we favour the run-time cost over the graph complexity
		     */
		    if (IP_NULL_ACTION_SEND_ICMP_UNREACH == ind0->ind_action)
		    {
			next0 = IP_NULL_NEXT_ICMP;
			icmp4_error_set_vnet_buffer(
			    p0,
			    ICMP4_destination_unreachable,
			    ICMP4_destination_unreachable_destination_unreachable_host,
			    0);
		    }
		    else if (IP_NULL_ACTION_SEND_ICMP_PROHIBIT == ind0->ind_action)
		    {
			next0 = IP_NULL_NEXT_ICMP;
			icmp4_error_set_vnet_buffer(
			    p0,
			    ICMP4_destination_unreachable,
			    ICMP4_destination_unreachable_host_administratively_prohibited,
			    0);
		    }
		}
		else
		{
		    if (IP_NULL_ACTION_SEND_ICMP_UNREACH == ind0->ind_action)
		    {
			next0 = IP_NULL_NEXT_ICMP;
			icmp6_error_set_vnet_buffer(
			    p0,
			    ICMP6_destination_unreachable,
			    ICMP6_destination_unreachable_no_route_to_destination,
			    0);
		    }
		    else if (IP_NULL_ACTION_SEND_ICMP_PROHIBIT == ind0->ind_action)
		    {
			next0 = IP_NULL_NEXT_ICMP;
			icmp6_error_set_vnet_buffer(
			    p0,
			    ICMP6_destination_unreachable,
			    ICMP6_destination_unreachable_destination_administratively_prohibited,
			    0);
		    }
		}
	    }

	    if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
	    {
		ip_null_dpo_trace_t *tr = vlib_add_trace (vm, node, p0,
							  sizeof (*tr));
		tr->ind_index = indi0;
	    }
	    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);
    }

    return frame->n_vectors;
}

static u8 *
format_ip_null_dpo_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  ip_null_dpo_trace_t *t = va_arg (*args, ip_null_dpo_trace_t *);

  s = format (s, "%U", format_ip_null_dpo, t->ind_index, 0);
  return s;
}

static uword
ip4_null_dpo_switch (vlib_main_t * vm,
		    vlib_node_runtime_t * node,
		    vlib_frame_t * frame)
{
    return (ip_null_dpo_switch(vm, node, frame, 1));
}

/**
 * @brief
 */
VLIB_REGISTER_NODE (ip4_null_dpo_node) = {
  .function = ip4_null_dpo_switch,
  .name = "ip4-null",
  .vector_size = sizeof (u32),

  .format_trace = format_ip_null_dpo_trace,
  .n_next_nodes = IP_NULL_NEXT_NUM,
  .next_nodes = {
      [IP_NULL_NEXT_DROP] = "ip4-drop",
      [IP_NULL_NEXT_ICMP] = "ip4-icmp-error",
  },
};

static uword
ip6_null_dpo_switch (vlib_main_t * vm,
		    vlib_node_runtime_t * node,
		    vlib_frame_t * frame)
{
    return (ip_null_dpo_switch(vm, node, frame, 0));
}

/**
 * @brief
 */
VLIB_REGISTER_NODE (ip6_null_dpo_node) = {
  .function = ip6_null_dpo_switch,
  .name = "ip6-null",
  .vector_size = sizeof (u32),

  .format_trace = format_ip_null_dpo_trace,
  .n_next_nodes = IP_NULL_NEXT_NUM,
  .next_nodes = {
      [IP_NULL_NEXT_DROP] = "ip6-drop",
      [IP_NULL_NEXT_ICMP] = "ip6-icmp-error",
  },
};

void
ip_null_dpo_module_init (void)
{
    dpo_register(DPO_IP_NULL, &ip_null_vft, ip_null_nodes);
}