xref: /titanic_41/usr/src/lib/libslp/clib/slp_ua_common.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <syslog.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <thread.h>
34*7c478bd9Sstevel@tonic-gate #include <synch.h>
35*7c478bd9Sstevel@tonic-gate #include <slp-internal.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /* This is used to pass needed params to consumer_thr and slp_call */
38*7c478bd9Sstevel@tonic-gate struct thr_call_args {
39*7c478bd9Sstevel@tonic-gate 	slp_handle_impl_t *hp;
40*7c478bd9Sstevel@tonic-gate 	SLPGenericAppCB *cb;
41*7c478bd9Sstevel@tonic-gate 	void *cookie;
42*7c478bd9Sstevel@tonic-gate 	SLPMsgReplyCB *msg_cb;
43*7c478bd9Sstevel@tonic-gate 	slp_target_list_t *targets;
44*7c478bd9Sstevel@tonic-gate };
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static SLPError consumer(void *);
47*7c478bd9Sstevel@tonic-gate static void slp_call(void *);
48*7c478bd9Sstevel@tonic-gate static SLPError check_message_fit(slp_handle_impl_t *, slp_target_list_t *);
49*7c478bd9Sstevel@tonic-gate 
slp_ua_common(SLPHandle hSLP,const char * scopes,SLPGenericAppCB cb,void * cookie,SLPMsgReplyCB msg_cb)50*7c478bd9Sstevel@tonic-gate SLPError slp_ua_common(SLPHandle hSLP, const char *scopes,
51*7c478bd9Sstevel@tonic-gate 			SLPGenericAppCB cb, void *cookie,
52*7c478bd9Sstevel@tonic-gate 			SLPMsgReplyCB msg_cb) {
53*7c478bd9Sstevel@tonic-gate 	slp_handle_impl_t *hp;
54*7c478bd9Sstevel@tonic-gate 	slp_target_list_t *targets;
55*7c478bd9Sstevel@tonic-gate 	struct thr_call_args *args;
56*7c478bd9Sstevel@tonic-gate 	slp_queue_t *q;
57*7c478bd9Sstevel@tonic-gate 	SLPError err;
58*7c478bd9Sstevel@tonic-gate 	thread_t tid;
59*7c478bd9Sstevel@tonic-gate 	int terr;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	hp = (slp_handle_impl_t *)hSLP;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	/* select targets */
64*7c478bd9Sstevel@tonic-gate 	if ((err = slp_new_target_list(hp, scopes, &targets)) != SLP_OK)
65*7c478bd9Sstevel@tonic-gate 		return (err);
66*7c478bd9Sstevel@tonic-gate 	if ((err = check_message_fit(hp, targets)) != SLP_OK) {
67*7c478bd9Sstevel@tonic-gate 		slp_destroy_target_list(targets);
68*7c478bd9Sstevel@tonic-gate 		return (err);
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	/* populate the args structure */
72*7c478bd9Sstevel@tonic-gate 	args = malloc(sizeof (*args));
73*7c478bd9Sstevel@tonic-gate 	if (args == NULL) {
74*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "ua_common", "out of memory");
75*7c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	args->hp = hp;
79*7c478bd9Sstevel@tonic-gate 	args->cb = cb;
80*7c478bd9Sstevel@tonic-gate 	args->cookie = cookie;
81*7c478bd9Sstevel@tonic-gate 	args->msg_cb = msg_cb;
82*7c478bd9Sstevel@tonic-gate 	args->targets = targets;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	/* create the queue that this call will use */
85*7c478bd9Sstevel@tonic-gate 	q = slp_new_queue(&err);	/* freed in consumer_thr */
86*7c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
87*7c478bd9Sstevel@tonic-gate 		goto error;
88*7c478bd9Sstevel@tonic-gate 	hp->q = q;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	/* kick off the producer thread */
91*7c478bd9Sstevel@tonic-gate 	if ((terr = thr_create(
92*7c478bd9Sstevel@tonic-gate 		NULL, 0, (void *(*)(void *)) slp_call, args, 0, &tid)) != 0) {
93*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "ua_common", "could not start thread: %s",
94*7c478bd9Sstevel@tonic-gate 			strerror(terr));
95*7c478bd9Sstevel@tonic-gate 		err = SLP_INTERNAL_SYSTEM_ERROR;
96*7c478bd9Sstevel@tonic-gate 		goto error;
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 	hp->producer_tid = tid;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if (hp->async) {
101*7c478bd9Sstevel@tonic-gate 		/* kick off the consumer thread */
102*7c478bd9Sstevel@tonic-gate 		if ((terr = thr_create(
103*7c478bd9Sstevel@tonic-gate 			NULL, 0, (void *(*)(void *))consumer,
104*7c478bd9Sstevel@tonic-gate 			args, 0, NULL)) != 0) {
105*7c478bd9Sstevel@tonic-gate 			slp_err(LOG_CRIT, 0, "ua_common",
106*7c478bd9Sstevel@tonic-gate 				"could not start thread: %s",
107*7c478bd9Sstevel@tonic-gate 				strerror(terr));
108*7c478bd9Sstevel@tonic-gate 			err = SLP_INTERNAL_SYSTEM_ERROR;
109*7c478bd9Sstevel@tonic-gate 			/* cleanup producer thread, if necessary */
110*7c478bd9Sstevel@tonic-gate 			hp->cancel = 1;
111*7c478bd9Sstevel@tonic-gate 			(void) thr_join(tid, NULL, NULL);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 			goto error;
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 		return (SLP_OK);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 	/* else	sync */
118*7c478bd9Sstevel@tonic-gate 	return (consumer(args));
119*7c478bd9Sstevel@tonic-gate error:
120*7c478bd9Sstevel@tonic-gate 	free(args);
121*7c478bd9Sstevel@tonic-gate 	return (err);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
consumer(void * ap)124*7c478bd9Sstevel@tonic-gate static SLPError consumer(void *ap) {
125*7c478bd9Sstevel@tonic-gate 	slp_handle_impl_t *hp;
126*7c478bd9Sstevel@tonic-gate 	char *reply;
127*7c478bd9Sstevel@tonic-gate 	void *collator;
128*7c478bd9Sstevel@tonic-gate 	int numResults = 0;
129*7c478bd9Sstevel@tonic-gate 	struct thr_call_args *args = (struct thr_call_args *)ap;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	hp = args->hp;
132*7c478bd9Sstevel@tonic-gate 	collator = NULL;
133*7c478bd9Sstevel@tonic-gate 	hp->consumer_tid = thr_self();
134*7c478bd9Sstevel@tonic-gate 	/* while cb wants more and there is more to get ... */
135*7c478bd9Sstevel@tonic-gate 	for (;;) {
136*7c478bd9Sstevel@tonic-gate 		SLPBoolean cont;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 		reply = slp_dequeue(hp->q);
139*7c478bd9Sstevel@tonic-gate 		/* reply == NULL if no more available or SLPClosed */
140*7c478bd9Sstevel@tonic-gate 		cont = args->msg_cb(hp, reply, args->cb, args->cookie,
141*7c478bd9Sstevel@tonic-gate 				    &collator, &numResults);
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 		if (reply) {
144*7c478bd9Sstevel@tonic-gate 		    free(reply);
145*7c478bd9Sstevel@tonic-gate 		} else {
146*7c478bd9Sstevel@tonic-gate 		    break;
147*7c478bd9Sstevel@tonic-gate 		}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		if (!cont) {
150*7c478bd9Sstevel@tonic-gate 		    /* cb doesn't want any more; invoke last call */
151*7c478bd9Sstevel@tonic-gate 		    args->msg_cb(hp, NULL, args->cb, args->cookie,
152*7c478bd9Sstevel@tonic-gate 				    &collator, &numResults);
153*7c478bd9Sstevel@tonic-gate 		    break;
154*7c478bd9Sstevel@tonic-gate 		}
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 	/* cleanup */
157*7c478bd9Sstevel@tonic-gate 	/* clean stop producer [thread] */
158*7c478bd9Sstevel@tonic-gate 	hp->cancel = 1;
159*7c478bd9Sstevel@tonic-gate 	(void) thr_join(hp->producer_tid, NULL, NULL);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	/* empty and free queue */
162*7c478bd9Sstevel@tonic-gate 	slp_flush_queue(hp->q, free);
163*7c478bd9Sstevel@tonic-gate 	slp_destroy_queue(hp->q);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	free(args);
166*7c478bd9Sstevel@tonic-gate 	slp_end_call(hp);
167*7c478bd9Sstevel@tonic-gate 	return (SLP_OK);
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate /*
171*7c478bd9Sstevel@tonic-gate  * This is the producer thread
172*7c478bd9Sstevel@tonic-gate  */
slp_call(void * ap)173*7c478bd9Sstevel@tonic-gate static void slp_call(void *ap) {
174*7c478bd9Sstevel@tonic-gate 	struct thr_call_args *args = (struct thr_call_args *)ap;
175*7c478bd9Sstevel@tonic-gate 	slp_target_t *t;
176*7c478bd9Sstevel@tonic-gate 	const char *uc_scopes, *mc_scopes;
177*7c478bd9Sstevel@tonic-gate 	SLPBoolean use_tcp = SLP_FALSE;
178*7c478bd9Sstevel@tonic-gate 	size_t len;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	/* Unicast */
181*7c478bd9Sstevel@tonic-gate 	if (uc_scopes = slp_get_uc_scopes(args->targets)) {
182*7c478bd9Sstevel@tonic-gate 		size_t mtu;
183*7c478bd9Sstevel@tonic-gate 		int i;
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 		/* calculate msg length */
186*7c478bd9Sstevel@tonic-gate 		len = slp_hdrlang_length(args->hp);
187*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < args->hp->msg.iovlen; i++) {
188*7c478bd9Sstevel@tonic-gate 			len += args->hp->msg.iov[i].iov_len;
189*7c478bd9Sstevel@tonic-gate 		}
190*7c478bd9Sstevel@tonic-gate 		len += strlen(uc_scopes);
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		mtu = slp_get_mtu();
193*7c478bd9Sstevel@tonic-gate 		if (len > mtu)
194*7c478bd9Sstevel@tonic-gate 			use_tcp = SLP_TRUE;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 		for (
197*7c478bd9Sstevel@tonic-gate 			t = slp_next_uc_target(args->targets);
198*7c478bd9Sstevel@tonic-gate 			t;
199*7c478bd9Sstevel@tonic-gate 			t = slp_next_uc_target(args->targets)) {
200*7c478bd9Sstevel@tonic-gate 			if (args->hp->cancel)
201*7c478bd9Sstevel@tonic-gate 				break;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 			if (use_tcp)
204*7c478bd9Sstevel@tonic-gate 				slp_uc_tcp_send(args->hp, t, uc_scopes,
205*7c478bd9Sstevel@tonic-gate 						SLP_FALSE, 0);
206*7c478bd9Sstevel@tonic-gate 			else
207*7c478bd9Sstevel@tonic-gate 				slp_uc_udp_send(args->hp, t, uc_scopes);
208*7c478bd9Sstevel@tonic-gate 		}
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	/* Multicast */
212*7c478bd9Sstevel@tonic-gate 	if ((!args->hp->cancel) &&
213*7c478bd9Sstevel@tonic-gate 	    (mc_scopes = slp_get_mc_scopes(args->targets)))
214*7c478bd9Sstevel@tonic-gate 		slp_mc_send(args->hp, mc_scopes);
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	/* Wait for TCP to complete, if necessary */
217*7c478bd9Sstevel@tonic-gate 	if (args->hp->tcp_lock)
218*7c478bd9Sstevel@tonic-gate 		slp_tcp_wait(args->hp);
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	slp_destroy_target_list(args->targets);
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	/* free the message */
223*7c478bd9Sstevel@tonic-gate 	free(args->hp->msg.iov);
224*7c478bd9Sstevel@tonic-gate 	free(args->hp->msg.msg);
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	/* null terminate message queue */
227*7c478bd9Sstevel@tonic-gate 	(void) slp_enqueue(args->hp->q, NULL);
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	thr_exit(NULL);	/* we're outa here */
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate /*
233*7c478bd9Sstevel@tonic-gate  * If the message to be sent needs to be multicast, check that it
234*7c478bd9Sstevel@tonic-gate  * can fit into a datagram. If not, return BUFFER_OVERFLOW, otherwise
235*7c478bd9Sstevel@tonic-gate  * return SLP_OK.
236*7c478bd9Sstevel@tonic-gate  */
check_message_fit(slp_handle_impl_t * hp,slp_target_list_t * targets)237*7c478bd9Sstevel@tonic-gate static SLPError check_message_fit(slp_handle_impl_t *hp,
238*7c478bd9Sstevel@tonic-gate 					slp_target_list_t *targets) {
239*7c478bd9Sstevel@tonic-gate 	size_t msgSize;
240*7c478bd9Sstevel@tonic-gate 	int i;
241*7c478bd9Sstevel@tonic-gate 	const char *mc_scopes;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	if (!(mc_scopes = slp_get_mc_scopes(targets)))
244*7c478bd9Sstevel@tonic-gate 		return (SLP_OK);	/* no mc targets to worry about */
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	msgSize = slp_hdrlang_length(hp);
247*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < hp->msg.iovlen; i++) {
248*7c478bd9Sstevel@tonic-gate 		msgSize += hp->msg.iov[i].iov_len;
249*7c478bd9Sstevel@tonic-gate 	}
250*7c478bd9Sstevel@tonic-gate 	msgSize += strlen(mc_scopes);
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	if (msgSize > slp_get_mtu())
253*7c478bd9Sstevel@tonic-gate 		return (SLP_BUFFER_OVERFLOW);
254*7c478bd9Sstevel@tonic-gate 	return (SLP_OK);
255*7c478bd9Sstevel@tonic-gate }
256