aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/udp/udp_encap_node.c
blob: 1ebe79532f457066d76e9632b862914a1d7ea391 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (c) 2017-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/udp/udp_encap.h>
#include <vnet/udp/udp.h>

typedef struct udp4_encap_trace_t_
{
  udp_header_t udp;
  ip4_header_t ip;
} udp4_encap_trace_t;

typedef struct udp6_encap_trace_t_
{
  udp_header_t udp;
  ip6_header_t ip;
} udp6_encap_trace_t;

extern vlib_combined_counter_main_t udp_encap_counters;

static u8 *
format_udp4_encap_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 *);
  udp4_encap_trace_t *t;

  t = va_arg (*args, udp4_encap_trace_t *);

  s = format (s, "%U\n  %U",
	      format_ip4_header, &t->ip, sizeof (t->ip),
	      format_udp_header, &t->udp, sizeof (t->udp));
  return (s);
}

static u8 *
format_udp6_encap_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 *);
  udp6_encap_trace_t *t;

  t = va_arg (*args, udp6_encap_trace_t *);

  s = format (s, "%U\n  %U",
	      format_ip6_header, &t->ip, sizeof (t->ip),
	      format_udp_header, &t->udp, sizeof (t->udp));
  return (s);
}

always_inline uword
udp_encap_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
		  vlib_frame_t *frame, ip_address_family_t encap_family,
		  ip_address_family_t payload_family)
{
  vlib_combined_counter_main_t *cm = &udp_encap_counters;
  u32 *from = vlib_frame_vector_args (frame);
  u32 n_left_from, n_left_to_next, *to_next, next_index;
  u32 thread_index = vm->thread_index;

  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from >= 4 && n_left_to_next >= 2)
	{
	  vlib_buffer_t *b0, *b1;
	  udp_encap_t *ue0, *ue1;
	  u32 bi0, next0, uei0;
	  u32 bi1, next1, uei1;

	  /* Prefetch next iteration. */
	  {
	    vlib_buffer_t *p2, *p3;

	    p2 = vlib_get_buffer (vm, from[2]);
	    p3 = vlib_get_buffer (vm, from[3]);

	    vlib_prefetch_buffer_header (p2, STORE);
	    vlib_prefetch_buffer_header (p3, STORE);
	  }

	  bi0 = to_next[0] = from[0];
	  bi1 = to_next[1] = from[1];

	  from += 2;
	  n_left_from -= 2;
	  to_next += 2;
	  n_left_to_next -= 2;

	  b0 = vlib_get_buffer (vm, bi0);
	  b1 = vlib_get_buffer (vm, bi1);

	  uei0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
	  uei1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];

	  vlib_increment_combined_counter (cm, thread_index, uei0, 1,
					   vlib_buffer_length_in_chain (vm,
									b0));
	  vlib_increment_combined_counter (cm, thread_index, uei1, 1,
					   vlib_buffer_length_in_chain (vm,
									b1));

	  /* Rewrite packet header and updates lengths. */
	  ue0 = udp_encap_get (uei0);
	  ue1 = udp_encap_get (uei1);

	  /* Paint */
	  if (encap_family == AF_IP6)
	    {
	      const u8 n_bytes =
		sizeof (udp_header_t) + sizeof (ip6_header_t);
	      ip_udp_encap_two (vm, b0, b1, (u8 *) &ue0->ue_hdrs,
				(u8 *) &ue1->ue_hdrs, n_bytes, encap_family,
				payload_family);
	      if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  udp6_encap_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  tr->udp = ue0->ue_hdrs.ip6.ue_udp;
		  tr->ip = ue0->ue_hdrs.ip6.ue_ip6;
		}
	      if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
		{
		  udp6_encap_trace_t *tr =
		    vlib_add_trace (vm, node, b1, sizeof (*tr));
		  tr->udp = ue1->ue_hdrs.ip6.ue_udp;
		  tr->ip = ue1->ue_hdrs.ip6.ue_ip6;
		}
	    }
	  else
	    {
	      const u8 n_bytes =
		sizeof (udp_header_t) + sizeof (ip4_header_t);

	      ip_udp_encap_two (vm, b0, b1, (u8 *) &ue0->ue_hdrs,
				(u8 *) &ue1->ue_hdrs, n_bytes, encap_family,
				payload_family);

	      if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  udp4_encap_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  tr->udp = ue0->ue_hdrs.ip4.ue_udp;
		  tr->ip = ue0->ue_hdrs.ip4.ue_ip4;
		}
	      if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
		{
		  udp4_encap_trace_t *tr =
		    vlib_add_trace (vm, node, b1, sizeof (*tr));
		  tr->udp = ue1->ue_hdrs.ip4.ue_udp;
		  tr->ip = ue1->ue_hdrs.ip4.ue_ip4;
		}
	    }

	  next0 = ue0->ue_dpo.dpoi_next_node;
	  next1 = ue1->ue_dpo.dpoi_next_node;
	  vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ue0->ue_dpo.dpoi_index;
	  vnet_buffer (b1)->ip.adj_index[VLIB_TX] = ue1->ue_dpo.dpoi_index;

	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
					   to_next, n_left_to_next,
					   bi0, bi1, next0, next1);
	}

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0, next0, uei0;
	  vlib_buffer_t *b0;
	  udp_encap_t *ue0;

	  bi0 = to_next[0] = from[0];

	  from += 1;
	  n_left_from -= 1;
	  to_next += 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);

	  uei0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];

	  /* Rewrite packet header and updates lengths. */
	  ue0 = udp_encap_get (uei0);

	  vlib_increment_combined_counter (cm, thread_index, uei0, 1,
					   vlib_buffer_length_in_chain (vm,
									b0));

	  /* Paint */
	  if (encap_family == AF_IP6)
	    {
	      const u8 n_bytes =
		sizeof (udp_header_t) + sizeof (ip6_header_t);
	      ip_udp_encap_one (vm, b0, (u8 *) &ue0->ue_hdrs.ip6, n_bytes,
				encap_family, payload_family);

	      if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  udp6_encap_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  tr->udp = ue0->ue_hdrs.ip6.ue_udp;
		  tr->ip = ue0->ue_hdrs.ip6.ue_ip6;
		}
	    }
	  else
	    {
	      const u8 n_bytes =
		sizeof (udp_header_t) + sizeof (ip4_header_t);

	      ip_udp_encap_one (vm, b0, (u8 *) &ue0->ue_hdrs.ip4, n_bytes,
				encap_family, payload_family);

	      if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  udp4_encap_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  tr->udp = ue0->ue_hdrs.ip4.ue_udp;
		  tr->ip = ue0->ue_hdrs.ip4.ue_ip4;
		}
	    }

	  next0 = ue0->ue_dpo.dpoi_next_node;
	  vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ue0->ue_dpo.dpoi_index;

	  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;
}

VLIB_NODE_FN (udp4o4_encap_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return udp_encap_inline (vm, node, frame, AF_IP4, AF_IP4);
}

VLIB_NODE_FN (udp6o4_encap_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return udp_encap_inline (vm, node, frame, AF_IP4, AF_IP6);
}

VLIB_NODE_FN (udp4_encap_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return udp_encap_inline (vm, node, frame, AF_IP4, N_AF);
}

VLIB_NODE_FN (udp6o6_encap_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return udp_encap_inline (vm, node, frame, AF_IP6, AF_IP6);
}

VLIB_NODE_FN (udp4o6_encap_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return udp_encap_inline (vm, node, frame, AF_IP6, AF_IP4);
}

VLIB_NODE_FN (udp6_encap_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return udp_encap_inline (vm, node, frame, AF_IP6, N_AF);
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (udp4o4_encap_node) = {
  .name = "udp4o4-encap",
  .vector_size = sizeof (u32),
  .format_trace = format_udp4_encap_trace,
  .n_next_nodes = 0,
};

VLIB_REGISTER_NODE (udp6o4_encap_node) = {
  .name = "udp6o4-encap",
  .vector_size = sizeof (u32),
  .format_trace = format_udp6_encap_trace,
  .n_next_nodes = 0,
  .sibling_of = "udp4o4-encap",
};

VLIB_REGISTER_NODE (udp4_encap_node) = {
  .name = "udp4-encap",
  .vector_size = sizeof (u32),
  .format_trace = format_udp4_encap_trace,
  .n_next_nodes = 0,
  .sibling_of = "udp4o4-encap",
};

VLIB_REGISTER_NODE (udp6o6_encap_node) = {
  .name = "udp6o6-encap",
  .vector_size = sizeof (u32),
  .format_trace = format_udp6_encap_trace,
  .n_next_nodes = 0,
};

VLIB_REGISTER_NODE (udp4o6_encap_node) = {
  .name = "udp4o6-encap",
  .vector_size = sizeof (u32),
  .format_trace = format_udp4_encap_trace,
  .n_next_nodes = 0,
  .sibling_of = "udp6o6-encap",
};

VLIB_REGISTER_NODE (udp6_encap_node) = {
  .name = "udp6-encap",
  .vector_size = sizeof (u32),
  .format_trace = format_udp6_encap_trace,
  .n_next_nodes = 0,
  .sibling_of = "udp6o6-encap",
};
/* *INDENT-ON* */


/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
t">void sm_lock_and_del_segment_inline (segment_manager_t * sm, u32 fs_index) { fifo_segment_t *fs; u8 is_prealloc; clib_rwlock_writer_lock (&sm->segments_rwlock); fs = segment_manager_get_segment_if_valid (sm, fs_index); if (!fs) goto done; is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED; if (is_prealloc && !segment_manager_app_detached (sm)) goto done; segment_manager_del_segment (sm, fs); done: clib_rwlock_writer_unlock (&sm->segments_rwlock); } void segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index) { sm_lock_and_del_segment_inline (sm, fs_index); } /** * Reads a segment from the segment manager's pool without lock */ fifo_segment_t * segment_manager_get_segment (segment_manager_t * sm, u32 segment_index) { return pool_elt_at_index (sm->segments, segment_index); } u64 segment_manager_segment_handle (segment_manager_t * sm, fifo_segment_t * segment) { u32 segment_index = segment_manager_segment_index (sm, segment); return (((u64) segment_manager_index (sm) << 32) | segment_index); } u64 segment_manager_make_segment_handle (u32 segment_manager_index, u32 segment_index) { return (((u64) segment_manager_index << 32) | segment_index); } fifo_segment_t * segment_manager_get_segment_w_handle (u64 segment_handle) { u32 sm_index, segment_index; segment_manager_t *sm; segment_manager_parse_segment_handle (segment_handle, &sm_index, &segment_index); sm = segment_manager_get (sm_index); if (!sm || pool_is_free_index (sm->segments, segment_index)) return 0; return pool_elt_at_index (sm->segments, segment_index); } /** * Reads a segment from the segment manager's pool and acquires reader lock * * Caller must drop the reader's lock by calling * @ref segment_manager_segment_reader_unlock once it finishes working with * the segment. */ fifo_segment_t * segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index) { clib_rwlock_reader_lock (&sm->segments_rwlock); return pool_elt_at_index (sm->segments, segment_index); } void segment_manager_segment_reader_lock (segment_manager_t * sm) { clib_rwlock_reader_lock (&sm->segments_rwlock); } void segment_manager_segment_reader_unlock (segment_manager_t * sm) { clib_rwlock_reader_unlock (&sm->segments_rwlock); } void segment_manager_segment_writer_unlock (segment_manager_t * sm) { clib_rwlock_writer_unlock (&sm->segments_rwlock); } segment_manager_t * segment_manager_alloc (void) { segment_manager_main_t *smm = &sm_main; segment_manager_t *sm; pool_get_zero (smm->segment_managers, sm); clib_rwlock_init (&sm->segments_rwlock); return sm; } int segment_manager_init (segment_manager_t * sm) { segment_manager_props_t *props; props = segment_manager_properties_get (sm); sm->max_fifo_size = props->max_fifo_size ? props->max_fifo_size : sm_main.default_max_fifo_size; sm->max_fifo_size = clib_max (sm->max_fifo_size, 4096); segment_manager_set_watermarks (sm, props->high_watermark, props->low_watermark); return 0; } /** * Initializes segment manager based on options provided. * Returns error if ssvm segment(s) allocation fails. */ int segment_manager_init_first (segment_manager_t * sm) { segment_manager_props_t *props; uword first_seg_size; fifo_segment_t *fs; int fs_index, i; segment_manager_init (sm); props = segment_manager_properties_get (sm); first_seg_size = clib_max (props->segment_size, sm_main.default_segment_size); if (props->prealloc_fifos) { u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10); u32 rx_rounded_data_size, tx_rounded_data_size; u32 prealloc_fifo_pairs = props->prealloc_fifos; u32 rx_fifo_size, tx_fifo_size, pair_size; u32 approx_segment_count; /* Figure out how many segments should be preallocated */ rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size))); tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size))); rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size; tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size; pair_size = rx_fifo_size + tx_fifo_size; approx_total_size = (u64) prealloc_fifo_pairs *pair_size; if (first_seg_size > approx_total_size) max_seg_size = first_seg_size; approx_segment_count = (approx_total_size + (max_seg_size - 1)) / max_seg_size; /* Allocate the segments */ for (i = 0; i < approx_segment_count + 1; i++) { fs_index = segment_manager_add_segment (sm, max_seg_size, 0); if (fs_index < 0) { clib_warning ("Failed to preallocate segment %d", i); return fs_index; } fs = segment_manager_get_segment (sm, fs_index); if (i == 0) sm->event_queue = segment_manager_alloc_queue (fs, props); fifo_segment_preallocate_fifo_pairs (fs, props->rx_fifo_size, props->tx_fifo_size, &prealloc_fifo_pairs); fifo_segment_flags (fs) = FIFO_SEGMENT_F_IS_PREALLOCATED; if (prealloc_fifo_pairs == 0) break; } return 0; } fs_index = segment_manager_add_segment (sm, first_seg_size, 0); if (fs_index < 0) { clib_warning ("Failed to allocate segment"); return fs_index; } fs = segment_manager_get_segment (sm, fs_index); sm->event_queue = segment_manager_alloc_queue (fs, props); if (props->prealloc_fifo_hdrs) { u32 hdrs_per_slice; /* Do not preallocate on slice associated to main thread */ i = (vlib_num_workers ()? 1 : 0); hdrs_per_slice = props->prealloc_fifo_hdrs / (fs->n_slices - i); for (; i < fs->n_slices; i++) { if (fifo_segment_prealloc_fifo_hdrs (fs, i, hdrs_per_slice)) return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL; } } return 0; } void segment_manager_cleanup_detached_listener (segment_manager_t * sm) { app_worker_t *app_wrk; app_wrk = app_worker_get_if_valid (sm->app_wrk_index); if (!app_wrk) return; app_worker_del_detached_sm (app_wrk, segment_manager_index (sm)); } /** * Cleanup segment manager. */ void segment_manager_free (segment_manager_t * sm) { segment_manager_main_t *smm = &sm_main; fifo_segment_t *fifo_segment; ASSERT (vlib_get_thread_index () == 0 && !segment_manager_has_fifos (sm) && segment_manager_app_detached (sm)); if (sm->flags & SEG_MANAGER_F_DETACHED_LISTENER) segment_manager_cleanup_detached_listener (sm); /* If we have empty preallocated segments that haven't been removed, remove * them now. Apart from that, the first segment in the first segment manager * is not removed when all fifos are removed. It can only be removed when * the manager is explicitly deleted/detached by the app. */ clib_rwlock_writer_lock (&sm->segments_rwlock); /* *INDENT-OFF* */ pool_foreach (fifo_segment, sm->segments) { segment_manager_del_segment (sm, fifo_segment); } /* *INDENT-ON* */ pool_free (sm->segments); clib_rwlock_writer_unlock (&sm->segments_rwlock); clib_rwlock_free (&sm->segments_rwlock); if (CLIB_DEBUG) clib_memset (sm, 0xfe, sizeof (*sm)); pool_put (smm->segment_managers, sm); } static void sm_free_w_index_helper (void *arg) { u32 sm_index = *(u32 *) arg; segment_manager_t *sm; ASSERT (vlib_get_thread_index () == 0); if ((sm = segment_manager_get_if_valid (sm_index))) segment_manager_free (sm); } static void segment_manager_free_safe (segment_manager_t * sm) { if (!vlib_thread_is_main_w_barrier ()) { u32 sm_index = segment_manager_index (sm); vlib_rpc_call_main_thread (sm_free_w_index_helper, (u8 *) & sm_index, sizeof (sm_index)); } else { segment_manager_free (sm); } } void segment_manager_init_free (segment_manager_t * sm) { ASSERT (vlib_get_thread_index () == 0); segment_manager_app_detach (sm); if (segment_manager_has_fifos (sm)) segment_manager_del_sessions (sm); else { ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm)); segment_manager_free (sm); } } segment_manager_t * segment_manager_get (u32 index) { return pool_elt_at_index (sm_main.segment_managers, index); } segment_manager_t * segment_manager_get_if_valid (u32 index) { if (pool_is_free_index (sm_main.segment_managers, index)) return 0; return pool_elt_at_index (sm_main.segment_managers, index); } u32 segment_manager_index (segment_manager_t * sm) { return sm - sm_main.segment_managers; } u8 segment_manager_has_fifos (segment_manager_t * sm) { fifo_segment_t *seg; u8 first = 1; /* *INDENT-OFF* */ segment_manager_foreach_segment_w_lock (seg, sm, ({ if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg) && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED)) { clib_warning ("segment %d has no fifos!", segment_manager_segment_index (sm, seg)); first = 0; } if (fifo_segment_has_fifos (seg)) { segment_manager_segment_reader_unlock (sm); return 1; } })); /* *INDENT-ON* */ return 0; } /** * Initiate disconnects for all sessions 'owned' by a segment manager */ void segment_manager_del_sessions (segment_manager_t * sm) { session_handle_t *handles = 0, *handle; fifo_segment_t *fs; session_t *session; int slice_index; svm_fifo_t *f; ASSERT (pool_elts (sm->segments) != 0); /* Across all fifo segments used by the server */ /* *INDENT-OFF* */ segment_manager_foreach_segment_w_lock (fs, sm, ({ for (slice_index = 0; slice_index < fs->n_slices; slice_index++) { f = fifo_segment_get_slice_fifo_list (fs, slice_index); /* * Remove any residual sessions from the session lookup table * Don't bother deleting the individual fifos, we're going to * throw away the fifo segment in a minute. */ while (f) { session = session_get_if_valid (f->shr->master_session_index, f->master_thread_index); if (session) vec_add1 (handles, session_handle (session)); f = f->next; } } /* Instead of removing the segment, test when cleaning up disconnected * sessions if the segment can be removed. */ })); /* *INDENT-ON* */ vec_foreach (handle, handles) { session = session_get_from_handle (*handle); session_close (session); /* Avoid propagating notifications back to the app */ session->app_wrk_index = APP_INVALID_INDEX; } vec_free (handles); } int segment_manager_try_alloc_fifos (fifo_segment_t * fifo_segment, u32 thread_index, u32 rx_fifo_size, u32 tx_fifo_size, svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo) { rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size); *rx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index, rx_fifo_size, FIFO_SEGMENT_RX_FIFO); tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size); *tx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index, tx_fifo_size, FIFO_SEGMENT_TX_FIFO); if (*rx_fifo == 0) { /* This would be very odd, but handle it... */ if (*tx_fifo != 0) { fifo_segment_free_fifo (fifo_segment, *tx_fifo); *tx_fifo = 0; } return -1; } if (*tx_fifo == 0) { if (*rx_fifo != 0) { fifo_segment_free_fifo (fifo_segment, *rx_fifo); *rx_fifo = 0; } return -1; } return 0; } int segment_manager_alloc_session_fifos (segment_manager_t * sm, u32 thread_index, svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo) { int alloc_fail = 1, rv = 0, new_fs_index; uword free_bytes, max_free_bytes = 0; segment_manager_props_t *props; fifo_segment_t *fs = 0, *cur; u32 sm_index, fs_index; props = segment_manager_properties_get (sm); /* * Find the first free segment to allocate the fifos in */ segment_manager_segment_reader_lock (sm); /* *INDENT-OFF* */ pool_foreach (cur, sm->segments) { free_bytes = fifo_segment_available_bytes (cur); if (free_bytes > max_free_bytes) { max_free_bytes = free_bytes; fs = cur; } } /* *INDENT-ON* */ if (fs) { alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index, props->rx_fifo_size, props->tx_fifo_size, rx_fifo, tx_fifo); /* On success, keep lock until fifos are initialized */ if (!alloc_fail) goto alloc_success; } segment_manager_segment_reader_unlock (sm); /* * Allocation failed, see if we can add a new segment */ if (props->add_segment) { if ((new_fs_index = segment_manager_add_segment (sm, 0, 1)) < 0) { clib_warning ("Failed to add new segment"); return SESSION_E_SEG_CREATE; } fs = segment_manager_get_segment_w_lock (sm, new_fs_index); alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index, props->rx_fifo_size, props->tx_fifo_size, rx_fifo, tx_fifo); if (alloc_fail) { clib_warning ("Added a segment, still can't allocate a fifo"); segment_manager_segment_reader_unlock (sm); return SESSION_E_SEG_NO_SPACE2; } } else { SESSION_DBG ("Can't add new seg and no space to allocate fifos!"); return SESSION_E_SEG_NO_SPACE; } alloc_success: ASSERT (rx_fifo && tx_fifo); sm_index = segment_manager_index (sm); fs_index = segment_manager_segment_index (sm, fs); (*tx_fifo)->segment_manager = sm_index; (*rx_fifo)->segment_manager = sm_index; (*tx_fifo)->segment_index = fs_index; (*rx_fifo)->segment_index = fs_index; /* Drop the lock after app is notified */ segment_manager_segment_reader_unlock (sm); return rv; } void segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo) { segment_manager_t *sm; fifo_segment_t *fs; u32 segment_index; if (!rx_fifo || !tx_fifo) return; /* It's possible to have no segment manager if the session was removed * as result of a detach. */ if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager))) return; segment_index = rx_fifo->segment_index; fs = segment_manager_get_segment_w_lock (sm, segment_index); fifo_segment_free_fifo (fs, rx_fifo); fifo_segment_free_fifo (fs, tx_fifo); /* * Try to remove svm segment if it has no fifos. This can be done only if * the segment is not the first in the segment manager or if it is first * and it is not protected. Moreover, if the segment is first and the app * has detached from the segment manager, remove the segment manager. */ if (!fifo_segment_has_fifos (fs)) { segment_manager_segment_reader_unlock (sm); /* Remove segment if it holds no fifos or first but not protected */ if (segment_index != 0 || !sm->first_is_protected) sm_lock_and_del_segment_inline (sm, segment_index); /* Remove segment manager if no sessions and detached from app */ if (segment_manager_app_detached (sm) && !segment_manager_has_fifos (sm)) segment_manager_free_safe (sm); } else segment_manager_segment_reader_unlock (sm); } void segment_manager_detach_fifo (segment_manager_t *sm, svm_fifo_t **f) { fifo_segment_t *fs; fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index); fifo_segment_detach_fifo (fs, f); segment_manager_segment_reader_unlock (sm); } void segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f, session_t *s) { fifo_segment_t *fs; fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index); fifo_segment_attach_fifo (fs, f, s->thread_index); segment_manager_segment_reader_unlock (sm); (*f)->shr->master_session_index = s->session_index; (*f)->master_thread_index = s->thread_index; } u32 segment_manager_evt_q_expected_size (u32 q_len) { u32 fifo_evt_size, notif_q_size, q_hdrs; u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz; fifo_evt_size = 1 << max_log2 (sizeof (session_event_t)); notif_q_size = clib_max (16, q_len >> 4); msg_q_sz = q_len * sizeof (svm_msg_q_msg_t); fifo_evt_ring_sz = q_len * fifo_evt_size; session_ntf_ring_sz = notif_q_size * 256; q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t); return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs); } /** * Allocates shm queue in the first segment * * Must be called with lock held */ svm_msg_q_t * segment_manager_alloc_queue (fifo_segment_t * segment, segment_manager_props_t * props) { u32 fifo_evt_size, session_evt_size = 256, notif_q_size; svm_msg_q_cfg_t _cfg, *cfg = &_cfg; svm_msg_q_t *q; fifo_evt_size = sizeof (session_event_t); notif_q_size = clib_max (16, props->evt_q_size >> 4); /* *INDENT-OFF* */ svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = { {props->evt_q_size, fifo_evt_size, 0}, {notif_q_size, session_evt_size, 0} }; /* *INDENT-ON* */ cfg->consumer_pid = 0; cfg->n_rings = 2; cfg->q_nitems = props->evt_q_size; cfg->ring_cfgs = rc; q = fifo_segment_msg_q_alloc (segment, 0, cfg); if (props->use_mq_eventfd) { if (svm_msg_q_alloc_eventfd (q)) clib_warning ("failed to alloc eventfd"); } return q; } svm_msg_q_t * segment_manager_event_queue (segment_manager_t * sm) { return sm->event_queue; } /** * Frees shm queue allocated in the first segment */ void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q) { fifo_segment_t *segment; ssvm_shared_header_t *sh; void *oldheap; ASSERT (!pool_is_free_index (sm->segments, 0)); segment = segment_manager_get_segment_w_lock (sm, 0); sh = segment->ssvm.sh; oldheap = ssvm_push_heap (sh); svm_queue_free (q); ssvm_pop_heap (oldheap); segment_manager_segment_reader_unlock (sm); } /* * Init segment vm address allocator */ void segment_manager_main_init (void) { segment_manager_main_t *sm = &sm_main; sm->default_fifo_size = 1 << 12; sm->default_segment_size = 1 << 20; sm->default_app_mq_size = 128; sm->default_max_fifo_size = 4 << 20; sm->default_high_watermark = 80; sm->default_low_watermark = 50; } static clib_error_t * segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { segment_manager_main_t *smm = &sm_main; u8 show_segments = 0, verbose = 0; uword max_fifo_size; segment_manager_t *sm; fifo_segment_t *seg; app_worker_t *app_wrk; application_t *app; u8 custom_logic; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "segments")) show_segments = 1; else if (unformat (input, "verbose")) verbose = 1; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } vlib_cli_output (vm, "%d segment managers allocated", pool_elts (smm->segment_managers)); if (verbose && pool_elts (smm->segment_managers)) { vlib_cli_output (vm, "%-6s%=10s%=10s%=13s%=11s%=11s%=12s", "Index", "AppIndex", "Segments", "MaxFifoSize", "HighWater", "LowWater", "FifoTuning"); /* *INDENT-OFF* */ pool_foreach (sm, smm->segment_managers) { app_wrk = app_worker_get_if_valid (sm->app_wrk_index); app = app_wrk ? application_get (app_wrk->app_index) : 0; custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0; max_fifo_size = sm->max_fifo_size; vlib_cli_output (vm, "%-6d%=10d%=10d%=13U%=11d%=11d%=12s", segment_manager_index (sm), sm->app_wrk_index, pool_elts (sm->segments), format_memory_size, max_fifo_size, sm->high_watermark, sm->low_watermark, custom_logic ? "custom" : "none"); } /* *INDENT-ON* */ vlib_cli_output (vm, "\n"); } if (show_segments) { vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose); /* *INDENT-OFF* */ pool_foreach (sm, smm->segment_managers) { segment_manager_foreach_segment_w_lock (seg, sm, ({ vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose); })); } /* *INDENT-ON* */ } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (segment_manager_show_command, static) = { .path = "show segment-manager", .short_help = "show segment-manager [segments][verbose]", .function = segment_manager_show_fn, }; /* *INDENT-ON* */ void segment_manager_format_sessions (segment_manager_t * sm, int verbose) { vlib_main_t *vm = vlib_get_main (); app_worker_t *app_wrk; fifo_segment_t *fs; const u8 *app_name; int slice_index; u8 *s = 0, *str; svm_fifo_t *f; if (!sm) { if (verbose) vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App", "API Client", "SegManager"); else vlib_cli_output (vm, "%-40s%-20s", "Connection", "App"); return; } app_wrk = app_worker_get (sm->app_wrk_index); app_name = application_name_from_index (app_wrk->app_index); clib_rwlock_reader_lock (&sm->segments_rwlock); /* *INDENT-OFF* */ pool_foreach (fs, sm->segments) { for (slice_index = 0; slice_index < fs->n_slices; slice_index++) { f = fifo_segment_get_slice_fifo_list (fs, slice_index); while (f) { u32 session_index, thread_index; session_t *session; session_index = f->shr->master_session_index; thread_index = f->master_thread_index; session = session_get (session_index, thread_index); str = format (0, "%U", format_session, session, verbose); if (verbose) s = format (s, "%-40v%-20v%-15u%-10u", str, app_name, app_wrk->api_client_index, app_wrk->connects_seg_manager); else s = format (s, "%-40v%-20v", str, app_name); vlib_cli_output (vm, "%v", s); vec_reset_length (s); vec_free (str); f = f->next; } vec_free (s); } } /* *INDENT-ON* */ clib_rwlock_reader_unlock (&sm->segments_rwlock); } void segment_manager_set_watermarks (segment_manager_t * sm, u8 high_watermark, u8 low_watermark) { ASSERT (high_watermark <= 100 && low_watermark <= 100 && low_watermark <= high_watermark); sm->high_watermark = high_watermark; sm->low_watermark = low_watermark; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */