hsa: move tcp_echo to vpp_echo
[vpp.git] / src / plugins / hs_apps / sapi / vpp_echo_proto_tcp.c
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 tcp_echo_cleanup_cb (echo_session_t * s, u8 parent_died)
23 {
24   echo_main_t *em = &echo_main;
25   echo_session_t *ls;
26   ASSERT (s->session_state < ECHO_SESSION_STATE_CLOSED);
27   if (parent_died)
28     clib_atomic_fetch_add (&em->stats.clean_count.s, 1);
29   else if (s->listener_index != SESSION_INVALID_INDEX)
30     {
31       ls = pool_elt_at_index (em->sessions, s->listener_index);
32       clib_atomic_sub_fetch (&ls->accepted_session_count, 1);
33     }
34
35
36   clib_atomic_sub_fetch (&em->n_clients_connected, 1);
37   s->session_state = ECHO_SESSION_STATE_CLOSED;
38   if (!em->n_clients_connected)
39     em->state = STATE_DATA_DONE;
40 }
41
42 static void
43 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   echo_main_t *em = &echo_main;
48   echo_session_t *session = pool_elt_at_index (em->sessions, session_index);
49   if (is_failed)
50     {
51       ECHO_FAIL ("Bapi connect errored");
52       return;                   /* Dont handle bapi connect errors for now */
53     }
54
55   ECHO_LOG (1, "Connected session 0x%lx -> URI",
56             ((session_connected_msg_t *) mp)->handle);
57   session->session_type = ECHO_SESSION_TYPE_STREAM;
58   session->accepted_session_count = 0;
59   clib_atomic_fetch_add (&em->n_clients_connected, 1);
60   session->bytes_to_send = em->bytes_to_send;
61   session->bytes_to_receive = em->bytes_to_receive;
62   session->session_state = ECHO_SESSION_STATE_READY;
63   em->data_thread_args[client_index++] = session->session_index;
64
65   if (em->n_clients_connected == em->n_clients && em->state < STATE_READY)
66     {
67       echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
68       em->state = STATE_READY;
69     }
70 }
71
72 static void
73 tcp_echo_accepted_cb (session_accepted_msg_t * mp, echo_session_t * session)
74 {
75   static u32 client_index = 0;
76   echo_main_t *em = &echo_main;
77   echo_session_t *ls;
78
79   echo_notify_event (em, ECHO_EVT_FIRST_QCONNECT);
80   ls = pool_elt_at_index (em->sessions, session->listener_index);
81   session->session_type = ECHO_SESSION_TYPE_STREAM;
82   echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
83   clib_atomic_fetch_add (&ls->accepted_session_count, 1);
84   clib_atomic_fetch_add (&em->n_clients_connected, 1);
85
86   session->bytes_to_send = em->bytes_to_send;
87   session->bytes_to_receive = em->bytes_to_receive;
88   em->data_thread_args[client_index++] = session->session_index;
89   session->session_state = ECHO_SESSION_STATE_READY;
90
91   if (em->n_clients_connected == em->n_clients && em->state < STATE_READY)
92     {
93       echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
94       em->state = STATE_READY;
95     }
96 }
97
98 static void
99 tcp_echo_disconnected_reply_cb (echo_session_t * s)
100 {
101   s->session_state = ECHO_SESSION_STATE_CLOSING;
102 }
103
104 static void
105 tcp_echo_disconnected_cb (session_disconnected_msg_t * mp, echo_session_t * s)
106 {
107   echo_main_t *em = &echo_main;
108   echo_session_print_stats (em, s);
109   if (s->bytes_to_receive || s->bytes_to_send)
110     s->session_state = ECHO_SESSION_STATE_AWAIT_DATA;
111   else
112     s->session_state = ECHO_SESSION_STATE_CLOSING;
113   clib_atomic_fetch_add (&em->stats.close_count.s, 1);
114 }
115
116 static void
117 tcp_echo_reset_cb (session_reset_msg_t * mp, echo_session_t * s)
118 {
119   echo_main_t *em = &echo_main;
120   clib_atomic_fetch_add (&em->stats.reset_count.s, 1);
121   s->session_state = ECHO_SESSION_STATE_CLOSING;
122 }
123
124 echo_proto_cb_vft_t echo_tcp_proto_cb_vft = {
125   .disconnected_cb = tcp_echo_disconnected_cb,
126   .connected_cb = tcp_echo_connected_cb,
127   .accepted_cb = tcp_echo_accepted_cb,
128   .reset_cb = tcp_echo_reset_cb,
129   .disconnected_reply_cb = tcp_echo_disconnected_reply_cb,
130   .cleanup_cb = tcp_echo_cleanup_cb,
131 };
132
133 ECHO_REGISTER_PROTO (TRANSPORT_PROTO_TCP, echo_tcp_proto_cb_vft);
134 ECHO_REGISTER_PROTO (TRANSPORT_PROTO_TLS, echo_tcp_proto_cb_vft);
135 ECHO_REGISTER_PROTO (TRANSPORT_PROTO_SCTP, echo_tcp_proto_cb_vft);
136
137 /*
138  * fd.io coding-style-patch-verification: ON
139  *
140  * Local Variables:
141  * eval: (c-set-style "gnu")
142  * End:
143  */