aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/hs_apps/sapi/vpp_echo_proto_tcp.c
blob: 0a4a0bdbc1fba8cf1b53a40b4ce4cd05cdc0a6f7 (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
/*
 * Copyright (c) 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 <stdio.h>
#include <signal.h>

#include <hs_apps/sapi/vpp_echo_common.h>

static void
tcp_echo_cleanup_cb (echo_session_t * s, u8 parent_died)
{
  echo_main_t *em = &echo_main;
  echo_session_t *ls;
  ASSERT (s->session_state < ECHO_SESSION_STATE_CLOSED);
  if (parent_died)
    clib_atomic_fetch_add (&em->stats.clean_count.s, 1);
  else if (s->listener_index != SESSION_INVALID_INDEX)
    {
      ls = pool_elt_at_index (em->sessions, s->listener_index);
      clib_atomic_sub_fetch (&ls->accepted_session_count, 1);
    }


  clib_atomic_sub_fetch (&em->n_clients_connected, 1);
  s->session_state = ECHO_SESSION_STATE_CLOSED;
  if (!em->n_clients_connected)
    em->state = STATE_DATA_DONE;
}

static void
tcp_echo_connected_cb (session_connected_bundled_msg_t * mp,
		       u32 session_index, u8 is_failed)
{
  static u32 client_index = 0;
  echo_main_t *em = &echo_main;
  echo_session_t *session = pool_elt_at_index (em->sessions, session_index);
  if (is_failed)
    {
      ECHO_FAIL (ECHO_FAIL_TCP_BAPI_CONNECT,
		 "Bapi connect errored on session %u", session_index);
      return;			/* Dont handle bapi connect errors for now */
    }

  ECHO_LOG (2, "Connected session 0x%lx -> URI",
	    ((session_connected_msg_t *) mp)->handle);
  session->session_type = ECHO_SESSION_TYPE_STREAM;
  session->accepted_session_count = 0;
  clib_atomic_fetch_add (&em->n_clients_connected, 1);
  session->bytes_to_send = em->bytes_to_send;
  session->bytes_to_receive = em->bytes_to_receive;
  session->session_state = ECHO_SESSION_STATE_READY;
  em->data_thread_args[client_index++] = session->session_index;

  if (em->n_clients_connected == em->n_clients && em->state < STATE_READY)
    {
      echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
      em->state = STATE_READY;
    }
}

static void
tcp_echo_accepted_cb (session_accepted_msg_t * mp, echo_session_t * session)
{
  static u32 client_index = 0;
  echo_main_t *em = &echo_main;
  echo_session_t *ls;

  echo_notify_event (em, ECHO_EVT_FIRST_QCONNECT);
  ls = pool_elt_at_index (em->sessions, session->listener_index);
  session->session_type = ECHO_SESSION_TYPE_STREAM;
  echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
  clib_atomic_fetch_add (&ls->accepted_session_count, 1);
  clib_atomic_fetch_add (&em->n_clients_connected, 1);

  session->bytes_to_send = em->bytes_to_send;
  session->bytes_to_receive = em->bytes_to_receive;
  em->data_thread_args[client_index++] = session->session_index;
  session->session_state = ECHO_SESSION_STATE_READY;

  if (em->n_clients_connected == em->n_clients && em->state < STATE_READY)
    {
      echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
      em->state = STATE_READY;
    }
}

static void
tcp_echo_sent_disconnect_cb (echo_session_t * s)
{
  s->session_state = ECHO_SESSION_STATE_CLOSING;
}

static void
tcp_echo_disconnected_cb (session_disconnected_msg_t * mp, echo_session_t * s)
{
  echo_main_t *em = &echo_main;
  echo_session_print_stats (em, s);
  if (s->bytes_to_receive || s->bytes_to_send)
    s->session_state = ECHO_SESSION_STATE_AWAIT_DATA;
  else
    s->session_state = ECHO_SESSION_STATE_CLOSING;
  clib_atomic_fetch_add (&em->stats.close_count.s, 1);
}

static void
tcp_echo_reset_cb (session_reset_msg_t * mp, echo_session_t * s)
{
  echo_main_t *em = &echo_main;
  clib_atomic_fetch_add (&em->stats.reset_count.s, 1);
  s->session_state = ECHO_SESSION_STATE_CLOSING;
}

echo_proto_cb_vft_t echo_tcp_proto_cb_vft = {
  .disconnected_cb = tcp_echo_disconnected_cb,
  .connected_cb = tcp_echo_connected_cb,
  .accepted_cb = tcp_echo_accepted_cb,
  .reset_cb = tcp_echo_reset_cb,
  .sent_disconnect_cb = tcp_echo_sent_disconnect_cb,
  .cleanup_cb = tcp_echo_cleanup_cb,
};

echo_proto_cb_vft_t echo_tls_proto_cb_vft = {
  .disconnected_cb = tcp_echo_disconnected_cb,
  .connected_cb = tcp_echo_connected_cb,
  .accepted_cb = tcp_echo_accepted_cb,
  .reset_cb = tcp_echo_reset_cb,
  .sent_disconnect_cb = tcp_echo_sent_disconnect_cb,
  .cleanup_cb = tcp_echo_cleanup_cb,
};

ECHO_REGISTER_PROTO (TRANSPORT_PROTO_TCP, echo_tcp_proto_cb_vft);
ECHO_REGISTER_PROTO (TRANSPORT_PROTO_TLS, echo_tls_proto_cb_vft);
ECHO_REGISTER_PROTO (TRANSPORT_PROTO_SCTP, echo_tcp_proto_cb_vft);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */