summaryrefslogtreecommitdiffstats
path: root/src/vnet/session/application_interface.c
blob: 2283b0f6f94e80e4cbcccd25ac71e27790e681ca (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
/*
 * Copyright (c) 2016-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/session/application_interface.h>
#include <vnet/session/application.h>
#include <vnet/session/session.h>

/** @file
    VPP's application/session API bind/unbind/connect/disconnect calls
*/

/**
 * 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;
    }
  else if (unformat (input, "%U://session/%lu", unformat_transport_proto,
		     &transport_proto, &sep->transport_opts))
    {
      sep->transport_proto = transport_proto;
      sep->is_ip4 = 1;
      sep->ip.ip4.as_u32 = 1;	/* ip need to be non zero in vnet */
      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;
}

int
vnet_bind_uri (vnet_listen_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_listen (a);
}

int
vnet_unbind_uri (vnet_unlisten_args_t * a)
{
  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
  session_t *listener;
  u32 table_index;
  int rv;

  if ((rv = parse_uri (a->uri, &sep)))
    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_unlisten (a);
}

int
vnet_connect_uri (vnet_connect_args_t * a)
{
  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
  int rv;

  if ((rv = parse_uri (a->uri, &sep)))
    return rv;

  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
  if ((rv = vnet_connect (a)))
    return rv;
  return 0;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
out game. */ ep->expiration_time = now + 600.0; if (0) clib_warning ("resolving '%s', was %s valid", ep->name, (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ? "already" : "not"); /* * The world is a mess. A single DNS request sent to e.g. 8.8.8.8 * may yield multiple, subtly different responses - all with the same * DNS protocol-level ID. * * Last response wins in terms of what ends up in the cache. * First response wins in terms of the response sent to the client. */ /* Strong hint that we may not find a pending resolution entry */ entry_was_valid = (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ? 1 : 0; if (vec_len (ep->dns_response)) ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID; /* Most likely, send 1 message */ for (i = 0; i < vec_len (ep->pending_requests); i++) { vl_api_registration_t *regp; pr = vec_elt_at_index (ep->pending_requests, i); switch (pr->request_type) { case DNS_API_PENDING_NAME_TO_IP: { vl_api_dns_resolve_name_reply_t *rmp; regp = vl_api_client_index_to_registration (pr->client_index); if (regp == 0) continue; rmp = vl_msg_api_alloc (sizeof (*rmp)); rmp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY + dm->msg_id_base); rmp->context = pr->client_context; min_ttl = ~0; rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl); if (min_ttl != ~0) ep->expiration_time = now + min_ttl; rmp->retval = clib_host_to_net_u32 (rv); vl_api_send_msg (regp, (u8 *) rmp); } break; case DNS_API_PENDING_IP_TO_NAME: { vl_api_dns_resolve_ip_reply_t *rmp; regp = vl_api_client_index_to_registration (pr->client_index); if (regp == 0) continue; rmp = vl_msg_api_alloc (sizeof (*rmp)); rmp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY + dm->msg_id_base); rmp->context = pr->client_context; min_ttl = ~0; rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl); if (min_ttl != ~0) ep->expiration_time = now + min_ttl; rmp->retval = clib_host_to_net_u32 (rv); vl_api_send_msg (regp, (u8 *) rmp); } break; case DNS_PEER_PENDING_IP_TO_NAME: case DNS_PEER_PENDING_NAME_TO_IP: if (pr->is_ip6) vnet_send_dns6_reply (vm, dm, pr, ep, 0 /* allocate a buffer */ ); else vnet_send_dns4_reply (vm, dm, pr, ep, 0 /* allocate a buffer */ ); break; default: clib_warning ("request type %d unknown", pr->request_type); break; } } vec_free (ep->pending_requests); remove_count = 0; for (i = 0; i < vec_len (dm->unresolved_entries); i++) { if (dm->unresolved_entries[i] == pool_index) { vec_delete (dm->unresolved_entries, 1, i); remove_count++; i--; } } /* See multiple response comment above... */ if (remove_count == 0) { u32 error_code = entry_was_valid ? DNS46_REPLY_ERROR_MULTIPLE_REPLY : DNS46_REPLY_ERROR_NO_UNRESOLVED_ENTRY; vlib_node_increment_counter (vm, dns46_reply_node.index, error_code, 1); dns_cache_unlock (dm); return; } /* Deal with bogus names, server issues, etc. */ switch (rcode) { default: case DNS_RCODE_NO_ERROR: break; case DNS_RCODE_SERVER_FAILURE: case DNS_RCODE_NOT_IMPLEMENTED: case DNS_RCODE_REFUSED: if (ep->server_af == 0) clib_warning ("name server %U can't resolve '%s'", format_ip4_address, dm->ip4_name_servers + ep->server_rotor, ep->name); else clib_warning ("name server %U can't resolve '%s'", format_ip6_address, dm->ip6_name_servers + ep->server_rotor, ep->name); /* FALLTHROUGH */ case DNS_RCODE_NAME_ERROR: case DNS_RCODE_FORMAT_ERROR: /* remove trash from the cache... */ vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries); break; } dns_cache_unlock (dm); return; } static void retry_scan (vlib_main_t * vm, dns_main_t * dm, f64 now) { int i; dns_cache_entry_t *ep; for (i = 0; i < vec_len (dm->unresolved_entries); i++) { dns_cache_lock (dm, 11); ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]); ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0); vnet_send_dns_request (vm, dm, ep); dns_cache_unlock (dm); } } static uword dns_resolver_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) { dns_main_t *dm = &dns_main; f64 now; f64 timeout = 1000.0; uword *event_data = 0; uword event_type; int i; while (1) { vlib_process_wait_for_event_or_clock (vm, timeout); now = vlib_time_now (vm); event_type = vlib_process_get_events (vm, (uword **) & event_data); switch (event_type) { /* Send one of these when a resolution is pending */ case DNS_RESOLVER_EVENT_PENDING: timeout = 2.0; break; case DNS_RESOLVER_EVENT_RESOLVED: for (i = 0; i < vec_len (event_data); i++) resolve_event (vm, dm, now, (u8 *) event_data[i]); break; case ~0: /* timeout */ retry_scan (vm, dm, now); break; } vec_reset_length (event_data); /* No work? Back to slow timeout mode... */ if (vec_len (dm->unresolved_entries) == 0) timeout = 1000.0; } return 0; /* or not */ } void vnet_dns_create_resolver_process (vlib_main_t * vm, dns_main_t * dm) { /* Already created the resolver process? */ if (dm->resolver_process_node_index > 0) return; /* No, create it now and make a note of the node index */ dm->resolver_process_node_index = vlib_process_create (vm, "dns-resolver-process", dns_resolver_process, 16 /* log2_n_stack_bytes */ ); } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */