Line data Source code
1 : /*
2 : * Copyright (c) 2019 Cisco and/or its affiliates.
3 : * Licensed under the Apache License, Version 2.0 (the "License");
4 : * you may not use this file except in compliance with the License.
5 : * You may obtain a copy of the License at:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
9 : * Unless required by applicable law or agreed to in writing, software
10 : * distributed under the License is distributed on an "AS IS" BASIS,
11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : * See the License for the specific language governing permissions and
13 : * limitations under the License.
14 : */
15 :
16 : #include <stdio.h>
17 : #include <signal.h>
18 :
19 : #include <hs_apps/sapi/vpp_echo_common.h>
20 :
21 : static void
22 0 : tcp_echo_cleanup_cb (echo_session_t * s, u8 parent_died)
23 : {
24 0 : echo_main_t *em = &echo_main;
25 : echo_session_t *ls;
26 0 : ASSERT (s->session_state < ECHO_SESSION_STATE_CLOSED);
27 0 : if (parent_died)
28 0 : clib_atomic_fetch_add (&em->stats.clean_count.s, 1);
29 0 : else if (s->listener_index != SESSION_INVALID_INDEX)
30 : {
31 0 : ls = pool_elt_at_index (em->sessions, s->listener_index);
32 0 : clib_atomic_sub_fetch (&ls->accepted_session_count, 1);
33 : }
34 :
35 :
36 0 : clib_atomic_sub_fetch (&em->n_clients_connected, 1);
37 0 : s->session_state = ECHO_SESSION_STATE_CLOSED;
38 0 : if (!em->n_clients_connected)
39 0 : em->state = STATE_DATA_DONE;
40 0 : }
41 :
42 : static void
43 0 : tcp_echo_connected_cb (session_connected_bundled_msg_t * mp,
44 : u32 session_index, u8 is_failed)
45 : {
46 : static u32 client_index = 0;
47 0 : echo_main_t *em = &echo_main;
48 0 : echo_session_t *session = pool_elt_at_index (em->sessions, session_index);
49 0 : if (is_failed)
50 : {
51 0 : ECHO_FAIL (ECHO_FAIL_TCP_BAPI_CONNECT,
52 : "Bapi connect errored on session %u", session_index);
53 0 : return; /* Dont handle bapi connect errors for now */
54 : }
55 :
56 0 : ECHO_LOG (2, "Connected session 0x%lx -> URI",
57 : ((session_connected_msg_t *) mp)->handle);
58 0 : session->session_type = ECHO_SESSION_TYPE_STREAM;
59 0 : session->accepted_session_count = 0;
60 0 : clib_atomic_fetch_add (&em->n_clients_connected, 1);
61 0 : session->bytes_to_send = em->bytes_to_send;
62 0 : session->bytes_to_receive = em->bytes_to_receive;
63 0 : session->session_state = ECHO_SESSION_STATE_READY;
64 0 : em->data_thread_args[client_index++] = session->session_index;
65 :
66 0 : if (em->n_clients_connected == em->n_clients && em->state < STATE_READY)
67 : {
68 0 : echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
69 0 : em->state = STATE_READY;
70 : }
71 : }
72 :
73 : static void
74 0 : tcp_echo_accepted_cb (session_accepted_msg_t * mp, echo_session_t * session)
75 : {
76 : static u32 client_index = 0;
77 0 : echo_main_t *em = &echo_main;
78 : echo_session_t *ls;
79 :
80 0 : echo_notify_event (em, ECHO_EVT_FIRST_QCONNECT);
81 0 : ls = pool_elt_at_index (em->sessions, session->listener_index);
82 0 : session->session_type = ECHO_SESSION_TYPE_STREAM;
83 0 : echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
84 0 : clib_atomic_fetch_add (&ls->accepted_session_count, 1);
85 0 : clib_atomic_fetch_add (&em->n_clients_connected, 1);
86 :
87 0 : session->bytes_to_send = em->bytes_to_send;
88 0 : session->bytes_to_receive = em->bytes_to_receive;
89 0 : em->data_thread_args[client_index++] = session->session_index;
90 0 : session->session_state = ECHO_SESSION_STATE_READY;
91 :
92 0 : if (em->n_clients_connected == em->n_clients && em->state < STATE_READY)
93 : {
94 0 : echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
95 0 : em->state = STATE_READY;
96 : }
97 0 : }
98 :
99 : static void
100 0 : tcp_echo_sent_disconnect_cb (echo_session_t * s)
101 : {
102 0 : s->session_state = ECHO_SESSION_STATE_CLOSING;
103 0 : }
104 :
105 : static void
106 0 : tcp_echo_disconnected_cb (session_disconnected_msg_t * mp, echo_session_t * s)
107 : {
108 0 : echo_main_t *em = &echo_main;
109 0 : echo_session_print_stats (em, s);
110 0 : if (s->bytes_to_receive || s->bytes_to_send)
111 0 : s->session_state = ECHO_SESSION_STATE_AWAIT_DATA;
112 : else
113 0 : s->session_state = ECHO_SESSION_STATE_CLOSING;
114 0 : clib_atomic_fetch_add (&em->stats.close_count.s, 1);
115 0 : }
116 :
117 : static void
118 0 : tcp_echo_reset_cb (session_reset_msg_t * mp, echo_session_t * s)
119 : {
120 0 : echo_main_t *em = &echo_main;
121 0 : clib_atomic_fetch_add (&em->stats.reset_count.s, 1);
122 0 : s->session_state = ECHO_SESSION_STATE_CLOSING;
123 0 : }
124 :
125 : echo_proto_cb_vft_t echo_tcp_proto_cb_vft = {
126 : .disconnected_cb = tcp_echo_disconnected_cb,
127 : .connected_cb = tcp_echo_connected_cb,
128 : .accepted_cb = tcp_echo_accepted_cb,
129 : .reset_cb = tcp_echo_reset_cb,
130 : .sent_disconnect_cb = tcp_echo_sent_disconnect_cb,
131 : .cleanup_cb = tcp_echo_cleanup_cb,
132 : };
133 :
134 : echo_proto_cb_vft_t echo_tls_proto_cb_vft = {
135 : .disconnected_cb = tcp_echo_disconnected_cb,
136 : .connected_cb = tcp_echo_connected_cb,
137 : .accepted_cb = tcp_echo_accepted_cb,
138 : .reset_cb = tcp_echo_reset_cb,
139 : .sent_disconnect_cb = tcp_echo_sent_disconnect_cb,
140 : .cleanup_cb = tcp_echo_cleanup_cb,
141 : };
142 :
143 4 : ECHO_REGISTER_PROTO (TRANSPORT_PROTO_TCP, echo_tcp_proto_cb_vft);
144 4 : ECHO_REGISTER_PROTO (TRANSPORT_PROTO_TLS, echo_tls_proto_cb_vft);
145 :
146 : /*
147 : * fd.io coding-style-patch-verification: ON
148 : *
149 : * Local Variables:
150 : * eval: (c-set-style "gnu")
151 : * End:
152 : */
|