1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * tcp.h
5  *
6  * Function prototypes
7  *
8  * Copyright (C) 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  *
25  */
26 
27 #ifndef R2CLUSTER_TCP_H
28 #define R2CLUSTER_TCP_H
29 
30 #include <linux/socket.h>
31 #ifdef __KERNEL__
32 #include <net/sock.h>
33 #include <linux/tcp.h>
34 #else
35 #include <sys/socket.h>
36 #endif
37 #include <linux/inet.h>
38 #include <linux/in.h>
39 
40 struct r2net_msg {
41 	__be16 magic;
42 	__be16 data_len;
43 	__be16 msg_type;
44 	__be16 pad1;
45 	__be32 sys_status;
46 	__be32 status;
47 	__be32 key;
48 	__be32 msg_num;
49 	__u8  buf[0];
50 };
51 
52 typedef int (r2net_msg_handler_func)(struct r2net_msg *msg, u32 len, void *data,
53 				     void **ret_data);
54 typedef void (r2net_post_msg_handler_func)(int status, void *data,
55 					   void *ret_data);
56 
57 #define R2NET_MAX_PAYLOAD_BYTES  (4096 - sizeof(struct r2net_msg))
58 
59 /* same as hb delay, we're waiting for another node to recognize our hb */
60 #define R2NET_RECONNECT_DELAY_MS_DEFAULT	2000
61 
62 #define R2NET_KEEPALIVE_DELAY_MS_DEFAULT	2000
63 #define R2NET_IDLE_TIMEOUT_MS_DEFAULT		30000
64 
65 
66 /* TODO: figure this out.... */
r2net_link_down(int err,struct socket * sock)67 static inline int r2net_link_down(int err, struct socket *sock)
68 {
69 	if (sock) {
70 		if (sock->sk->sk_state != TCP_ESTABLISHED &&
71 			sock->sk->sk_state != TCP_CLOSE_WAIT)
72 			return 1;
73 	}
74 
75 	if (err >= 0)
76 		return 0;
77 	switch (err) {
78 
79 	/* ????????????????????????? */
80 	case -ERESTARTSYS:
81 	case -EBADF:
82 	/* When the server has died, an ICMP port unreachable
83 	 * message prompts ECONNREFUSED. */
84 	case -ECONNREFUSED:
85 	case -ENOTCONN:
86 	case -ECONNRESET:
87 	case -EPIPE:
88 		return 1;
89 
90 	}
91 	return 0;
92 }
93 
94 enum {
95 	R2NET_DRIVER_UNINITED,
96 	R2NET_DRIVER_READY,
97 };
98 
99 int r2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
100 		       u8 target_node, int *status);
101 int r2net_send_message_vec(u32 msg_type, u32 key, struct kvec *vec,
102 			   size_t veclen, u8 target_node, int *status);
103 
104 int r2net_register_handler(u32 msg_type, u32 key, u32 max_len,
105 			   r2net_msg_handler_func *func, void *data,
106 			   r2net_post_msg_handler_func *post_func,
107 			   struct list_head *unreg_list);
108 void r2net_unregister_handler_list(struct list_head *list);
109 
110 void r2net_fill_node_map(unsigned long *map, unsigned bytes);
111 
112 void r2net_force_data_magic(struct r2net_msg *, u16, u32);
113 void r2net_hb_node_up_manual(int);
114 struct r2net_node *r2net_nn_from_num(u8);
115 
116 struct r2nm_node;
117 int r2net_register_hb_callbacks(void);
118 void r2net_unregister_hb_callbacks(void);
119 int r2net_start_listening(struct r2nm_node *node);
120 void r2net_stop_listening(struct r2nm_node *node);
121 void r2net_disconnect_node(struct r2nm_node *node);
122 int r2net_num_connected_peers(void);
123 
124 int r2net_init(void);
125 void r2net_exit(void);
126 
127 struct r2net_send_tracking;
128 struct r2net_sock_container;
129 
130 #if 0
131 int r2net_debugfs_init(void);
132 void r2net_debugfs_exit(void);
133 void r2net_debug_add_nst(struct r2net_send_tracking *nst);
134 void r2net_debug_del_nst(struct r2net_send_tracking *nst);
135 void r2net_debug_add_sc(struct r2net_sock_container *sc);
136 void r2net_debug_del_sc(struct r2net_sock_container *sc);
137 #else
r2net_debugfs_init(void)138 static inline int r2net_debugfs_init(void)
139 {
140 	return 0;
141 }
r2net_debugfs_exit(void)142 static inline void r2net_debugfs_exit(void)
143 {
144 }
r2net_debug_add_nst(struct r2net_send_tracking * nst)145 static inline void r2net_debug_add_nst(struct r2net_send_tracking *nst)
146 {
147 }
r2net_debug_del_nst(struct r2net_send_tracking * nst)148 static inline void r2net_debug_del_nst(struct r2net_send_tracking *nst)
149 {
150 }
r2net_debug_add_sc(struct r2net_sock_container * sc)151 static inline void r2net_debug_add_sc(struct r2net_sock_container *sc)
152 {
153 }
r2net_debug_del_sc(struct r2net_sock_container * sc)154 static inline void r2net_debug_del_sc(struct r2net_sock_container *sc)
155 {
156 }
157 #endif	/* CONFIG_DEBUG_FS */
158 
159 #endif /* R2CLUSTER_TCP_H */
160