aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/srp/pg.c
blob: 54f1a3bba18a8c21a16e0533f31370997a289210 (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
/*
 * Copyright (c) 2015 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.
 */
/*
 * srp/pg.c: packet generator srp interface
 *
 * Copyright (c) 2008 Eliot Dresselhaus
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <vlib/vlib.h>
#include <vnet/pg/pg.h>
#include <vnet/srp/srp.h>
#include <vnet/ethernet/ethernet.h>

typedef struct {
  pg_edit_t ttl;
  pg_edit_t is_inner_ring;
  pg_edit_t mode;
  pg_edit_t priority;
  pg_edit_t parity;
  pg_edit_t type;
  pg_edit_t src_address;
  pg_edit_t dst_address;
} pg_srp_header_t;

static inline void
pg_srp_header_init (pg_srp_header_t * e)
{
  pg_edit_init (&e->ttl, srp_and_ethernet_header_t, srp.ttl);
  pg_edit_init_bitfield (&e->is_inner_ring, srp_and_ethernet_header_t,
			 srp.as_u16,
			 7, 1);
  pg_edit_init_bitfield (&e->mode, srp_and_ethernet_header_t,
			 srp.as_u16,
			 4, 3);
  pg_edit_init_bitfield (&e->priority, srp_and_ethernet_header_t,
			 srp.as_u16,
			 1, 3);
  pg_edit_init_bitfield (&e->parity, srp_and_ethernet_header_t,
			 srp.as_u16,
			 0, 1);
  pg_edit_init (&e->type, srp_and_ethernet_header_t, ethernet.type);
  pg_edit_init (&e->src_address, srp_and_ethernet_header_t, ethernet.src_address);
  pg_edit_init (&e->dst_address, srp_and_ethernet_header_t, ethernet.dst_address);
}

uword
unformat_pg_srp_header (unformat_input_t * input, va_list * args)
{
  pg_stream_t * s = va_arg (*args, pg_stream_t *);
  pg_srp_header_t * e;
  u32 error, group_index;
  
  e = pg_create_edit_group (s, sizeof (e[0]), sizeof (srp_header_t),
			    &group_index);
  pg_srp_header_init (e);

  error = 1;
  if (! unformat (input, "%U: %U -> %U",
		  unformat_pg_edit,
		    unformat_ethernet_type_net_byte_order, &e->type,
		  unformat_pg_edit,
		    unformat_ethernet_address, &e->src_address,
		  unformat_pg_edit,
		    unformat_ethernet_address, &e->dst_address))
    goto done;

  {
    srp_header_t h;

    h.as_u16 = 0;
    h.mode = SRP_MODE_data;
    h.ttl = 255;
    h.parity = count_set_bits (h.as_u16) ^ 1;
  
    pg_edit_set_fixed (&e->mode, h.mode);
    pg_edit_set_fixed (&e->ttl, h.ttl);
    pg_edit_set_fixed (&e->is_inner_ring, h.is_inner_ring);
    pg_edit_set_fixed (&e->priority, h.priority);
    pg_edit_set_fixed (&e->parity, h.parity);
  }

  error = 0;
  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "mode %U",
		    unformat_pg_edit,
		    unformat_pg_number, &e->mode))
	;
      else if (unformat (input, "ttl %U",
			 unformat_pg_edit,
			 unformat_pg_number, &e->ttl))
	;
      else if (unformat (input, "priority %U",
			 unformat_pg_edit,
			 unformat_pg_number, &e->priority))
	;
      else
	break;
    }
      
  {
    ethernet_main_t * em = &ethernet_main;
    ethernet_type_info_t * ti = 0;
    pg_node_t * pg_node = 0;

    if (e->type.type == PG_EDIT_FIXED)
      {
	u16 t = *(u16 *) e->type.values[PG_EDIT_LO];
	ti = ethernet_get_type_info (em, clib_net_to_host_u16 (t));
	if (ti && ti->node_index != ~0)
	  pg_node = pg_get_node (ti->node_index);
      }

    if (pg_node && pg_node->unformat_edit
	&& unformat_user (input, pg_node->unformat_edit, s))
      ;
    else if (! unformat_user (input, unformat_pg_payload, s))
      goto done;
  }

 done:
  if (error)
    pg_free_edit_group (s);
  return error == 0;
}
e allocate all applications */ application_t *app_pool; /** * Pool of workers associated to apps */ app_worker_t *workers; /** * Hash table of apps by api client index */ uword *app_by_api_client_index; /** * Hash table of builtin apps by name */ uword *app_by_name; } app_main_t; #define foreach_app_init_args \ _(u32, api_client_index) \ _(u8 *, name) \ _(u64 *, options) \ _(u8 *, namespace_id) \ _(session_cb_vft_t *, session_cb_vft) \ _(u32, app_index) \ typedef struct app_init_args_ { #define _(_type, _name) _type _name; foreach_app_init_args #undef _ } app_init_args_t; typedef struct _vnet_app_worker_add_del_args { u32 app_index; /**< App for which a new worker is requested */ u32 wrk_index; /**< Index to delete or return value if add */ ssvm_private_t *segment; /**< First segment in segment manager */ svm_msg_q_t *evt_q; /**< Worker message queue */ u8 is_add; /**< Flag set if addition */ } vnet_app_worker_add_del_args_t; #define APP_INVALID_INDEX ((u32)~0) #define APP_NS_INVALID_INDEX ((u32)~0) #define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0) app_worker_t *app_worker_alloc (application_t * app); int app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk); app_worker_t *app_worker_get (u32 wrk_index); app_worker_t *app_worker_get_if_valid (u32 wrk_index); application_t *app_worker_get_app (u32 wrk_index); void app_worker_free (app_worker_t * app_wrk); int app_worker_open_session (app_worker_t * app, session_endpoint_t * tep, u32 api_context); segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *, stream_session_t *); segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *); int app_worker_alloc_connects_segment_manager (app_worker_t * app); int app_worker_add_segment_notify (u32 app_or_wrk, ssvm_private_t * fs); u32 app_worker_n_listeners (app_worker_t * app); stream_session_t *app_worker_first_listener (app_worker_t * app, u8 fib_proto, u8 transport_proto); u8 app_worker_application_is_builtin (app_worker_t * app_wrk); int app_worker_send_event (app_worker_t * app, stream_session_t * s, u8 evt); int app_worker_lock_and_send_event (app_worker_t * app, stream_session_t * s, u8 evt_type); clib_error_t *vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a); int application_start_listen (application_t * app, session_endpoint_cfg_t * tep, session_handle_t * handle); int application_stop_listen (u32 app_index, u32 app_or_wrk, session_handle_t handle); application_t *application_alloc (void); int application_alloc_and_init (app_init_args_t * args); void application_free (application_t * app); application_t *application_get (u32 index); application_t *application_get_if_valid (u32 index); application_t *application_lookup (u32 api_client_index); application_t *application_lookup_name (const u8 * name); u32 application_index (application_t * app); app_worker_t *application_get_worker (application_t * app, u32 wrk_index); app_worker_t *application_get_default_worker (application_t * app); app_worker_t *application_listener_select_worker (stream_session_t * ls, u8 is_local); int application_api_queue_is_full (application_t * app); int application_is_proxy (application_t * app); int application_is_builtin (application_t * app); int application_is_builtin_proxy (application_t * app); u32 application_session_table (application_t * app, u8 fib_proto); u32 application_local_session_table (application_t * app); u8 *application_name_from_index (u32 app_or_wrk); u8 application_has_local_scope (application_t * app); u8 application_has_global_scope (application_t * app); u8 application_use_mq_for_ctrl (application_t * app); void application_setup_proxy (application_t * app); void application_remove_proxy (application_t * app); segment_manager_properties_t *application_get_segment_manager_properties (u32 app_index); segment_manager_properties_t * application_segment_manager_properties (application_t * app); /* * Local session */ local_session_t *application_local_session_alloc (app_worker_t * app); void application_local_session_free (app_worker_t * app, local_session_t * ls); local_session_t *application_get_local_session (app_worker_t * app, u32 session_index); local_session_t *application_get_local_session_from_handle (session_handle_t handle); local_session_t * application_get_local_listen_session_from_handle (session_handle_t lh); int application_start_local_listen (application_t * server, session_endpoint_cfg_t * sep, session_handle_t * handle); int application_stop_local_listen (u32 app_index, u32 app_or_wrk, session_handle_t lh); int application_local_session_connect (app_worker_t * client, app_worker_t * server, local_session_t * ls, u32 opaque); int application_local_session_connect_notify (local_session_t * ls); int application_local_session_disconnect (u32 app_or_wrk, local_session_t * ls); int application_local_session_disconnect_w_index (u32 app_or_wrk, u32 ls_index); void app_worker_local_sessions_free (app_worker_t * app); always_inline u32 local_session_id (local_session_t * ls) { ASSERT (ls->session_index < (2 << 16)); u32 app_or_wrk_index; if (ls->session_state == SESSION_STATE_LISTENING) { ASSERT (ls->app_index < (2 << 16)); app_or_wrk_index = ls->app_index; } else { ASSERT (ls->app_wrk_index < (2 << 16)); app_or_wrk_index = ls->app_wrk_index; } return ((u32) app_or_wrk_index << 16 | (u32) ls->session_index); } always_inline void local_session_parse_id (u32 ls_id, u32 * app_or_wrk, u32 * session_index) { *app_or_wrk = ls_id >> 16; *session_index = ls_id & 0xFF; } always_inline void local_session_parse_handle (session_handle_t handle, u32 * app_or_wrk_index, u32 * session_index) { u32 bottom; ASSERT ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX); bottom = (handle & 0xFFFFFFFF); local_session_parse_id (bottom, app_or_wrk_index, session_index); } always_inline session_handle_t application_local_session_handle (local_session_t * ls) { return ((u64) SESSION_LOCAL_HANDLE_PREFIX << 32) | (u64) local_session_id (ls); } always_inline local_session_t * application_get_local_listen_session (application_t * app, u32 session_index) { return pool_elt_at_index (app->local_listen_sessions, session_index); } always_inline local_session_t * application_get_local_listener_w_handle (session_handle_t handle) { u32 server_index, session_index; application_t *app; local_session_parse_handle (handle, &server_index, &session_index); app = application_get (server_index); return application_get_local_listen_session (app, session_index); } always_inline u8 application_local_session_listener_has_transport (local_session_t * ls) { transport_proto_t tp; tp = session_type_transport_proto (ls->listener_session_type); return (tp != TRANSPORT_PROTO_NONE); } void mq_send_local_session_disconnected_cb (u32 app_or_wrk, local_session_t * ls); uword unformat_application_proto (unformat_input_t * input, va_list * args); #endif /* SRC_VNET_SESSION_APPLICATION_H_ */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */