1 /*
2  * net/tipc/port.h: Include file for TIPC port code
3  *
4  * Copyright (c) 1994-2007, Ericsson AB
5  * Copyright (c) 2004-2007, 2010-2011, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the names of the copyright holders nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #ifndef _TIPC_PORT_H
38 #define _TIPC_PORT_H
39 
40 #include "ref.h"
41 #include "net.h"
42 #include "msg.h"
43 #include "node_subscr.h"
44 
45 #define TIPC_FLOW_CONTROL_WIN 512
46 
47 typedef void (*tipc_msg_err_event) (void *usr_handle, u32 portref,
48 		struct sk_buff **buf, unsigned char const *data,
49 		unsigned int size, int reason,
50 		struct tipc_portid const *attmpt_destid);
51 
52 typedef void (*tipc_named_msg_err_event) (void *usr_handle, u32 portref,
53 		struct sk_buff **buf, unsigned char const *data,
54 		unsigned int size, int reason,
55 		struct tipc_name_seq const *attmpt_dest);
56 
57 typedef void (*tipc_conn_shutdown_event) (void *usr_handle, u32 portref,
58 		struct sk_buff **buf, unsigned char const *data,
59 		unsigned int size, int reason);
60 
61 typedef void (*tipc_msg_event) (void *usr_handle, u32 portref,
62 		struct sk_buff **buf, unsigned char const *data,
63 		unsigned int size, unsigned int importance,
64 		struct tipc_portid const *origin);
65 
66 typedef void (*tipc_named_msg_event) (void *usr_handle, u32 portref,
67 		struct sk_buff **buf, unsigned char const *data,
68 		unsigned int size, unsigned int importance,
69 		struct tipc_portid const *orig,
70 		struct tipc_name_seq const *dest);
71 
72 typedef void (*tipc_conn_msg_event) (void *usr_handle, u32 portref,
73 		struct sk_buff **buf, unsigned char const *data,
74 		unsigned int size);
75 
76 typedef void (*tipc_continue_event) (void *usr_handle, u32 portref);
77 
78 /**
79  * struct user_port - TIPC user port (used with native API)
80  * @usr_handle: user-specified field
81  * @ref: object reference to associated TIPC port
82  * <various callback routines>
83  */
84 
85 struct user_port {
86 	void *usr_handle;
87 	u32 ref;
88 	tipc_msg_err_event err_cb;
89 	tipc_named_msg_err_event named_err_cb;
90 	tipc_conn_shutdown_event conn_err_cb;
91 	tipc_msg_event msg_cb;
92 	tipc_named_msg_event named_msg_cb;
93 	tipc_conn_msg_event conn_msg_cb;
94 	tipc_continue_event continue_event_cb;
95 };
96 
97 /**
98  * struct tipc_port - TIPC port structure
99  * @usr_handle: pointer to additional user-defined information about port
100  * @lock: pointer to spinlock for controlling access to port
101  * @connected: non-zero if port is currently connected to a peer port
102  * @conn_type: TIPC type used when connection was established
103  * @conn_instance: TIPC instance used when connection was established
104  * @conn_unacked: number of unacknowledged messages received from peer port
105  * @published: non-zero if port has one or more associated names
106  * @congested: non-zero if cannot send because of link or port congestion
107  * @max_pkt: maximum packet size "hint" used when building messages sent by port
108  * @ref: unique reference to port in TIPC object registry
109  * @phdr: preformatted message header used when sending messages
110  * @port_list: adjacent ports in TIPC's global list of ports
111  * @dispatcher: ptr to routine which handles received messages
112  * @wakeup: ptr to routine to call when port is no longer congested
113  * @user_port: ptr to user port associated with port (if any)
114  * @wait_list: adjacent ports in list of ports waiting on link congestion
115  * @waiting_pkts:
116  * @sent: # of non-empty messages sent by port
117  * @acked: # of non-empty message acknowledgements from connected port's peer
118  * @publications: list of publications for port
119  * @pub_count: total # of publications port has made during its lifetime
120  * @probing_state:
121  * @probing_interval:
122  * @timer_ref:
123  * @subscription: "node down" subscription used to terminate failed connections
124  */
125 struct tipc_port {
126 	void *usr_handle;
127 	spinlock_t *lock;
128 	int connected;
129 	u32 conn_type;
130 	u32 conn_instance;
131 	u32 conn_unacked;
132 	int published;
133 	u32 congested;
134 	u32 max_pkt;
135 	u32 ref;
136 	struct tipc_msg phdr;
137 	struct list_head port_list;
138 	u32 (*dispatcher)(struct tipc_port *, struct sk_buff *);
139 	void (*wakeup)(struct tipc_port *);
140 	struct user_port *user_port;
141 	struct list_head wait_list;
142 	u32 waiting_pkts;
143 	u32 sent;
144 	u32 acked;
145 	struct list_head publications;
146 	u32 pub_count;
147 	u32 probing_state;
148 	u32 probing_interval;
149 	struct timer_list timer;
150 	struct tipc_node_subscr subscription;
151 };
152 
153 extern spinlock_t tipc_port_list_lock;
154 struct port_list;
155 
156 /*
157  * TIPC port manipulation routines
158  */
159 struct tipc_port *tipc_createport_raw(void *usr_handle,
160 		u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
161 		void (*wakeup)(struct tipc_port *), const u32 importance);
162 
163 int tipc_reject_msg(struct sk_buff *buf, u32 err);
164 
165 int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode);
166 
167 void tipc_acknowledge(u32 port_ref, u32 ack);
168 
169 int tipc_createport(void *usr_handle,
170 		unsigned int importance, tipc_msg_err_event error_cb,
171 		tipc_named_msg_err_event named_error_cb,
172 		tipc_conn_shutdown_event conn_error_cb, tipc_msg_event msg_cb,
173 		tipc_named_msg_event named_msg_cb,
174 		tipc_conn_msg_event conn_msg_cb,
175 		tipc_continue_event continue_event_cb, u32 *portref);
176 
177 int tipc_deleteport(u32 portref);
178 
179 int tipc_portimportance(u32 portref, unsigned int *importance);
180 int tipc_set_portimportance(u32 portref, unsigned int importance);
181 
182 int tipc_portunreliable(u32 portref, unsigned int *isunreliable);
183 int tipc_set_portunreliable(u32 portref, unsigned int isunreliable);
184 
185 int tipc_portunreturnable(u32 portref, unsigned int *isunreturnable);
186 int tipc_set_portunreturnable(u32 portref, unsigned int isunreturnable);
187 
188 int tipc_publish(u32 portref, unsigned int scope,
189 		struct tipc_name_seq const *name_seq);
190 int tipc_withdraw(u32 portref, unsigned int scope,
191 		struct tipc_name_seq const *name_seq);
192 
193 int tipc_connect2port(u32 portref, struct tipc_portid const *port);
194 
195 int tipc_disconnect(u32 portref);
196 
197 int tipc_shutdown(u32 ref);
198 
199 
200 /*
201  * The following routines require that the port be locked on entry
202  */
203 int tipc_disconnect_port(struct tipc_port *tp_ptr);
204 
205 /*
206  * TIPC messaging routines
207  */
208 int tipc_send(u32 portref, unsigned int num_sect, struct iovec const *msg_sect);
209 
210 int tipc_send2name(u32 portref, struct tipc_name const *name, u32 domain,
211 		unsigned int num_sect, struct iovec const *msg_sect);
212 
213 int tipc_send2port(u32 portref, struct tipc_portid const *dest,
214 		unsigned int num_sect, struct iovec const *msg_sect);
215 
216 int tipc_send_buf2port(u32 portref, struct tipc_portid const *dest,
217 		struct sk_buff *buf, unsigned int dsz);
218 
219 int tipc_multicast(u32 portref, struct tipc_name_seq const *seq,
220 		unsigned int section_count, struct iovec const *msg);
221 
222 int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
223 			      struct iovec const *msg_sect, u32 num_sect,
224 			      int err);
225 struct sk_buff *tipc_port_get_ports(void);
226 void tipc_port_recv_proto_msg(struct sk_buff *buf);
227 void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp);
228 void tipc_port_reinit(void);
229 
230 /**
231  * tipc_port_lock - lock port instance referred to and return its pointer
232  */
233 
tipc_port_lock(u32 ref)234 static inline struct tipc_port *tipc_port_lock(u32 ref)
235 {
236 	return (struct tipc_port *)tipc_ref_lock(ref);
237 }
238 
239 /**
240  * tipc_port_unlock - unlock a port instance
241  *
242  * Can use pointer instead of tipc_ref_unlock() since port is already locked.
243  */
244 
tipc_port_unlock(struct tipc_port * p_ptr)245 static inline void tipc_port_unlock(struct tipc_port *p_ptr)
246 {
247 	spin_unlock_bh(p_ptr->lock);
248 }
249 
tipc_port_deref(u32 ref)250 static inline struct tipc_port *tipc_port_deref(u32 ref)
251 {
252 	return (struct tipc_port *)tipc_ref_deref(ref);
253 }
254 
tipc_peer_port(struct tipc_port * p_ptr)255 static inline u32 tipc_peer_port(struct tipc_port *p_ptr)
256 {
257 	return msg_destport(&p_ptr->phdr);
258 }
259 
tipc_peer_node(struct tipc_port * p_ptr)260 static inline u32 tipc_peer_node(struct tipc_port *p_ptr)
261 {
262 	return msg_destnode(&p_ptr->phdr);
263 }
264 
tipc_port_congested(struct tipc_port * p_ptr)265 static inline int tipc_port_congested(struct tipc_port *p_ptr)
266 {
267 	return (p_ptr->sent - p_ptr->acked) >= (TIPC_FLOW_CONTROL_WIN * 2);
268 }
269 
270 /**
271  * tipc_port_recv_msg - receive message from lower layer and deliver to port user
272  */
273 
tipc_port_recv_msg(struct sk_buff * buf)274 static inline int tipc_port_recv_msg(struct sk_buff *buf)
275 {
276 	struct tipc_port *p_ptr;
277 	struct tipc_msg *msg = buf_msg(buf);
278 	u32 destport = msg_destport(msg);
279 	u32 dsz = msg_data_sz(msg);
280 	u32 err;
281 
282 	/* forward unresolved named message */
283 	if (unlikely(!destport)) {
284 		tipc_net_route_msg(buf);
285 		return dsz;
286 	}
287 
288 	/* validate destination & pass to port, otherwise reject message */
289 	p_ptr = tipc_port_lock(destport);
290 	if (likely(p_ptr)) {
291 		if (likely(p_ptr->connected)) {
292 			if ((unlikely(msg_origport(msg) != tipc_peer_port(p_ptr))) ||
293 			    (unlikely(msg_orignode(msg) != tipc_peer_node(p_ptr))) ||
294 			    (unlikely(!msg_connected(msg)))) {
295 				err = TIPC_ERR_NO_PORT;
296 				tipc_port_unlock(p_ptr);
297 				goto reject;
298 			}
299 		}
300 		err = p_ptr->dispatcher(p_ptr, buf);
301 		tipc_port_unlock(p_ptr);
302 		if (likely(!err))
303 			return dsz;
304 	} else {
305 		err = TIPC_ERR_NO_PORT;
306 	}
307 reject:
308 	return tipc_reject_msg(buf, err);
309 }
310 
311 #endif
312