// SPDX-License-Identifier: GPL-2.0-or-later /* * * Copyright (C) 2004 Oracle. All rights reserved. * * ---- * * Callers for this were originally written against a very simple synchronous * API. This implementation reflects those simple callers. Some day I'm sure * we'll need to move to a more robust posting/callback mechanism. * * Transmit calls pass in kernel virtual addresses and block copying this into * the socket's tx buffers via a usual blocking sendmsg. They'll block waiting * for a failed socket to timeout. TX callers can also pass in a pointer to an * 'int' which gets filled with an errno off the wire in response to the * message they send. * * Handlers for unsolicited messages are registered. Each socket has a page * that incoming data is copied into. First the header, then the data. * Handlers are called from only one thread with a reference to this per-socket * page. This page is destroyed after the handler call, so it can't be * referenced beyond the call. Handlers may block but are discouraged from * doing so. * * Any framing errors (bad magic, large payload lengths) close a connection. * * Our sock_container holds the state we associate with a socket. It's current * framing state is held there as well as the refcounting we do around when it * is safe to tear down the socket. The socket is only finally torn down from * the container when the container loses all of its references -- so as long * as you hold a ref on the container you can trust that the socket is valid * for use with kernel socket APIs. * * Connections are initiated between a pair of nodes when the node with the * higher node number gets a heartbeat callback which indicates that the lower * numbered node has started heartbeating. The lower numbered node is passive * and only accepts the connection if the higher numbered node is heartbeating.
*/
/* * In the following two log macros, the whitespace after the ',' just * before ##args is intentional. Otherwise, gcc 2.95 will eat the * previous token if args expands to nothing.
*/ #define msglog(hdr, fmt, args...) do { \
typeof(hdr) __hdr = (hdr); \
mlog(ML_MSG, "[mag %u len %u typ %u stat %d sys_stat %d " \ "key %08x num %u] " fmt, \
be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len), \
be16_to_cpu(__hdr->msg_type), be32_to_cpu(__hdr->status), \
be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->key), \
be32_to_cpu(__hdr->msg_num) , ##args); \
} while (0)
/* * listen work is only queued by the listening socket callbacks on the * o2net_wq. teardown detaches the callbacks before destroying the workqueue. * quorum work is queued as sock containers are shutdown.. stop_listening * tears down all the node's sock containers, preventing future shutdowns * and queued quorum work, before canceling delayed quorum work and * destroying the work queue.
*/ staticstruct workqueue_struct *o2net_wq; staticstruct work_struct o2net_listen_work;
int o2net_num_connected_peers(void)
{ return atomic_read(&o2net_connected_peers);
}
staticvoid o2net_set_nn_state(struct o2net_node *nn, struct o2net_sock_container *sc, unsigned valid, int err)
{ int was_valid = nn->nn_sc_valid; int was_err = nn->nn_persistent_error; struct o2net_sock_container *old_sc = nn->nn_sc;
assert_spin_locked(&nn->nn_lock);
if (old_sc && !sc)
atomic_dec(&o2net_connected_peers); elseif (!old_sc && sc)
atomic_inc(&o2net_connected_peers);
/* the node num comparison and single connect/accept path should stop
* an non-null sc from being overwritten with another */
BUG_ON(sc && nn->nn_sc && nn->nn_sc != sc);
mlog_bug_on_msg(err && valid, "err %d valid %u\n", err, valid);
mlog_bug_on_msg(valid && !sc, "valid %u sc %p\n", valid, sc);
if (was_valid && !valid) { if (old_sc)
printk(KERN_NOTICE "o2net: No longer connected to "
SC_NODEF_FMT "\n", SC_NODEF_ARGS(old_sc));
o2net_complete_nodes_nsw(nn);
}
/* trigger the connecting worker func as long as we're not valid, * it will back off if it shouldn't connect. This can be called * from node config teardown and so needs to be careful about
* the work queue actually being up. */ if (!valid && o2net_wq) { unsignedlong delay; /* delay if we're within a RECONNECT_DELAY of the
* last attempt */
delay = (nn->nn_last_connect_attempt +
msecs_to_jiffies(o2net_reconnect_delay()))
- jiffies; if (delay > msecs_to_jiffies(o2net_reconnect_delay()))
delay = 0;
mlog(ML_CONN, "queueing conn attempt in %lu jiffies\n", delay);
queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay);
/* * Delay the expired work after idle timeout. * * We might have lots of failed connection attempts that run * through here but we only cancel the connect_expired work when * a connection attempt succeeds. So only the first enqueue of * the connect_expired work will do anything. The rest will see * that it's already queued and do nothing.
*/
delay += msecs_to_jiffies(o2net_idle_timeout());
queue_delayed_work(o2net_wq, &nn->nn_connect_expired, delay);
}
/* keep track of the nn's sc ref for the caller */ if ((old_sc == NULL) && sc)
sc_get(sc); if (old_sc && (old_sc != sc)) {
o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work);
sc_put(old_sc);
}
}
switch(sk->sk_state) { /* ignore connecting sockets as they make progress */ case TCP_SYN_SENT: case TCP_SYN_RECV: break; case TCP_ESTABLISHED:
o2net_sc_queue_work(sc, &sc->sc_connect_work); break; default:
printk(KERN_INFO "o2net: Connection to " SC_NODEF_FMT " shutdown, state %d\n",
SC_NODEF_ARGS(sc), sk->sk_state);
o2net_sc_queue_work(sc, &sc->sc_shutdown_work); break;
}
out:
read_unlock_bh(&sk->sk_callback_lock);
state_change(sk);
}
/* * we register callbacks so we can queue work on events before calling * the original callbacks. our callbacks our careful to test user_data * to discover when they've reaced with o2net_unregister_callbacks().
*/ staticvoid o2net_register_callbacks(struct sock *sk, struct o2net_sock_container *sc)
{
write_lock_bh(&sk->sk_callback_lock);
/* accepted sockets inherit the old listen socket data ready */ if (sk->sk_data_ready == o2net_listen_data_ready) {
sk->sk_data_ready = sk->sk_user_data;
sk->sk_user_data = NULL;
}
staticint o2net_unregister_callbacks(struct sock *sk, struct o2net_sock_container *sc)
{ int ret = 0;
write_lock_bh(&sk->sk_callback_lock); if (sk->sk_user_data == sc) {
ret = 1;
sk->sk_user_data = NULL;
sk->sk_data_ready = sc->sc_data_ready;
sk->sk_state_change = sc->sc_state_change;
}
write_unlock_bh(&sk->sk_callback_lock);
return ret;
}
/* * this is a little helper that is called by callers who have seen a problem * with an sc and want to detach it from the nn if someone already hasn't beat * them to it. if an error is given then the shutdown will be persistent * and pending transmits will be canceled.
*/ staticvoid o2net_ensure_shutdown(struct o2net_node *nn, struct o2net_sock_container *sc, int err)
{
spin_lock(&nn->nn_lock); if (nn->nn_sc == sc)
o2net_set_nn_state(nn, NULL, 0, err);
spin_unlock(&nn->nn_lock);
}
/* * This work queue function performs the blocking parts of socket shutdown. A * few paths lead here. set_nn_state will trigger this callback if it sees an * sc detached from the nn. state_change will also trigger this callback * directly when it sees errors. In that case we need to call set_nn_state * ourselves as state_change couldn't get the nn_lock and call set_nn_state * itself.
*/ staticvoid o2net_shutdown_sc(struct work_struct *work)
{ struct o2net_sock_container *sc =
container_of(work, struct o2net_sock_container,
sc_shutdown_work); struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
sclog(sc, "shutting down\n");
/* drop the callbacks ref and call shutdown only once */ if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) { /* we shouldn't flush as we're in the thread, the
* races with pending sc work structs are harmless */
timer_delete_sync(&sc->sc_idle_timeout);
o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
sc_put(sc);
kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR);
}
/* not fatal so failed connects before the other guy has our
* heartbeat can be retried */
o2net_ensure_shutdown(nn, sc, 0);
sc_put(sc);
}
/* max_len is protection for the handler func. incoming messages won't
* be given to the handler if their payload is longer than the max. */ int o2net_register_handler(u32 msg_type, u32 key, u32 max_len,
o2net_msg_handler_func *func, void *data,
o2net_post_msg_handler_func *post_func, struct list_head *unreg_list)
{ struct o2net_msg_handler *nmh = NULL; struct rb_node **p, *parent; int ret = 0;
if (max_len > O2NET_MAX_PAYLOAD_BYTES) {
mlog(0, "max_len for message handler out of range: %u\n",
max_len);
ret = -EINVAL; goto out;
}
if (!msg_type) {
mlog(0, "no message type provided: %u, %p\n", msg_type, func);
ret = -EINVAL; goto out;
} if (!func) {
mlog(0, "no message handler provided: %u, %p\n",
msg_type, func);
ret = -EINVAL; goto out;
}
nmh = kzalloc(sizeof(struct o2net_msg_handler), GFP_NOFS); if (nmh == NULL) {
ret = -ENOMEM; goto out;
}
nmh->nh_func = func;
nmh->nh_func_data = data;
nmh->nh_post_func = post_func;
nmh->nh_msg_type = msg_type;
nmh->nh_max_len = max_len;
nmh->nh_key = key; /* the tree and list get this ref.. they're both removed in
* unregister when this ref is dropped */
kref_init(&nmh->nh_kref);
INIT_LIST_HEAD(&nmh->nh_unregister_item);
write_lock(&o2net_handler_lock); if (o2net_handler_tree_lookup(msg_type, key, &p, &parent))
ret = -EEXIST; else {
rb_link_node(&nmh->nh_node, parent, p);
rb_insert_color(&nmh->nh_node, &o2net_handler_tree);
list_add_tail(&nmh->nh_unregister_item, unreg_list);
mlog(ML_TCP, "registered handler func %p type %u key %08x\n",
func, msg_type, key); /* we've had some trouble with handlers seemingly vanishing. */
mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p,
&parent) == NULL, "couldn't find handler we *just* registered " "for type %u key %08x\n", msg_type, key);
}
write_unlock(&o2net_handler_lock);
ret = kernel_sendmsg(sock, &msg, vec, veclen, total); if (likely(ret == total)) return 0;
mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret, total); if (ret >= 0)
ret = -EPIPE; /* should be smarter, I bet */
out:
mlog(0, "returning error: %d\n", ret); return ret;
}
/* Get a map of all nodes to which this node is currently connected to */ void o2net_fill_node_map(unsignedlong *map, unsignedint bits)
{ struct o2net_sock_container *sc; int node, ret;
bitmap_zero(map, bits); for (node = 0; node < O2NM_MAX_NODES; ++node) { if (!o2net_tx_can_proceed(o2net_nn_from_num(node), &sc, &ret)) continue; if (!ret) {
set_bit(node, map);
sc_put(sc);
}
}
}
EXPORT_SYMBOL_GPL(o2net_fill_node_map);
/* finally, convert the message header to network byte-order
* and send */
mutex_lock(&sc->sc_send_lock);
ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen, sizeof(struct o2net_msg) + caller_bytes);
mutex_unlock(&sc->sc_send_lock);
msglog(msg, "sending returned %d\n", ret); if (ret < 0) {
mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret); goto out;
}
/* wait on other node's handler */
o2net_set_nst_status_time(&nst);
wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));
o2net_update_send_stats(&nst, sc);
/* Note that we avoid overwriting the callers status return * variable if a system error was reported on the other
* side. Callers beware. */
ret = o2net_sys_err_to_errno(nsw.ns_sys_status); if (status && !ret)
*status = nsw.ns_status;
mlog(0, "woken, returning system status %d, user status %d\n",
ret, nsw.ns_status);
out:
o2net_debug_del_nst(&nst); /* must be before dropping sc and node */ if (sc)
sc_put(sc);
kfree(vec);
kfree(msg);
o2net_complete_nsw(nn, &nsw, 0, 0, 0); return ret;
}
EXPORT_SYMBOL_GPL(o2net_send_message_vec);
/* leave other fields intact from the incoming message, msg_num
* in particular */
hdr->sys_status = cpu_to_be32(syserr);
hdr->status = cpu_to_be32(err);
hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC); // twiddle the magic
hdr->data_len = 0;
msglog(hdr, "about to send status magic %d\n", err); /* hdr has been in host byteorder this whole time */ return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
}
/* this returns -errno if the header was unknown or too large, etc.
* after this is called the buffer us reused for the next message */ staticint o2net_process_message(struct o2net_sock_container *sc, struct o2net_msg *hdr)
{ struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); int ret = 0, handler_status; enum o2net_system_error syserr; struct o2net_msg_handler *nmh = NULL; void *ret_data = NULL;
msglog(hdr, "processing message\n");
o2net_sc_postpone_idle(sc);
switch(be16_to_cpu(hdr->magic)) { case O2NET_MSG_STATUS_MAGIC: /* special type for returning message status */
o2net_complete_nsw(nn, NULL,
be32_to_cpu(hdr->msg_num),
be32_to_cpu(hdr->sys_status),
be32_to_cpu(hdr->status)); goto out; case O2NET_MSG_KEEP_REQ_MAGIC:
o2net_sendpage(sc, o2net_keep_resp, sizeof(*o2net_keep_resp)); goto out; case O2NET_MSG_KEEP_RESP_MAGIC: goto out; case O2NET_MSG_MAGIC: break; default:
msglog(hdr, "bad magic\n");
ret = -EINVAL; goto out;
}
/* find a handler for it */
handler_status = 0;
nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
be32_to_cpu(hdr->key)); if (!nmh) {
mlog(ML_TCP, "couldn't find handler for type %u key %08x\n",
be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
syserr = O2NET_ERR_NO_HNDLR; goto out_respond;
}
syserr = O2NET_ERR_NONE;
if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
syserr = O2NET_ERR_OVERFLOW;
out_respond: /* this destroys the hdr, so don't use it after this */
mutex_lock(&sc->sc_send_lock);
ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
handler_status);
mutex_unlock(&sc->sc_send_lock);
hdr = NULL;
mlog(0, "sending handler status %d, syserr %d returned %d\n",
handler_status, syserr, ret);
if (nmh) {
BUG_ON(ret_data != NULL && nmh->nh_post_func == NULL); if (nmh->nh_post_func)
(nmh->nh_post_func)(handler_status, nmh->nh_func_data,
ret_data);
}
out: if (nmh)
o2net_handler_put(nmh); return ret;
}
if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " Advertised net " "protocol version %llu but %llu is required. " "Disconnecting.\n", SC_NODEF_ARGS(sc),
(unsignedlonglong)be64_to_cpu(hand->protocol_version),
O2NET_PROTOCOL_VERSION);
/* don't bother reconnecting if its the wrong version. */
o2net_ensure_shutdown(nn, sc, -ENOTCONN); return -1;
}
/* * Ensure timeouts are consistent with other nodes, otherwise * we can end up with one node thinking that the other must be down, * but isn't. This can ultimately cause corruption.
*/ if (be32_to_cpu(hand->o2net_idle_timeout_ms) !=
o2net_idle_timeout()) {
printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a network " "idle timeout of %u ms, but we use %u ms locally. " "Disconnecting.\n", SC_NODEF_ARGS(sc),
be32_to_cpu(hand->o2net_idle_timeout_ms),
o2net_idle_timeout());
o2net_ensure_shutdown(nn, sc, -ENOTCONN); return -1;
}
if (be32_to_cpu(hand->o2net_keepalive_delay_ms) !=
o2net_keepalive_delay()) {
printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a keepalive " "delay of %u ms, but we use %u ms locally. " "Disconnecting.\n", SC_NODEF_ARGS(sc),
be32_to_cpu(hand->o2net_keepalive_delay_ms),
o2net_keepalive_delay());
o2net_ensure_shutdown(nn, sc, -ENOTCONN); return -1;
}
if (be32_to_cpu(hand->o2hb_heartbeat_timeout_ms) !=
O2HB_MAX_WRITE_TIMEOUT_MS) {
printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a heartbeat " "timeout of %u ms, but we use %u ms locally. " "Disconnecting.\n", SC_NODEF_ARGS(sc),
be32_to_cpu(hand->o2hb_heartbeat_timeout_ms),
O2HB_MAX_WRITE_TIMEOUT_MS);
o2net_ensure_shutdown(nn, sc, -ENOTCONN); return -1;
}
sc->sc_handshake_ok = 1;
spin_lock(&nn->nn_lock); /* set valid and queue the idle timers only if it hasn't been
* shut down already */ if (nn->nn_sc == sc) {
o2net_sc_reset_idle_timer(sc);
atomic_set(&nn->nn_timeout, 0);
o2net_set_nn_state(nn, sc, 1, 0);
}
spin_unlock(&nn->nn_lock);
/* shift everything up as though it wasn't there */
sc->sc_page_off -= sizeof(struct o2net_handshake); if (sc->sc_page_off)
memmove(hand, hand + 1, sc->sc_page_off);
return 0;
}
/* this demuxes the queued rx bytes into header or payload bits and calls * handlers as each full message is read off the socket. it returns -error,
* == 0 eof, or > 0 for progress made.*/ staticint o2net_advance_rx(struct o2net_sock_container *sc)
{ struct o2net_msg *hdr; int ret = 0; void *data;
size_t datalen;
if (unlikely(sc->sc_handshake_ok == 0)) { if(sc->sc_page_off < sizeof(struct o2net_handshake)) {
data = page_address(sc->sc_page) + sc->sc_page_off;
datalen = sizeof(struct o2net_handshake) - sc->sc_page_off;
ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen); if (ret > 0)
sc->sc_page_off += ret;
}
if (sc->sc_page_off == sizeof(struct o2net_handshake)) {
o2net_check_handshake(sc); if (unlikely(sc->sc_handshake_ok == 0))
ret = -EPROTO;
} goto out;
}
/* do we need more header? */ if (sc->sc_page_off < sizeof(struct o2net_msg)) {
data = page_address(sc->sc_page) + sc->sc_page_off;
datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen); if (ret > 0) {
sc->sc_page_off += ret; /* only swab incoming here.. we can * only get here once as we cross from
* being under to over */ if (sc->sc_page_off == sizeof(struct o2net_msg)) {
hdr = page_address(sc->sc_page); if (be16_to_cpu(hdr->data_len) >
O2NET_MAX_PAYLOAD_BYTES)
ret = -EOVERFLOW;
}
} if (ret <= 0) goto out;
}
if (sc->sc_page_off < sizeof(struct o2net_msg)) { /* oof, still don't have a header */ goto out;
}
/* this was swabbed above when we first read it */
hdr = page_address(sc->sc_page);
/* do we need more payload? */ if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) { /* need more payload */
data = page_address(sc->sc_page) + sc->sc_page_off;
datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
sc->sc_page_off;
ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen); if (ret > 0)
sc->sc_page_off += ret; if (ret <= 0) goto out;
}
if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) { /* we can only get here once, the first time we read * the payload.. so set ret to progress if the handler
* works out. after calling this the message is toast */
ret = o2net_process_message(sc, hdr); if (ret == 0)
ret = 1;
sc->sc_page_off = 0;
}
/* this work func is triggered by data ready. it reads until it can read no * more. it interprets 0, eof, as fatal. if data_ready hits while we're doing
* our work the work struct will be marked and we'll be called again. */ staticvoid o2net_rx_until_empty(struct work_struct *work)
{ struct o2net_sock_container *sc =
container_of(work, struct o2net_sock_container, sc_rx_work); int ret;
do {
ret = o2net_advance_rx(sc);
} while (ret > 0);
if (ret <= 0 && ret != -EAGAIN) { struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
sclog(sc, "saw error %d, closing\n", ret); /* not permanent so read failed handshake can retry */
o2net_ensure_shutdown(nn, sc, 0);
}
/* called when a connect completes and after a sock is accepted. the
* rx path will see the response and mark the sc valid */ staticvoid o2net_sc_connect_completed(struct work_struct *work)
{ struct o2net_sock_container *sc =
container_of(work, struct o2net_sock_container,
sc_connect_work);
mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n",
(unsignedlonglong)O2NET_PROTOCOL_VERSION,
(unsignedlonglong)be64_to_cpu(o2net_hand->connector_id));
/* this is called as a work_struct func. */ staticvoid o2net_sc_send_keep_req(struct work_struct *work)
{ struct o2net_sock_container *sc =
container_of(work, struct o2net_sock_container,
sc_keepalive_work.work);
/* socket shutdown does a timer_delete_sync against this as it tears down. * we can't start this timer until we've got to the point in sc buildup
* where shutdown is going to be involved */ staticvoid o2net_idle_timer(struct timer_list *t)
{ struct o2net_sock_container *sc = timer_container_of(sc, t,
sc_idle_timeout); struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); #ifdef CONFIG_DEBUG_FS unsignedlong msecs = ktime_to_ms(ktime_get()) -
ktime_to_ms(sc->sc_tv_timer); #else unsignedlong msecs = o2net_idle_timeout(); #endif
printk(KERN_NOTICE "o2net: Connection to " SC_NODEF_FMT " has been " "idle for %lu.%lu secs.\n",
SC_NODEF_ARGS(sc), msecs / 1000, msecs % 1000);
/* idle timerout happen, don't shutdown the connection, but * make fence decision. Maybe the connection can recover before * the decision is made.
*/
atomic_set(&nn->nn_timeout, 1);
o2quo_conn_err(o2net_num_from_nn(nn));
queue_delayed_work(o2net_wq, &nn->nn_still_up,
msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
/* clear fence decision since the connection recover from timeout*/ if (atomic_read(&nn->nn_timeout)) {
o2quo_conn_up(o2net_num_from_nn(nn));
cancel_delayed_work(&nn->nn_still_up);
atomic_set(&nn->nn_timeout, 0);
}
/* Only push out an existing timer */ if (timer_pending(&sc->sc_idle_timeout))
o2net_sc_reset_idle_timer(sc);
}
/* this work func is kicked whenever a path sets the nn state which doesn't * have valid set. This includes seeing hb come up, losing a connection, * having a connect attempt fail, etc. This centralizes the logic which decides * if a connect attempt should be made or if we should give up and all future
* transmit attempts should fail */ staticvoid o2net_start_connect(struct work_struct *work)
{ struct o2net_node *nn =
container_of(work, struct o2net_node, nn_connect_work.work); struct o2net_sock_container *sc = NULL; struct o2nm_node *node = NULL, *mynode = NULL; struct socket *sock = NULL; struct sockaddr_in myaddr = {0, }, remoteaddr = {0, }; int ret = 0, stop; unsignedint timeout; unsignedint nofs_flag;
/* * sock_create allocates the sock with GFP_KERNEL. We must * prevent the filesystem from being reentered by memory reclaim.
*/
nofs_flag = memalloc_nofs_save(); /* if we're greater we initiate tx, otherwise we accept */ if (o2nm_this_node() <= o2net_num_from_nn(nn)) goto out;
/* watch for racing with tearing a node down */
node = o2nm_get_node_by_num(o2net_num_from_nn(nn)); if (node == NULL) goto out;
mynode = o2nm_get_node_by_num(o2nm_this_node()); if (mynode == NULL) goto out;
spin_lock(&nn->nn_lock); /* * see if we already have one pending or have given up. * For nn_timeout, it is set when we close the connection * because of the idle time out. So it means that we have * at least connected to that node successfully once, * now try to connect to it again.
*/
timeout = atomic_read(&nn->nn_timeout);
stop = (nn->nn_sc ||
(nn->nn_persistent_error &&
(nn->nn_persistent_error != -ENOTCONN || timeout == 0)));
spin_unlock(&nn->nn_lock); if (stop) goto out;
nn->nn_last_connect_attempt = jiffies;
sc = sc_alloc(node); if (sc == NULL) {
mlog(0, "couldn't allocate sc\n");
ret = -ENOMEM; goto out;
}
ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); if (ret < 0) {
mlog(0, "can't create socket: %d\n", ret); goto out;
}
sc->sc_sock = sock; /* freed by sc_kref_release */
ret = sc->sc_sock->ops->connect(sc->sc_sock,
(struct sockaddr *)&remoteaddr, sizeof(remoteaddr),
O_NONBLOCK); if (ret == -EINPROGRESS)
ret = 0;
out: if (ret && sc) {
printk(KERN_NOTICE "o2net: Connect attempt to " SC_NODEF_FMT " failed with errno %d\n", SC_NODEF_ARGS(sc), ret); /* 0 err so that another will be queued and attempted
* from set_nn_state */
o2net_ensure_shutdown(nn, sc, 0);
} if (sc)
sc_put(sc); if (node)
o2nm_node_put(node); if (mynode)
o2nm_node_put(mynode);
if (node_num != o2nm_this_node()) { /* believe it or not, accept and node heartbeating testing * can succeed for this node before we got here.. so * only use set_nn_state to clear the persistent error
* if that hasn't already happened */
spin_lock(&nn->nn_lock);
atomic_set(&nn->nn_timeout, 0); if (nn->nn_persistent_error)
o2net_set_nn_state(nn, NULL, 0, 0);
spin_unlock(&nn->nn_lock);
}
}
/* * sock_create_lite allocates the sock with GFP_KERNEL. We must * prevent the filesystem from being reentered by memory reclaim.
*/
nofs_flag = memalloc_nofs_save();
BUG_ON(sock == NULL);
*more = 0;
ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
sock->sk->sk_protocol, &new_sock); if (ret) goto out;
new_sock->type = sock->type;
new_sock->ops = sock->ops;
ret = sock->ops->accept(sock, new_sock, &arg); if (ret < 0) goto out;
ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin, 1); if (ret < 0) goto out;
node = o2nm_get_node_by_ip(sin.sin_addr.s_addr); if (node == NULL) {
printk(KERN_NOTICE "o2net: Attempt to connect from unknown " "node at %pI4:%d\n", &sin.sin_addr.s_addr,
ntohs(sin.sin_port));
ret = -EINVAL; goto out;
}
if (o2nm_this_node() >= node->nd_num) {
local_node = o2nm_get_node_by_num(o2nm_this_node()); if (local_node)
printk(KERN_NOTICE "o2net: Unexpected connect attempt " "seen at node '%s' (%u, %pI4:%d) from " "node '%s' (%u, %pI4:%d)\n",
local_node->nd_name, local_node->nd_num,
&(local_node->nd_ipv4_address),
ntohs(local_node->nd_ipv4_port),
node->nd_name,
node->nd_num, &sin.sin_addr.s_addr,
ntohs(sin.sin_port));
ret = -EINVAL; goto out;
}
/* this happens all the time when the other node sees our heartbeat
* and tries to connect before we see their heartbeat */ if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) {
mlog(ML_CONN, "attempt to connect from node '%s' at " "%pI4:%d but it isn't heartbeating\n",
node->nd_name, &sin.sin_addr.s_addr,
ntohs(sin.sin_port));
ret = -EINVAL; goto out;
}
nn = o2net_nn_from_num(node->nd_num);
spin_lock(&nn->nn_lock); if (nn->nn_sc)
ret = -EBUSY; else
ret = 0;
spin_unlock(&nn->nn_lock); if (ret) {
printk(KERN_NOTICE "o2net: Attempt to connect from node '%s' " "at %pI4:%d but it already has an open connection\n",
node->nd_name, &sin.sin_addr.s_addr,
ntohs(sin.sin_port)); goto out;
}
sc = sc_alloc(node); if (sc == NULL) {
ret = -ENOMEM; goto out;
}
/* * It is critical to note that due to interrupt moderation * at the network driver level, we can't assume to get a * softIRQ for every single conn since tcp SYN packets * can arrive back-to-back, and therefore many pending * accepts may result in just 1 softIRQ. If we terminate * the o2net_accept_one() loop upon seeing an err, what happens * to the rest of the conns in the queue? If no new SYN * arrives for hours, no softIRQ will be delivered, * and the connections will just sit in the queue.
*/
for (;;) {
o2net_accept_one(sock, &more); if (!more) break;
cond_resched();
}
}
/* This callback may called twice when a new connection * is being established as a child socket inherits everything * from a parent LISTEN socket, including the data_ready cb of * the parent. This leads to a hazard. In o2net_accept_one() * we are still initializing the child socket but have not * changed the inherited data_ready callback yet when * data starts arriving. * We avoid this hazard by checking the state. * For the listening socket, the state will be TCP_LISTEN; for the new * socket, will be TCP_ESTABLISHED. Also, in this case, * sk->sk_user_data is not a valid function pointer.
*/
sock->sk->sk_reuse = SK_CAN_REUSE;
ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin)); if (ret < 0) {
printk(KERN_ERR "o2net: Error %d while binding socket at " "%pI4:%u\n", ret, &addr, ntohs(port)); goto out;
}
ret = sock->ops->listen(sock, 64); if (ret < 0)
printk(KERN_ERR "o2net: Error %d while listening on %pI4:%u\n",
ret, &addr, ntohs(port));
out: if (ret) {
o2net_listen_sock = NULL; if (sock)
sock_release(sock);
} return ret;
}
/* * called from node manager when we should bring up our network listening * socket. node manager handles all the serialization to only call this * once and to match it with o2net_stop_listening(). note, * o2nm_this_node() doesn't work yet as we're being called while it * is being set up.
*/ int o2net_start_listening(struct o2nm_node *node)
{ int ret = 0;
ret = o2net_open_listening_sock(node->nd_ipv4_address,
node->nd_ipv4_port); if (ret) {
destroy_workqueue(o2net_wq);
o2net_wq = NULL;
} else
o2quo_conn_up(node->nd_num);
return ret;
}
/* again, o2nm_this_node() doesn't work here as we're involved in
* tearing it down */ void o2net_stop_listening(struct o2nm_node *node)
{ struct socket *sock = o2net_listen_sock;
size_t i;
/* stop the listening socket from generating work */
write_lock_bh(&sock->sk->sk_callback_lock);
sock->sk->sk_data_ready = sock->sk->sk_user_data;
sock->sk->sk_user_data = NULL;
write_unlock_bh(&sock->sk->sk_callback_lock);
for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) { struct o2nm_node *node = o2nm_get_node_by_num(i); if (node) {
o2net_disconnect_node(node);
o2nm_node_put(node);
}
}
/* finish all work and tear down the work queue */
mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
destroy_workqueue(o2net_wq);
o2net_wq = NULL;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.