summaryrefslogtreecommitdiffstats
path: root/src/plugins/crypto_ipsecmb/ipsecmb.c
blob: d579c0d74a65f6e9547ba812cccd6ab022cd07b6 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * ipsecmb.c - Intel IPSec Multi-buffer library Crypto Engine
 *
 * Copyright (c) 2019 Cisco Systemss
 * 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 <fcntl.h>

#include <intel-ipsec-mb.h>

#include <vnet/vnet.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>
#include <vnet/crypto/crypto.h>
#include <vppinfra/cpu.h>

typedef struct
{
  MB_MGR *mgr;
  __m128i cbc_iv;
} ipsecmb_per_thread_data_t;

typedef struct ipsecmb_main_t_
{
  ipsecmb_per_thread_data_t *per_thread_data;
} ipsecmb_main_t;

static ipsecmb_main_t ipsecmb_main;

#define foreach_ipsecmb_hmac_op                                \
  _(SHA1, SHA1, sha1)                                          \
  _(SHA256, SHA_256, sha256)                                   \
  _(SHA384, SHA_384, sha384)                                   \
  _(SHA512, SHA_512, sha512)

#define foreach_ipsecmb_cipher_op                              \
  _(AES_128_CBC, 128, 16, 16)                                  \
  _(AES_192_CBC, 192, 24, 16)                                  \
  _(AES_256_CBC, 256, 32, 16)

always_inline void
hash_expand_keys (const MB_MGR * mgr,
		  const u8 * key,
		  u32 length,
		  u8 block_size,
		  u8 ipad[256], u8 opad[256], hash_one_block_t fn)
{
  u8 buf[block_size];
  int i = 0;

  if (length > block_size)
    {
      return;
    }

  memset (buf, 0x36, sizeof (buf));
  for (i = 0; i < length; i++)
    {
      buf[i] ^= key[i];
    }
  fn (buf, ipad);

  memset (buf, 0x5c, sizeof (buf));

  for (i = 0; i < length; i++)
    {
      buf[i] ^= key[i];
    }
  fn (buf, opad);
}

always_inline void
ipsecmb_retire_hmac_job (JOB_AES_HMAC * job, u32 * n_fail)
{
  vnet_crypto_op_t *op = job->user_data;

  if (STS_COMPLETED != job->status)
    {
      op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
      *n_fail = *n_fail + 1;
    }
  else
    op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;

  if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
    {
      if ((memcmp (op->digest, job->auth_tag_output, op->digest_len)))
	{
	  *n_fail = *n_fail + 1;
	  op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
	}
    }
  else
    clib_memcpy_fast (op->digest, job->auth_tag_output, op->digest_len);
}

static_always_inline u32
ipsecmb_ops_hmac_inline (vlib_main_t * vm,
			 const ipsecmb_per_thread_data_t * ptd,
			 vnet_crypto_op_t * ops[],
			 u32 n_ops,
			 u32 block_size,
			 hash_one_block_t fn, JOB_HASH_ALG alg)
{
  JOB_AES_HMAC *job;
  u32 i, n_fail = 0;
  u8 scratch[n_ops][64];

  /*
   * queue all the jobs first ...
   */
  for (i = 0; i < n_ops; i++)
    {
      vnet_crypto_op_t *op = ops[i];
      u8 ipad[256], opad[256];

      hash_expand_keys (ptd->mgr, op->key, op->key_len,
			block_size, ipad, opad, fn);

      job = IMB_GET_NEXT_JOB (ptd->mgr);

      job->src = op->src;
      job->hash_start_src_offset_in_bytes = 0;
      job->msg_len_to_hash_in_bytes = op->len;
      job->hash_alg = alg;
      job->auth_tag_output_len_in_bytes = op->digest_len;
      job->auth_tag_output = scratch[i];

      job->cipher_mode = NULL_CIPHER;
      job->cipher_direction = DECRYPT;
      job->chain_order = HASH_CIPHER;

      job->aes_key_len_in_bytes = op->key_len;

      job->u.HMAC._hashed_auth_key_xor_ipad = ipad;
      job->u.HMAC._hashed_auth_key_xor_opad = opad;
      job->user_data = op;

      job = IMB_SUBMIT_JOB (ptd->mgr);

      if (job)
	ipsecmb_retire_hmac_job (job, &n_fail);
    }

  /*
   * .. then flush (i.e. complete) them
   *  We will have queued enough to satisfy the 'multi' buffer
   */
  while ((job = IMB_FLUSH_JOB (ptd->mgr)))
    {
      ipsecmb_retire_hmac_job (job, &n_fail);
    }

  return n_ops - n_fail;
}

#define _(a, b, c)                                                      \
static_always_inline u32                                                \
ipsecmb_ops_hmac_##a (vlib_main_t * vm,                                 \
                      vnet_crypto_op_t * ops[],                         \
                      u32 n_ops)                                        \
{                                                                       \
  ipsecmb_per_thread_data_t *ptd;                                       \
  ipsecmb_main_t *imbm;                                                 \
                                                                        \
  imbm = &ipsecmb_main;                                                 \
  ptd = vec_elt_at_index (imbm->per_thread_data, vm->thread_index);     \
                                                                        \
  return ipsecmb_ops_hmac_inline (vm, ptd, ops, n_ops,                  \
                                  b##_BLOCK_SIZE,                       \
                                  ptd->mgr->c##_one_block,              \
                                  b);                                   \
  }
foreach_ipsecmb_hmac_op;
#undef _

#define EXPANDED_KEY_N_BYTES (16 * 15)

always_inline void
ipsecmb_retire_cipher_job (JOB_AES_HMAC * job, u32 * n_fail)
{
  vnet_crypto_op_t *op = job->user_data;

  if (STS_COMPLETED != job->status)
    {
      op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
      *n_fail = *n_fail + 1;
    }
  else
    op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
}

static_always_inline u32
ipsecmb_ops_cipher_inline (vlib_main_t * vm,
			   ipsecmb_per_thread_data_t * ptd,
			   vnet_crypto_op_t * ops[],
			   u32 n_ops, u32 key_len, u32 iv_len,
			   keyexp_t fn, JOB_CIPHER_DIRECTION direction)
{
  JOB_AES_HMAC *job;
  u32 i, n_fail = 0;

  /*
   * queue all the jobs first ...
   */
  for (i = 0; i < n_ops; i++)
    {
      u8 aes_enc_key_expanded[EXPANDED_KEY_N_BYTES];
      u8 aes_dec_key_expanded[EXPANDED_KEY_N_BYTES];
      vnet_crypto_op_t *op = ops[i];
      __m128i iv;

      fn (op->key, aes_enc_key_expanded, aes_dec_key_expanded);

      job = IMB_GET_NEXT_JOB (ptd->mgr);

      job->src = op->src;
      job->dst = op->dst;
      job->msg_len_to_cipher_in_bytes = op->len;
      job->cipher_start_src_offset_in_bytes = 0;

      job->hash_alg = NULL_HASH;
      job->cipher_mode = CBC;
      job->cipher_direction = direction;
      job->chain_order = (direction == ENCRYPT ? CIPHER_HASH : HASH_CIPHER);

      if ((direction == ENCRYPT) && (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV))
	{
	  iv = ptd->cbc_iv;
	  _mm_storeu_si128 ((__m128i *) op->iv, iv);
	  ptd->cbc_iv = _mm_aesenc_si128 (iv, iv);
	}

      job->aes_key_len_in_bytes = key_len;
      job->aes_enc_key_expanded = aes_enc_key_expanded;
      job->aes_dec_key_expanded = aes_dec_key_expanded;
      job->iv = op->iv;
      job->iv_len_in_bytes = iv_len;

      job->user_data = op;

      job = IMB_SUBMIT_JOB (ptd->mgr);

      if (job)
	ipsecmb_retire_cipher_job (job, &n_fail);
    }

  /*
   * .. then flush (i.e. complete) them
   *  We will have queued enough to satisfy the 'multi' buffer
   */
  while ((job = IMB_FLUSH_JOB (ptd->mgr)))
    {
      ipsecmb_retire_cipher_job (job, &n_fail);
    }

  return n_ops - n_fail;
}

#define _(a, b, c, d)                                                   \
static_always_inline u32                                                \
ipsecmb_ops_cipher_enc_##a (vlib_main_t * vm,                           \
                            vnet_crypto_op_t * ops[],                   \
                            u32 n_ops)                                  \
{                                                                       \
  ipsecmb_per_thread_data_t *ptd;                                       \
  ipsecmb_main_t *imbm;                                                 \
                                                                        \
  imbm = &ipsecmb_main;                                                 \
  ptd = vec_elt_at_index (imbm->per_thread_data, vm->thread_index);     \
                                                                        \
  return ipsecmb_ops_cipher_inline (vm, ptd, ops, n_ops, c, d,          \
                                    ptd->mgr->keyexp_##b,               \
                                    ENCRYPT);                           \
  }
foreach_ipsecmb_cipher_op;
#undef _

#define _(a, b, c, d)                                                   \
static_always_inline u32                                                \
ipsecmb_ops_cipher_dec_##a (vlib_main_t * vm,                           \
                            vnet_crypto_op_t * ops[],                   \
                            u32 n_ops)                                  \
{                                                                       \
  ipsecmb_per_thread_data_t *ptd;                                       \
  ipsecmb_main_t *imbm;                                                 \
                                                                        \
  imbm = &ipsecmb_main;                                                 \
  ptd = vec_elt_at_index (imbm->per_thread_data, vm->thread_index);     \
                                                                        \
  return ipsecmb_ops_cipher_inline (vm, ptd, ops, n_ops, c, d,          \
                                    ptd->mgr->keyexp_##b,               \
                                    DECRYPT);                           \
  }
foreach_ipsecmb_cipher_op;
#undef _

clib_error_t *
crypto_ipsecmb_iv_init (ipsecmb_main_t * imbm)
{
  ipsecmb_per_thread_data_t *ptd;
  clib_error_t *err = 0;
  int fd;

  if ((fd = open ("/dev/urandom", O_RDONLY)) < 0)
    return clib_error_return_unix (0, "failed to open '/dev/urandom'");

  vec_foreach (ptd, imbm->per_thread_data)
  {
    if (read (fd, &ptd->cbc_iv, sizeof (ptd->cbc_iv)) != sizeof (ptd->cbc_iv))
      {
	err = clib_error_return_unix (0, "'/dev/urandom' read failure");
	close (fd);
	return (err);
      }
  }

  close (fd);
  return (NULL);
}

static clib_error_t *
crypto_ipsecmb_init (vlib_main_t * vm)
{
  ipsecmb_main_t *imbm = &ipsecmb_main;
  ipsecmb_per_thread_data_t *ptd;
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  clib_error_t *error;
  u32 eidx;

  if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
    return error;

  /*
   * A priority that is better than OpenSSL but worse than VPP natvie
   */
  eidx = vnet_crypto_register_engine (vm, "ipsecmb", 80,
				      "Intel IPSEC multi-buffer");

  vec_validate (imbm->per_thread_data, tm->n_vlib_mains - 1);

  if (clib_cpu_supports_avx512f ())
    {
      vec_foreach (ptd, imbm->per_thread_data)
      {
	ptd->mgr = alloc_mb_mgr (0);
	init_mb_mgr_avx512 (ptd->mgr);
      }
    }
  else if (clib_cpu_supports_avx2 ())
    {
      vec_foreach (ptd, imbm->per_thread_data)
      {
	ptd->mgr = alloc_mb_mgr (0);
	init_mb_mgr_avx2 (ptd->mgr);
      }
    }
  else
    {
      vec_foreach (ptd, imbm->per_thread_data)
      {
	ptd->mgr = alloc_mb_mgr (0);
	init_mb_mgr_sse (ptd->mgr);
      }
    }

  if (clib_cpu_supports_x86_aes () && (error = crypto_ipsecmb_iv_init (imbm)))
    return (error);


#define _(a, b, c)                                                       \
  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
                                    ipsecmb_ops_hmac_##a);               \

  foreach_ipsecmb_hmac_op;
#undef _
#define _(a, b, c, d)                                                   \
  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
                                    ipsecmb_ops_cipher_enc_##a);        \

  foreach_ipsecmb_cipher_op;
#undef _
#define _(a, b, c, d)                                                   \
  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
                                    ipsecmb_ops_cipher_dec_##a);        \

  foreach_ipsecmb_cipher_op;
#undef _

  return (NULL);
}

VLIB_INIT_FUNCTION (crypto_ipsecmb_init);

/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () =
{
  .version = VPP_BUILD_VER,
  .description = "Intel IPSEC multi-buffer",
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
an class="n">u32 table_index, server_index, li; stream_session_t *listener; application_t *client, *server; local_session_t *ll; u8 fib_proto; u64 lh; if (session_endpoint_is_zero (&a->sep)) return VNET_API_ERROR_INVALID_VALUE; client = application_get (a->app_index); session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ ); client_wrk = application_get_worker (client, a->wrk_map_index); /* * 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, &a->sep); if (lh == SESSION_DROP_HANDLE) return VNET_API_ERROR_APP_CONNECT_FILTERED; if (lh == SESSION_INVALID_HANDLE) goto global_scope; local_session_parse_handle (lh, &server_index, &li); /* * Break loop if rule in local table points to connecting app. This * can happen if client is a generic proxy. Route connect through * global table instead. */ if (server_index != a->app_index) { server = application_get (server_index); ll = application_get_local_listen_session (server, li); listener = (stream_session_t *) ll; server_wrk = application_listener_select_worker (listener, 1 /* is_local */ ); return application_local_session_connect (client_wrk, server_wrk, ll, a->api_context); } } /* * If nothing found, check the global scope for locally attached * destinations. Make sure first that we're allowed to. */ global_scope: if (session_endpoint_is_local (&a->sep)) return VNET_API_ERROR_SESSION_CONNECT; if (!application_has_global_scope (client)) return VNET_API_ERROR_APP_CONNECT_SCOPE; fib_proto = session_endpoint_fib_proto (&a->sep); table_index = application_session_table (client, fib_proto); listener = session_lookup_listener (table_index, &a->sep); if (listener) { server_wrk = application_listener_select_worker (listener, 0 /* is_local */ ); ll = (local_session_t *) listener; return application_local_session_connect (client_wrk, server_wrk, ll, a->api_context); } /* * Not connecting to a local server, propagate to transport */ if (app_worker_open_session (client_wrk, &a->sep, a->api_context)) return VNET_API_ERROR_SESSION_CONNECT; return 0; } /** * unformat a vnet URI * * transport-proto://[hostname]ip46-addr:port * eg. tcp://ip46-addr:port * tls://[testtsl.fd.io]ip46-addr:port * * u8 ip46_address[16]; * u16 port_in_host_byte_order; * stream_session_type_t sst; * u8 *fifo_name; * * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address, * &sst, &port, &fifo_name)) * etc... * */ uword unformat_vnet_uri (unformat_input_t * input, va_list * args) { session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *); u32 transport_proto = 0, port; if (unformat (input, "%U://%U/%d", unformat_transport_proto, &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port)) { sep->transport_proto = transport_proto; sep->port = clib_host_to_net_u16 (port); sep->is_ip4 = 1; return 1; } else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, &transport_proto, &sep->hostname, unformat_ip4_address, &sep->ip.ip4, &port)) { sep->transport_proto = transport_proto; sep->port = clib_host_to_net_u16 (port); sep->is_ip4 = 1; return 1; } else if (unformat (input, "%U://%U/%d", unformat_transport_proto, &transport_proto, unformat_ip6_address, &sep->ip.ip6, &port)) { sep->transport_proto = transport_proto; sep->port = clib_host_to_net_u16 (port); sep->is_ip4 = 0; return 1; } else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, &transport_proto, &sep->hostname, unformat_ip6_address, &sep->ip.ip6, &port)) { sep->transport_proto = transport_proto; sep->port = clib_host_to_net_u16 (port); sep->is_ip4 = 0; return 1; } return 0; } static u8 *cache_uri; static session_endpoint_cfg_t *cache_sep; int parse_uri (char *uri, session_endpoint_cfg_t * sep) { unformat_input_t _input, *input = &_input; if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri))) { *sep = *cache_sep; return 0; } /* Make sure */ uri = (char *) format (0, "%s%c", uri, 0); /* Parse uri */ unformat_init_string (input, uri, strlen (uri)); if (!unformat (input, "%U", unformat_vnet_uri, sep)) { unformat_free (input); return VNET_API_ERROR_INVALID_VALUE; } unformat_free (input); vec_free (cache_uri); cache_uri = (u8 *) uri; if (cache_sep) clib_mem_free (cache_sep); cache_sep = clib_mem_alloc (sizeof (*sep)); *cache_sep = *sep; return 0; } static int app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index) { app_namespace_t *app_ns; if (vec_len (namespace_id) == 0) { /* Use default namespace */ *app_ns_index = 0; return 0; } *app_ns_index = app_namespace_index_from_id (namespace_id); if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX) return VNET_API_ERROR_APP_INVALID_NS; app_ns = app_namespace_get (*app_ns_index); if (!app_ns) return VNET_API_ERROR_APP_INVALID_NS; if (app_ns->ns_secret != secret) return VNET_API_ERROR_APP_WRONG_NS_SECRET; return 0; } static u8 * app_name_from_api_index (u32 api_client_index) { vl_api_registration_t *regp; regp = vl_api_client_index_to_registration (api_client_index); if (regp) return format (0, "%s%c", regp->name, 0); clib_warning ("api client index %u does not have an api registration!", api_client_index); return format (0, "unknown%c", 0); } /** * Attach application to vpp * * Allocates a vpp app, i.e., a structure that keeps back pointers * to external app and a segment manager for shared memory fifo based * communication with the external app. */ clib_error_t * vnet_application_attach (vnet_app_attach_args_t * a) { svm_fifo_segment_private_t *fs; application_t *app = 0; app_worker_t *app_wrk; segment_manager_t *sm; u32 app_ns_index = 0; u8 *app_name = 0; u64 secret; int rv; if (a->api_client_index != APP_INVALID_INDEX) app = application_lookup (a->api_client_index); else if (a->name) app = application_lookup_name (a->name); else return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, "api index or name must be provided"); if (app) return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, 0, "app already attached"); if (a->api_client_index != APP_INVALID_INDEX) { app_name = app_name_from_api_index (a->api_client_index); a->name = app_name; } secret = a->options[APP_OPTIONS_NAMESPACE_SECRET]; if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index))) return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv); a->options[APP_OPTIONS_NAMESPACE] = app_ns_index; if ((rv = application_alloc_and_init ((app_init_args_t *) a))) return clib_error_return_code (0, rv, 0, "app init: %d", rv); app = application_get (a->app_index); if ((rv = app_worker_alloc_and_init (app, &app_wrk))) return clib_error_return_code (0, rv, 0, "app default wrk init: %d", rv); a->app_evt_q = app_wrk->event_queue; app_wrk->api_client_index = a->api_client_index; sm = segment_manager_get (app_wrk->first_segment_manager); fs = segment_manager_get_segment_w_lock (sm, 0); if (application_is_proxy (app)) application_setup_proxy (app); ASSERT (vec_len (fs->ssvm.name) <= 128); a->segment = &fs->ssvm; a->segment_handle = segment_manager_segment_handle (sm, fs); segment_manager_segment_reader_unlock (sm); vec_free (app_name); return 0; } /** * Detach application from vpp */ int vnet_application_detach (vnet_app_detach_args_t * a) { application_t *app; app = application_get_if_valid (a->app_index); if (!app) { clib_warning ("app not attached"); return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } app_interface_check_thread_and_barrier (vnet_application_detach, a); application_detach_process (app, a->api_client_index); return 0; } int vnet_bind_uri (vnet_bind_args_t * a) { session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; int rv; rv = parse_uri (a->uri, &sep); if (rv) return rv; sep.app_wrk_index = 0; clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); return vnet_bind_inline (a); } int vnet_unbind_uri (vnet_unbind_args_t * a) { session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; stream_session_t *listener; u32 table_index; int rv; rv = parse_uri (a->uri, &sep); if (rv) return rv; /* NOTE: only default fib tables supported for uri apis */ table_index = session_lookup_get_index_for_fib (fib_ip_proto (!sep.is_ip4), 0); listener = session_lookup_listener (table_index, (session_endpoint_t *) & sep); if (!listener) return VNET_API_ERROR_ADDRESS_NOT_IN_USE; a->handle = listen_session_get_handle (listener); return vnet_unbind_inline (a); } clib_error_t * vnet_connect_uri (vnet_connect_args_t * a) { session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; int rv; /* Parse uri */ rv = parse_uri (a->uri, &sep); if (rv) return clib_error_return_code (0, rv, 0, "app init: %d", rv); clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); if ((rv = application_connect (a))) return clib_error_return_code (0, rv, 0, "connect failed"); return 0; } int vnet_disconnect_session (vnet_disconnect_args_t * a) { if (session_handle_is_local (a->handle)) { local_session_t *ls; /* Disconnect reply came to worker 1 not main thread */ app_interface_check_thread_and_barrier (vnet_disconnect_session, a); if (!(ls = application_get_local_session_from_handle (a->handle))) return 0; return application_local_session_disconnect (a->app_index, ls); } else { app_worker_t *app_wrk; stream_session_t *s; s = session_get_from_handle_if_valid (a->handle); if (!s) return VNET_API_ERROR_INVALID_VALUE; app_wrk = app_worker_get (s->app_wrk_index); if (app_wrk->app_index != a->app_index) return VNET_API_ERROR_INVALID_VALUE; /* We're peeking into another's thread pool. Make sure */ ASSERT (s->session_index == session_index_from_handle (a->handle)); stream_session_disconnect (s); } return 0; } clib_error_t * vnet_bind (vnet_bind_args_t * a) { int rv; if ((rv = vnet_bind_inline (a))) return clib_error_return_code (0, rv, 0, "bind failed: %d", rv); return 0; } clib_error_t * vnet_unbind (vnet_unbind_args_t * a) { int rv; if ((rv = vnet_unbind_inline (a))) return clib_error_return_code (0, rv, 0, "unbind failed: %d", rv); return 0; } clib_error_t * vnet_connect (vnet_connect_args_t * a) { int rv; if ((rv = application_connect (a))) return clib_error_return_code (0, rv, 0, "connect failed: %d", rv); return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */