aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/cop/ip4_whitelist.c
blob: 1b5e336b75207fabcb52f7c2101d76fe07ac2e2c (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
/*
 * 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/cop/cop.h>
#include <vnet/fib/ip4_fib.h>
#include <vnet/dpo/load_balance.h>

typedef struct {
  u32 next_index;
  u32 sw_if_index;
} ip4_cop_whitelist_trace_t;

/* packet trace format function */
static u8 * format_ip4_cop_whitelist_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 *);
  ip4_cop_whitelist_trace_t * t = va_arg (*args, ip4_cop_whitelist_trace_t *);
  
  s = format (s, "IP4_COP_WHITELIST: sw_if_index %d, next index %d",
              t->sw_if_index, t->next_index);
  return s;
}

vlib_node_registration_t ip4_cop_whitelist_node;

#define foreach_ip4_cop_whitelist_error                         \
_(DROPPED, "ip4 cop whitelist packets dropped")

typedef enum {
#define _(sym,str) IP4_COP_WHITELIST_ERROR_##sym,
  foreach_ip4_cop_whitelist_error
#undef _
  IP4_COP_WHITELIST_N_ERROR,
} ip4_cop_whitelist_error_t;

static char * ip4_cop_whitelist_error_strings[] = {
#define _(sym,string) string,
  foreach_ip4_cop_whitelist_error
#undef _
};

static uword
ip4_cop_whitelist_node_fn (vlib_main_t * vm,
		  vlib_node_runtime_t * node,
		  vlib_frame_t * frame)
{
  u32 n_left_from, * from, * to_next;
  cop_feature_type_t next_index;
  cop_main_t *cm = &cop_main;
  vlib_combined_counter_main_t * vcm = &load_balance_main.lbm_via_counters;
  u32 thread_index = vm->thread_index;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index,
			   to_next, n_left_to_next);

      while (n_left_from >= 4 && n_left_to_next >= 2)
      	{
          u32 bi0, bi1;
          vlib_buffer_t * b0, * b1;
          u32 next0, next1;
          u32 sw_if_index0, sw_if_index1;
          ip4_header_t * ip0, * ip1;
          cop_config_main_t * ccm0, * ccm1;
          cop_config_data_t * c0, * c1;
      	  ip4_fib_mtrie_t * mtrie0, * mtrie1;
      	  ip4_fib_mtrie_leaf_t leaf0, leaf1;
          u32 lb_index0, lb_index1;
          const load_balance_t * lb0, *lb1;
          const dpo_id_t *dpo0, *dpo1;

      	  /* 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, LOAD);
      	    vlib_prefetch_buffer_header (p3, LOAD);

      	    CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
      	    CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
      	  }

          /* speculatively enqueue b0 and b1 to the current next frame */
      	  to_next[0] = bi0 = from[0];
      	  to_next[1] = bi1 = from[1];
      	  from += 2;
      	  to_next += 2;
      	  n_left_from -= 2;
      	  n_left_to_next -= 2;

      	  b0 = vlib_get_buffer (vm, bi0);
          sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];

      	  ip0 = vlib_buffer_get_current (b0);

      	  ccm0 = cm->cop_config_mains + VNET_COP_IP4;

      	  c0 = vnet_get_config_data
              (&ccm0->config_main,
               &vnet_buffer (b0)->cop.current_config_index,
               &next0,
               sizeof (c0[0]));

	  mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;

          leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);

      	  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
                                             &ip0->src_address, 2);

      	  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
                                             &ip0->src_address, 3);

      	  lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);

	  ASSERT (lb_index0
                  == ip4_fib_table_lookup_lb (ip4_fib_get(c0->fib_index),
	  				       &ip0->src_address));
	  lb0 = load_balance_get (lb_index0);
          dpo0 = load_balance_get_bucket_i(lb0, 0);

          if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
            {
              b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
              next0 = RX_COP_DROP;
            }

      	  b1 = vlib_get_buffer (vm, bi1);
          sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];

      	  ip1 = vlib_buffer_get_current (b1);

      	  ccm1 = cm->cop_config_mains + VNET_COP_IP4;

      	  c1 = vnet_get_config_data
              (&ccm1->config_main,
               &vnet_buffer (b1)->cop.current_config_index,
               &next1,
               sizeof (c1[0]));
	  mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie;

          leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address);

      	  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
                                             &ip1->src_address, 2);

      	  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
                                             &ip1->src_address, 3);

      	  lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
	  ASSERT (lb_index1
                  == ip4_fib_table_lookup_lb (ip4_fib_get(c1->fib_index),
	  				       &ip1->src_address));
	  lb1 = load_balance_get (lb_index1);
          dpo1 = load_balance_get_bucket_i(lb1, 0);

          vlib_increment_combined_counter
              (vcm, thread_index, lb_index0, 1,
               vlib_buffer_length_in_chain (vm, b0)
               + sizeof(ethernet_header_t));

          vlib_increment_combined_counter
              (vcm, thread_index, lb_index1, 1,
               vlib_buffer_length_in_chain (vm, b1)
               + sizeof(ethernet_header_t));


          if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE))
            {
              b1->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
              next1 = RX_COP_DROP;
            }

          if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
                            && (b0->flags & VLIB_BUFFER_IS_TRACED)))
            {
              ip4_cop_whitelist_trace_t *t =
                 vlib_add_trace (vm, node, b0, sizeof (*t));
              t->sw_if_index = sw_if_index0;
              t->next_index = next0;
            }

          if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
                            && (b1->flags & VLIB_BUFFER_IS_TRACED)))
            {
              ip4_cop_whitelist_trace_t *t =
                 vlib_add_trace (vm, node, b1, sizeof (*t));
              t->sw_if_index = sw_if_index1;
              t->next_index = next1;
            }

          /* verify speculative enqueues, maybe switch current next frame */
          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;
	  vlib_buffer_t * b0;
          u32 next0;
          u32 sw_if_index0;
          ip4_header_t * ip0;
          cop_config_main_t *ccm0;
          cop_config_data_t *c0;
	  ip4_fib_mtrie_t * mtrie0;
	  ip4_fib_mtrie_leaf_t leaf0;
          u32 lb_index0;
          const load_balance_t * lb0;
          const dpo_id_t *dpo0;

          /* speculatively enqueue b0 to the current next frame */
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
          sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];

	  ip0 = vlib_buffer_get_current (b0);

	  ccm0 = cm->cop_config_mains + VNET_COP_IP4;

	  c0 = vnet_get_config_data 
              (&ccm0->config_main,
               &vnet_buffer (b0)->cop.current_config_index,
               &next0,
               sizeof (c0[0]));

	  mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;

          leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);

	  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, 
                                             &ip0->src_address, 2);

	  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, 
                                             &ip0->src_address, 3);

	  lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);

	  ASSERT (lb_index0 
                  == ip4_fib_table_lookup_lb (ip4_fib_get(c0->fib_index),
					      &ip0->src_address));

	  lb0 = load_balance_get (lb_index0);
          dpo0 = load_balance_get_bucket_i(lb0, 0);

          vlib_increment_combined_counter 
              (vcm, thread_index, lb_index0, 1,
               vlib_buffer_length_in_chain (vm, b0) 
               + sizeof(ethernet_header_t));

          if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
            {
              b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
              next0 = RX_COP_DROP;
            }

          if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
                            && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
            {
              ip4_cop_whitelist_trace_t *t = 
                 vlib_add_trace (vm, node, b0, sizeof (*t));
              t->sw_if_index = sw_if_index0;
              t->next_index = next0;
            }
            
          /* verify speculative enqueue, maybe switch current next frame */
	  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_REGISTER_NODE (ip4_cop_whitelist_node) = {
  .function = ip4_cop_whitelist_node_fn,
  .name = "ip4-cop-whitelist",
  .vector_size = sizeof (u32),
  .format_trace = format_ip4_cop_whitelist_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,
  
  .n_errors = ARRAY_LEN(ip4_cop_whitelist_error_strings),
  .error_strings = ip4_cop_whitelist_error_strings,

  .n_next_nodes = COP_RX_N_FEATURES,

  /* edit / add dispositions here */
  .next_nodes = {
    [IP4_RX_COP_WHITELIST] = "ip4-cop-whitelist",
    [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist",
    [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist",
    [IP4_RX_COP_INPUT] = "ip4-input",
    [IP6_RX_COP_INPUT] = "ip6-input",
    [DEFAULT_RX_COP_INPUT] = "ethernet-input",
    [RX_COP_DROP] = "error-drop",
  },
};

VLIB_NODE_FUNCTION_MULTIARCH (ip4_cop_whitelist_node, ip4_cop_whitelist_node_fn)

static clib_error_t *
ip4_whitelist_init (vlib_main_t * vm)
{
  return 0;
}

VLIB_INIT_FUNCTION (ip4_whitelist_init);
vec_append (rp->unprocessed_input, socket_main.input_buffer); msg_buffer = rp->unprocessed_input; } else { msg_buffer = socket_main.input_buffer; } /* Loop to process any full messages. */ ASSERT (vec_len (msg_buffer) > 0); do { /* Here, we are not sure how big a chunk of message we have left. */ /* Do we at least know how big the full message will be? */ if (vec_len (msg_buffer) <= sizeof (msgbuf_t)) /* No, so fragment is not a full message. */ goto save_and_split; /* Now we know how big the full message will be. */ msgbuf_len = ntohl (((msgbuf_t *) msg_buffer)->data_len) + sizeof (msgbuf_t); /* But do we have a full message? */ if (msgbuf_len > vec_len (msg_buffer)) { save_and_split: /* We don't have the entire message yet. */ /* If msg_buffer is unprocessed_input, nothing needs to be done. */ if (msg_buffer == socket_main.input_buffer) /* But if we were using the input buffer, save the fragment. */ { ASSERT (vec_len (rp->unprocessed_input) == 0); vec_validate (rp->unprocessed_input, vec_len (msg_buffer) - 1); clib_memcpy_fast (rp->unprocessed_input, msg_buffer, vec_len (msg_buffer)); _vec_len (rp->unprocessed_input) = vec_len (msg_buffer); } /* No more full messages, restore original input_buffer length. */ _vec_len (socket_main.input_buffer) = save_input_buffer_length; return 0; } /* * We have at least one full message. * But msg_buffer can contain more data, so copy one message data * so we can overwrite its length to what single message has. */ data_for_process = (u8 *) vec_dup (msg_buffer); _vec_len (data_for_process) = msgbuf_len; /* Everything is ready to signal the SOCKET_READ_EVENT. */ pool_get (socket_main.process_args, a); a->reg_index = reg_index; a->data = data_for_process; vlib_process_signal_event (vm, vl_api_clnt_node.index, SOCKET_READ_EVENT, a - socket_main.process_args); if (vec_len (msg_buffer) > msgbuf_len) /* There are some fragments left. Shrink the msg_buffer to simplify logic. */ vec_delete (msg_buffer, msgbuf_len, 0); else /* We are done with msg_buffer. */ _vec_len (msg_buffer) = 0; } while (vec_len (msg_buffer) > 0); /* Restore input_buffer, it could have been msg_buffer. */ _vec_len (socket_main.input_buffer) = save_input_buffer_length; return 0; } clib_error_t * vl_socket_write_ready (clib_file_t * uf) { clib_file_main_t *fm = &file_main; vl_api_registration_t *rp; int n; rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); /* Flush output vector. */ size_t total_bytes = vec_len (rp->output_vector); size_t bytes_to_send, remaining_bytes = total_bytes; void *p = rp->output_vector; while (remaining_bytes > 0) { bytes_to_send = remaining_bytes > 4096 ? 4096 : remaining_bytes; n = write (uf->file_descriptor, p, bytes_to_send); if (n < 0) { if (errno == EAGAIN) { break; } #if DEBUG > 2 clib_warning ("write error, close the file...\n"); #endif clib_file_del (fm, uf); vl_socket_free_registration_index (rp - socket_main.registration_pool); return 0; } remaining_bytes -= bytes_to_send; p += bytes_to_send; } vec_delete (rp->output_vector, total_bytes - remaining_bytes, 0); if (vec_len (rp->output_vector) <= 0 && (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE)) { uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY); } return 0; } clib_error_t * vl_socket_error_ready (clib_file_t * uf) { vl_api_registration_t *rp; clib_file_main_t *fm = &file_main; rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); clib_file_del (fm, uf); vl_socket_free_registration_index (rp - socket_main.registration_pool); return 0; } void socksvr_file_add (clib_file_main_t * fm, int fd) { vl_api_registration_t *rp; clib_file_t template = { 0 }; pool_get (socket_main.registration_pool, rp); clib_memset (rp, 0, sizeof (*rp)); template.read_function = vl_socket_read_ready; template.write_function = vl_socket_write_ready; template.error_function = vl_socket_error_ready; template.file_descriptor = fd; template.private_data = rp - socket_main.registration_pool; rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER; rp->vl_api_registration_pool_index = rp - socket_main.registration_pool; rp->clib_file_index = clib_file_add (fm, &template); } static clib_error_t * socksvr_accept_ready (clib_file_t * uf) { clib_file_main_t *fm = &file_main; socket_main_t *sm = &socket_main; clib_socket_t *sock = &sm->socksvr_listen_socket; clib_socket_t client; clib_error_t *error; error = clib_socket_accept (sock, &client); if (error) return error; socksvr_file_add (fm, client.fd); return 0; } static clib_error_t * socksvr_bogus_write (clib_file_t * uf) { clib_warning ("why am I here?"); return 0; } /* * vl_api_sockclnt_create_t_handler */ void vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp) { vl_api_registration_t *regp; vl_api_sockclnt_create_reply_t *rp; api_main_t *am = vlibapi_get_main (); hash_pair_t *hp; int rv = 0; u32 nmsg = hash_elts (am->msg_index_by_name_and_crc); u32 i = 0; regp = socket_main.current_rp; ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER); regp->name = format (0, "%s%c", mp->name, 0); u32 size = sizeof (*rp) + (nmsg * sizeof (vl_api_message_table_entry_t)); rp = vl_msg_api_alloc_zero (size); rp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE_REPLY); rp->index = htonl (sock_api_registration_handle (regp)); rp->context = mp->context; rp->response = htonl (rv); rp->count = htons (nmsg); /* *INDENT-OFF* */ hash_foreach_pair (hp, am->msg_index_by_name_and_crc, ({ rp->message_table[i].index = htons(hp->value[0]); (void) strncpy_s((char *)rp->message_table[i].name, 64 /* bytes of space at dst */, (char *)hp->key, 64-1 /* chars to copy, without zero byte. */); i++; })); /* *INDENT-ON* */ vl_api_send_msg (regp, (u8 *) rp); } /* * vl_api_sockclnt_delete_t_handler */ void vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp) { vl_api_registration_t *regp; vl_api_sockclnt_delete_reply_t *rp; regp = vl_api_client_index_to_registration (mp->client_index); if (!regp) return; u32 reg_index = socket_api_registration_handle_to_index (ntohl (mp->index)); rp = vl_msg_api_alloc (sizeof (*rp)); rp->_vl_msg_id = htons (VL_API_SOCKCLNT_DELETE_REPLY); rp->context = mp->context; if (!pool_is_free_index (socket_main.registration_pool, reg_index)) { rp->response = htonl (1); vl_api_send_msg (regp, (u8 *) rp); vl_api_registration_del_file (regp); vl_socket_free_registration_index (reg_index); } else { clib_warning ("unknown client ID %d", reg_index); rp->response = htonl (-1); vl_api_send_msg (regp, (u8 *) rp); } } clib_error_t * vl_sock_api_send_fd_msg (int socket_fd, int fds[], int n_fds) { struct msghdr mh = { 0 }; struct iovec iov[1]; char ctl[CMSG_SPACE (sizeof (int) * n_fds)]; struct cmsghdr *cmsg; char *msg = "fdmsg"; int rv; iov[0].iov_base = msg; iov[0].iov_len = strlen (msg); mh.msg_iov = iov; mh.msg_iovlen = 1; clib_memset (&ctl, 0, sizeof (ctl)); mh.msg_control = ctl; mh.msg_controllen = sizeof (ctl); cmsg = CMSG_FIRSTHDR (&mh); cmsg->cmsg_len = CMSG_LEN (sizeof (int) * n_fds); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; clib_memcpy_fast (CMSG_DATA (cmsg), fds, sizeof (int) * n_fds); rv = sendmsg (socket_fd, &mh, 0); if (rv < 0) return clib_error_return_unix (0, "sendmsg"); return 0; } vl_api_shm_elem_config_t * vl_api_make_shm_config (vl_api_sock_init_shm_t * mp) { vl_api_shm_elem_config_t *config = 0, *c; u64 cfg; int i; if (!mp->nitems) { vec_validate (config, 6); config[0].type = VL_API_VLIB_RING; config[0].size = 256; config[0].count = 32; config[1].type = VL_API_VLIB_RING; config[1].size = 1024; config[1].count = 16; config[2].type = VL_API_VLIB_RING; config[2].size = 4096; config[2].count = 2; config[3].type = VL_API_CLIENT_RING; config[3].size = 256; config[3].count = 32; config[4].type = VL_API_CLIENT_RING; config[4].size = 1024; config[4].count = 16; config[5].type = VL_API_CLIENT_RING; config[5].size = 4096; config[5].count = 2; config[6].type = VL_API_QUEUE; config[6].count = 128; config[6].size = sizeof (uword); } else { vec_validate (config, mp->nitems - 1); for (i = 0; i < mp->nitems; i++) { cfg = mp->configs[i]; /* Pretty much a hack but it avoids defining our own api type * in memclnt.api */ c = (vl_api_shm_elem_config_t *) & cfg; config[i].type = c->type; config[i].count = c->count; config[i].size = c->size; } } return config; } /* * Bootstrap shm api using the socket api */ void vl_api_sock_init_shm_t_handler (vl_api_sock_init_shm_t * mp) { vl_api_sock_init_shm_reply_t *rmp; ssvm_private_t _memfd_private, *memfd = &_memfd_private; svm_map_region_args_t _args, *a = &_args; vl_api_registration_t *regp; api_main_t *am = vlibapi_get_main (); svm_region_t *vlib_rp; clib_file_t *cf; vl_api_shm_elem_config_t *config = 0; vl_shmem_hdr_t *shmem_hdr; int rv, tries = 1000; regp = vl_api_client_index_to_registration (mp->client_index); if (regp == 0) { clib_warning ("API client disconnected"); return; } if (regp->registration_type != REGISTRATION_TYPE_SOCKET_SERVER) { rv = -31; /* VNET_API_ERROR_INVALID_REGISTRATION */ goto reply; } /* * Set up a memfd segment of the requested size wherein the * shmem data structures will be initialized */ clib_memset (memfd, 0, sizeof (*memfd)); memfd->ssvm_size = mp->requested_size; memfd->requested_va = 0ULL; memfd->i_am_master = 1; memfd->name = format (0, "%s%c", regp->name, 0); if ((rv = ssvm_master_init_memfd (memfd))) goto reply; /* Remember to close this fd when the socket connection goes away */ vec_add1 (regp->additional_fds_to_close, memfd->fd); /* * Create a plausible svm_region in the memfd backed segment */ clib_memset (a, 0, sizeof (*a)); a->baseva = memfd->sh->ssvm_va + MMAP_PAGESIZE; a->size = memfd->ssvm_size - MMAP_PAGESIZE; /* $$$$ might want a different config parameter */ a->pvt_heap_size = am->api_pvt_heap_size; a->flags = SVM_FLAGS_MHEAP; svm_region_init_mapped_region (a, (svm_region_t *) a->baseva); /* * Part deux, initialize the svm_region_t shared-memory header * api allocation rings, and so on. */ config = vl_api_make_shm_config (mp); vlib_rp = (svm_region_t *) a->baseva; vl_init_shmem (vlib_rp, config, 1 /* is_vlib (dont-care) */ , 1 /* is_private */ ); /* Remember who created this. Needs to be post vl_init_shmem */ shmem_hdr = (vl_shmem_hdr_t *) vlib_rp->user_ctx; shmem_hdr->clib_file_index = vl_api_registration_file_index (regp); vec_add1 (am->vlib_private_rps, vlib_rp); memfd->sh->ready = 1; vec_free (config); /* Recompute the set of input queues to poll in memclnt_process */ vec_reset_length (vl_api_queue_cursizes); reply: rmp = vl_msg_api_alloc (sizeof (*rmp)); rmp->_vl_msg_id = htons (VL_API_SOCK_INIT_SHM_REPLY); rmp->context = mp->context; rmp->retval = htonl (rv); /* * Note: The reply message needs to make it out the back door * before we send the magic fd message. That's taken care of by * the send function. */ vl_socket_api_send (regp, (u8 *) rmp); if (rv != 0) return; /* Send the magic "here's your sign (aka fd)" socket message */ cf = vl_api_registration_file (regp); /* Wait for reply to be consumed before sending the fd */ while (tries-- > 0) { int bytes; rv = ioctl (cf->file_descriptor, TIOCOUTQ, &bytes); if (rv < 0) { clib_unix_warning ("ioctl returned"); break; } if (bytes == 0) break; usleep (1e3); } vl_sock_api_send_fd_msg (cf->file_descriptor, &memfd->fd, 1); } #define foreach_vlib_api_msg \ _(SOCKCLNT_CREATE, sockclnt_create, 1) \ _(SOCKCLNT_DELETE, sockclnt_delete, 1) \ _(SOCK_INIT_SHM, sock_init_shm, 1) clib_error_t * vl_sock_api_init (vlib_main_t * vm) { clib_file_main_t *fm = &file_main; clib_file_t template = { 0 }; vl_api_registration_t *rp; socket_main_t *sm = &socket_main; clib_socket_t *sock = &sm->socksvr_listen_socket; clib_error_t *error; /* If not explicitly configured, do not bind/enable, etc. */ if (sm->socket_name == 0) return 0; #define _(N,n,t) \ vl_msg_api_set_handlers(VL_API_##N, #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), t); foreach_vlib_api_msg; #undef _ vec_resize (sm->input_buffer, 4096); sock->config = (char *) sm->socket_name; sock->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_ALLOW_GROUP_WRITE; error = clib_socket_init (sock); if (error) return error; pool_get (sm->registration_pool, rp); clib_memset (rp, 0, sizeof (*rp)); rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN; template.read_function = socksvr_accept_ready; template.write_function = socksvr_bogus_write; template.file_descriptor = sock->fd; template.private_data = rp - sm->registration_pool; rp->clib_file_index = clib_file_add (fm, &template); return 0; } static clib_error_t * socket_exit (vlib_main_t * vm) { socket_main_t *sm = &socket_main; vl_api_registration_t *rp; /* Defensive driving in case something wipes out early */ if (sm->registration_pool) { u32 index; /* *INDENT-OFF* */ pool_foreach (rp, sm->registration_pool, ({ vl_api_registration_del_file (rp); index = rp->vl_api_registration_pool_index; vl_socket_free_registration_index (index); })); /* *INDENT-ON* */ } return 0; } VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit); static clib_error_t * socksvr_config (vlib_main_t * vm, unformat_input_t * input) { socket_main_t *sm = &socket_main; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "socket-name %s", &sm->socket_name)) ; /* DEPRECATE: default keyword is ignored */ else if (unformat (input, "default")) ; else { return clib_error_return (0, "unknown input '%U'", format_unformat_error, input); } } if (!vec_len (sm->socket_name)) sm->socket_name = format (0, "%s/%s", vlib_unix_get_runtime_dir (), API_SOCKET_FILENAME); vec_terminate_c_string (sm->socket_name); return 0; } VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr"); void vlibsocket_reference () { } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */