2 * Copyright (c) 2020 Doc.ai and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <wireguard/wireguard.h>
17 #include <wireguard/wireguard_peer.h>
19 #define foreach_wg_handoff_error \
20 _(CONGESTION_DROP, "congestion drop")
24 #define _(sym,str) WG_HANDOFF_ERROR_##sym,
25 foreach_wg_handoff_error
28 } ipsec_handoff_error_t;
30 static char *wg_handoff_error_strings[] = {
31 #define _(sym,string) string,
32 foreach_wg_handoff_error
43 typedef struct wg_handoff_trace_t_
45 u32 next_worker_index;
50 format_wg_handoff_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 wg_handoff_trace_t *t = va_arg (*args, wg_handoff_trace_t *);
56 s = format (s, "next-worker %d peer %d", t->next_worker_index, t->peer);
61 static_always_inline uword
62 wg_handoff (vlib_main_t * vm,
63 vlib_node_runtime_t * node,
64 vlib_frame_t * frame, u32 fq_index, wg_handoff_mode_t mode)
66 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
67 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
68 u32 n_enq, n_left_from, *from;
72 from = vlib_frame_vector_args (frame);
73 n_left_from = frame->n_vectors;
74 vlib_get_buffers (vm, from, bufs, n_left_from);
79 while (n_left_from > 0)
81 const wg_peer_t *peer;
82 index_t peeri = INDEX_INVALID;
84 if (PREDICT_FALSE (mode == WG_HANDOFF_HANDSHAKE))
88 else if (mode == WG_HANDOFF_INP_DATA)
90 message_data_t *data = vlib_buffer_get_current (b[0]);
92 wg_index_table_lookup (&wmp->index_table, data->receiver_index);
94 peer = wg_peer_get (peeri);
96 ti[0] = peer->input_thread_index;
101 wg_peer_get_by_adj_index (vnet_buffer (b[0])->
102 ip.adj_index[VLIB_TX]);
103 peer = wg_peer_get (peeri);
104 ti[0] = peer->output_thread_index;
107 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
109 wg_handoff_trace_t *t =
110 vlib_add_trace (vm, node, b[0], sizeof (*t));
111 t->next_worker_index = ti[0];
120 n_enq = vlib_buffer_enqueue_to_thread (vm, fq_index, from,
121 thread_indices, frame->n_vectors, 1);
123 if (n_enq < frame->n_vectors)
124 vlib_node_increment_counter (vm, node->node_index,
125 WG_HANDOFF_ERROR_CONGESTION_DROP,
126 frame->n_vectors - n_enq);
131 VLIB_NODE_FN (wg_handshake_handoff) (vlib_main_t * vm,
132 vlib_node_runtime_t * node,
133 vlib_frame_t * from_frame)
135 wg_main_t *wmp = &wg_main;
137 return wg_handoff (vm, node, from_frame, wmp->in_fq_index,
138 WG_HANDOFF_HANDSHAKE);
141 VLIB_NODE_FN (wg_input_data_handoff) (vlib_main_t * vm,
142 vlib_node_runtime_t * node,
143 vlib_frame_t * from_frame)
145 wg_main_t *wmp = &wg_main;
147 return wg_handoff (vm, node, from_frame, wmp->in_fq_index,
148 WG_HANDOFF_INP_DATA);
151 VLIB_NODE_FN (wg_output_tun_handoff) (vlib_main_t * vm,
152 vlib_node_runtime_t * node,
153 vlib_frame_t * from_frame)
155 wg_main_t *wmp = &wg_main;
157 return wg_handoff (vm, node, from_frame, wmp->out_fq_index,
162 VLIB_REGISTER_NODE (wg_handshake_handoff) =
164 .name = "wg-handshake-handoff",
165 .vector_size = sizeof (u32),
166 .format_trace = format_wg_handoff_trace,
167 .type = VLIB_NODE_TYPE_INTERNAL,
168 .n_errors = ARRAY_LEN (wg_handoff_error_strings),
169 .error_strings = wg_handoff_error_strings,
176 VLIB_REGISTER_NODE (wg_input_data_handoff) =
178 .name = "wg-input-data-handoff",
179 .vector_size = sizeof (u32),
180 .format_trace = format_wg_handoff_trace,
181 .type = VLIB_NODE_TYPE_INTERNAL,
182 .n_errors = ARRAY_LEN (wg_handoff_error_strings),
183 .error_strings = wg_handoff_error_strings,
190 VLIB_REGISTER_NODE (wg_output_tun_handoff) =
192 .name = "wg-output-tun-handoff",
193 .vector_size = sizeof (u32),
194 .format_trace = format_wg_handoff_trace,
195 .type = VLIB_NODE_TYPE_INTERNAL,
196 .n_errors = ARRAY_LEN (wg_handoff_error_strings),
197 .error_strings = wg_handoff_error_strings,
206 * fd.io coding-style-patch-verification: ON
209 * eval: (c-set-style "gnu")