summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/sockfs/socknotify.c
blob: d6c1f9ea859f067bd3630f1278d4d414a280674a (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/stropts.h>
#include <sys/socketvar.h>
#include <sys/ksocket.h>
#include <io/ksocket/ksocket_impl.h>
#include <fs/sockfs/sockcommon.h>
#include <fs/sockfs/sodirect.h>
#include <fs/sockfs/sockfilter_impl.h>

/*
 * There can only be a single thread waiting for data (enforced by
 * so_lock_read()), whereas for write there might be multiple threads
 * waiting for transmit buffers. So therefore we use cv_broadcast for
 * write and cv_signal for read.
 */
#define	SO_WAKEUP_READER(so) {				\
	if ((so)->so_rcv_wakeup) {			\
		(so)->so_rcv_wakeup = B_FALSE;		\
		cv_signal(&(so)->so_rcv_cv);		\
	}						\
}

#define	SO_WAKEUP_WRITER(so) {			\
	if ((so)->so_snd_wakeup) {		\
		(so)->so_snd_wakeup = B_FALSE;	\
		cv_broadcast(&(so)->so_snd_cv);	\
	}					\
}

static int i_so_notify_last_rx(struct sonode *, int *, int *);
static int i_so_notify_last_tx(struct sonode *, int *, int *);

/*
 * The notification functions must be called with so_lock held,
 * and they will all *drop* so_lock before returning.
 */

/*
 * Wake up anyone waiting for the connection to be established.
 */
void
so_notify_connected(struct sonode *so)
{
	ASSERT(MUTEX_HELD(&so->so_lock));

	if (IS_KERNEL_SOCKET(so)) {
		KSOCKET_CALLBACK(so, connected, 0);
		mutex_exit(&so->so_lock);
	} else {
		socket_sendsig(so, SOCKETSIG_WRITE);
		mutex_exit(&so->so_lock);
		pollwakeup(&so->so_poll_list, POLLOUT);
	}
	sof_sonode_notify_filters(so, SOF_EV_CONNECTED, 0);

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * The socket is disconnecting, so no more data can be sent. Wake up
 * anyone that is waiting to send data.
 */
void
so_notify_disconnecting(struct sonode *so)
{
	int pollev = 0;
	int sigev = 0;

	ASSERT(MUTEX_HELD(&so->so_lock));
	(void) i_so_notify_last_tx(so, &pollev, &sigev);

	if (IS_KERNEL_SOCKET(so)) {
		KSOCKET_CALLBACK(so, cantsendmore, 0);
		mutex_exit(&so->so_lock);
	} else {
		if (sigev != 0)
			socket_sendsig(so, sigev);
		mutex_exit(&so->so_lock);
		if (pollev != 0)
			pollwakeup(&so->so_poll_list, pollev);
	}
	sof_sonode_notify_filters(so, SOF_EV_CANTSENDMORE, 0);

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * The socket is disconnected, so not more data can be sent or received.
 * Wake up anyone that is waiting to send or receive data.
 */
void
so_notify_disconnected(struct sonode *so, boolean_t connfailed, int error)
{
	int pollev = 0;
	int sigev = 0;

	ASSERT(MUTEX_HELD(&so->so_lock));

	(void) i_so_notify_last_tx(so, &pollev, &sigev);
	(void) i_so_notify_last_rx(so, &pollev, &sigev);

	if (IS_KERNEL_SOCKET(so)) {
		if (connfailed) {
			KSOCKET_CALLBACK(so, disconnected, error);
		} else {
			KSOCKET_CALLBACK(so, connectfailed, error);
		}
		mutex_exit(&so->so_lock);
	} else {
		if (sigev != 0)
			socket_sendsig(so, sigev);
		mutex_exit(&so->so_lock);
		if (pollev != 0)
			pollwakeup(&so->so_poll_list, pollev);
	}
	sof_sonode_notify_filters(so, (connfailed) ? SOF_EV_CONNECTFAILED :
	    SOF_EV_DISCONNECTED, error);

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * The socket is writeable. Wake up anyone waiting to send data.
 */
void
so_notify_writable(struct sonode *so)
{
	ASSERT(MUTEX_HELD(&so->so_lock));

	SO_WAKEUP_WRITER(so);

	if (IS_KERNEL_SOCKET(so)) {
		KSOCKET_CALLBACK(so, cansend, 0);
		mutex_exit(&so->so_lock);
	} else {
		socket_sendsig(so, SOCKETSIG_WRITE);
		mutex_exit(&so->so_lock);
		pollwakeup(&so->so_poll_list, POLLOUT);
	}

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));

	/* filters can start injecting data */
	if (so->so_filter_active > 0)
		sof_sonode_notify_filters(so, SOF_EV_INJECT_DATA_OUT_OK, 0);
}

/*
 * Data is available, so wake up anyone waiting for data.
 */
void
so_notify_data(struct sonode *so, size_t qlen)
{
	ASSERT(MUTEX_HELD(&so->so_lock));

	SO_WAKEUP_READER(so);

	if (IS_KERNEL_SOCKET(so)) {
		KSOCKET_CALLBACK(so, newdata, qlen);
		mutex_exit(&so->so_lock);
	} else {
		socket_sendsig(so, SOCKETSIG_READ);
		if (so->so_pollev & (SO_POLLEV_IN|SO_POLLEV_ALWAYS)) {
			so->so_pollev &= ~SO_POLLEV_IN;
			mutex_exit(&so->so_lock);
			pollwakeup(&so->so_poll_list, POLLIN|POLLRDNORM);
		} else {
			mutex_exit(&so->so_lock);
		}
	}

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * Transient error. Wake up anyone waiting to send or receive data.
 */
void
so_notify_error(struct sonode *so)
{
	ASSERT(MUTEX_HELD(&so->so_lock));

	SO_WAKEUP_WRITER(so);
	SO_WAKEUP_READER(so);

	if (IS_KERNEL_SOCKET(so)) {
		KSOCKET_CALLBACK(so, error, 0);
		mutex_exit(&so->so_lock);
	} else {
		socket_sendsig(so, SOCKETSIG_WRITE|SOCKETSIG_READ);
		so->so_pollev &= ~SO_POLLEV_IN;
		mutex_exit(&so->so_lock);
		pollwakeup(&so->so_poll_list, POLLOUT|POLLIN|POLLRDNORM);
	}

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * Out-of-band data is incoming, notify any interested parties.
 */
void
so_notify_oobsig(struct sonode *so)
{
	socket_sendsig(so, SOCKETSIG_URG);
	mutex_exit(&so->so_lock);
	pollwakeup(&so->so_poll_list, POLLRDBAND);
}

/*
 * Received out-of-band data. If the OOB data is delivered inline, then
 * in addition of regular OOB notification, anyone waiting for normal
 * data is also notified.
 */
void
so_notify_oobdata(struct sonode *so, boolean_t oob_inline)
{
	ASSERT(MUTEX_HELD(&so->so_lock));
	if (so->so_direct != NULL)
		SOD_UIOAFINI(so->so_direct);

	SO_WAKEUP_READER(so);

	if (IS_KERNEL_SOCKET(so)) {
		KSOCKET_CALLBACK(so, oobdata, 0);
		mutex_exit(&so->so_lock);
	} else {
		if (oob_inline) {
			socket_sendsig(so, SOCKETSIG_READ);
			so->so_pollev &= ~SO_POLLEV_IN;
			mutex_exit(&so->so_lock);
			pollwakeup(&so->so_poll_list,
			    POLLRDBAND|POLLIN|POLLRDNORM);
		} else {
			mutex_exit(&so->so_lock);
			pollwakeup(&so->so_poll_list, POLLRDBAND);
		}
	}

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * End-of-file has been reach, so peer will send no new data. Wake up
 * anyone that is waiting for data.
 */
void
so_notify_eof(struct sonode *so)
{
	int pollev = 0;
	int sigev = 0;

	ASSERT(MUTEX_HELD(&so->so_lock));

	(void) i_so_notify_last_rx(so, &pollev, &sigev);

	if (IS_KERNEL_SOCKET(so)) {
		KSOCKET_CALLBACK(so, cantrecvmore, 0);
		mutex_exit(&so->so_lock);
	} else {
		if (sigev != 0)
			socket_sendsig(so, sigev);
		mutex_exit(&so->so_lock);
		if (pollev != 0)
			pollwakeup(&so->so_poll_list, pollev);

	}
	sof_sonode_notify_filters(so, SOF_EV_CANTRECVMORE, 0);

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * Wake up anyone waiting for a new connection.
 */
void
so_notify_newconn(struct sonode *so)
{
	ASSERT(MUTEX_HELD(&so->so_lock));

	if (IS_KERNEL_SOCKET(so)) {
		KSOCKET_CALLBACK(so, newconn, 0);
		mutex_exit(&so->so_lock);
	} else {
		socket_sendsig(so, SOCKETSIG_READ);
		if (so->so_pollev & (SO_POLLEV_IN|SO_POLLEV_ALWAYS)) {
			so->so_pollev &= ~SO_POLLEV_IN;
			mutex_exit(&so->so_lock);
			pollwakeup(&so->so_poll_list, POLLIN|POLLRDNORM);
		} else {
			mutex_exit(&so->so_lock);
		}
	}

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * User initated shutdown/close, wake anyone that is trying to do
 * an operation that is no longer possible.
 */
void
so_notify_shutdown(struct sonode *so)
{
	int pollev = 0;
	int sigev = 0;

	ASSERT(MUTEX_HELD(&so->so_lock));
	ASSERT(so->so_state & (SS_CANTSENDMORE|SS_CANTRCVMORE));

	if (so->so_state & SS_CANTSENDMORE)
		(void) i_so_notify_last_tx(so, &pollev, &sigev);
	if (so->so_state & SS_CANTRCVMORE)
		(void) i_so_notify_last_rx(so, &pollev, &sigev);

	if (sigev != 0)
		socket_sendsig(so, sigev);
	mutex_exit(&so->so_lock);
	if (pollev != 0)
		pollwakeup(&so->so_poll_list, pollev);

	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
}

/*
 * No more data will be coming in, and this will be the last notification
 * made.
 */
static int
i_so_notify_last_rx(struct sonode *so, int *pollev, int *sigev)
{
	if (!(so->so_state & SS_SENTLASTREADSIG)) {
		SOCKET_TIMER_CANCEL(so);
		SO_WAKEUP_READER(so);
		so->so_state |= SS_SENTLASTREADSIG;
		so->so_pollev &= ~SO_POLLEV_IN;

		*pollev |= POLLIN|POLLRDNORM;
		*sigev |= SOCKETSIG_READ;

		return (1);
	} else {
		return (0);
	}
}

/*
 * The socket is un-writeable. Make one last notification.
 */
static int
i_so_notify_last_tx(struct sonode *so, int *pollev, int *sigev)
{
	if (!(so->so_state & SS_SENTLASTWRITESIG)) {
		SO_WAKEUP_WRITER(so);
		so->so_state |= SS_SENTLASTWRITESIG;

		*pollev |= POLLOUT;
		*sigev |= SOCKETSIG_WRITE;

		return (1);
	} else {
		return (0);
	}
}