-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathconnection.c
168 lines (148 loc) · 4.04 KB
/
connection.c
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
/**
* Tempesta FW
*
* Generic connection management.
*
* Copyright (C) 2014 NatSys Lab. ([email protected]).
* Copyright (C) 2015-2022 Tempesta Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "connection.h"
#include "gfsm.h"
#include "log.h"
#include "sync_socket.h"
#include "http.h"
#include "websocket.h"
TfwConnHooks *conn_hooks[TFW_CONN_MAX_PROTOS];
/*
* Initialize the connection structure.
* It's not on any list yet, so it's safe to do so without locks.
*/
void
tfw_connection_init(TfwConn *conn)
{
memset(conn, 0, sizeof(*conn));
INIT_LIST_HEAD(&conn->list);
}
void
tfw_connection_link_peer(TfwConn *conn, TfwPeer *peer)
{
BUG_ON(conn->peer || !list_empty(&conn->list));
conn->peer = peer;
tfw_peer_add_conn(peer, &conn->list);
}
/**
* Publish the "connection is established" event via TfwConnHooks.
*/
int
tfw_connection_new(TfwConn *conn)
{
return TFW_CONN_HOOK_CALL(conn, conn_init);
}
/**
* Call connection repairing via TfwConnHooks.
*/
void
tfw_connection_repair(TfwConn *conn)
{
TFW_CONN_HOOK_CALL(conn, conn_repair);
}
int
tfw_connection_close(TfwConn *conn, bool sync)
{
int r;
/*
* This function might be called from process context on Tempesta FW
* start or stop operation or from softirq, so need to save FPU context
* to call autovectorized synchronous sockets code.
*/
kernel_fpu_begin_mask(KFPU_MXCSR);
r = TFW_CONN_HOOK_CALL(conn, conn_close, sync);
kernel_fpu_end();
return r;
}
/**
* Publish the "connection is dropped" event via TfwConnHooks.
*/
void
tfw_connection_drop(TfwConn *conn)
{
/* Ask higher levels to free resources at connection close. */
TFW_CONN_HOOK_CALL(conn, conn_drop);
BUG_ON(conn->stream.msg);
}
/*
* Publish the "connection is released" event via TfwConnHooks.
*/
void
tfw_connection_release(TfwConn *conn)
{
/* Ask higher levels to free resources at connection release. */
TFW_CONN_HOOK_CALL(conn, conn_release);
BUG_ON((TFW_CONN_TYPE(conn) & Conn_Clnt)
&& !list_empty(&((TfwCliConn *)conn)->seq_queue));
}
/*
* Send @msg through connection @conn. Code architecture decisions
* ensure that conn->sk remains valid for the life of @conn instance.
* The socket itself may have been closed, but not deleted. ss_send()
* makes sure that data is sent only on an active socket.
* Return value:
* 0 - @msg had been sent successfully;
* -EBADF - connection is broken;
* -EBUSY - transmission work queue is full;
* -ENOMEM - out-of-memory error occurred.
*/
int
tfw_connection_send(TfwConn *conn, TfwMsg *msg)
{
return TFW_CONN_HOOK_CALL(conn, conn_send, msg);
}
int
tfw_connection_recv(TfwConn *conn, struct sk_buff *skb)
{
int r = T_OK;
struct sk_buff *next;
if (skb->prev)
skb->prev->next = NULL;
for (next = skb->next; skb;
skb = next, next = next ? next->next : NULL)
{
if (likely(r == T_OK || r == T_POSTPONE)) {
skb->next = skb->prev = NULL;
if (unlikely(TFW_CONN_PROTO(conn) == TFW_FSM_WS
|| TFW_CONN_PROTO(conn) == TFW_FSM_WSS))
r = tfw_ws_msg_process(conn, skb);
else
r = tfw_http_msg_process(conn, skb);
} else {
__kfree_skb(skb);
}
}
return r;
}
void
tfw_connection_hooks_register(TfwConnHooks *hooks, int type)
{
unsigned hid = TFW_CONN_TYPE2IDX(type);
BUG_ON(hid >= TFW_CONN_MAX_PROTOS || conn_hooks[hid]);
conn_hooks[hid] = hooks;
}
void
tfw_connection_hooks_unregister(int type)
{
conn_hooks[TFW_CONN_TYPE2IDX(type)] = NULL;
}