📄 lowcomms.c
字号:
queue_work(recv_workqueue, &new_con->rwork); } break; case SCTP_COMM_LOST: case SCTP_SHUTDOWN_COMP: { con = assoc2con(sn->sn_assoc_change.sac_assoc_id); if (con) { con->sctp_assoc = 0; } } break; /* We don't know which INIT failed, so clear the PENDING flags * on them all. if assoc_id is zero then it will then try * again */ case SCTP_CANT_STR_ASSOC: { log_print("Can't start SCTP association - retrying"); sctp_init_failed(); } break; default: log_print("unexpected SCTP assoc change id=%d state=%d", (int)sn->sn_assoc_change.sac_assoc_id, sn->sn_assoc_change.sac_state); } }}/* Data received from remote end */static int receive_from_sock(struct connection *con){ int ret = 0; struct msghdr msg = {}; struct kvec iov[2]; unsigned len; int r; int call_again_soon = 0; int nvec; char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; mutex_lock(&con->sock_mutex); if (con->sock == NULL) { ret = -EAGAIN; goto out_close; } if (con->rx_page == NULL) { /* * This doesn't need to be atomic, but I think it should * improve performance if it is. */ con->rx_page = alloc_page(GFP_ATOMIC); if (con->rx_page == NULL) goto out_resched; cbuf_init(&con->cb, PAGE_CACHE_SIZE); } /* Only SCTP needs these really */ memset(&incmsg, 0, sizeof(incmsg)); msg.msg_control = incmsg; msg.msg_controllen = sizeof(incmsg); /* * iov[0] is the bit of the circular buffer between the current end * point (cb.base + cb.len) and the end of the buffer. */ iov[0].iov_len = con->cb.base - cbuf_data(&con->cb); iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb); iov[1].iov_len = 0; nvec = 1; /* * iov[1] is the bit of the circular buffer between the start of the * buffer and the start of the currently used section (cb.base) */ if (cbuf_data(&con->cb) >= con->cb.base) { iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb); iov[1].iov_len = con->cb.base; iov[1].iov_base = page_address(con->rx_page); nvec = 2; } len = iov[0].iov_len + iov[1].iov_len; r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len, MSG_DONTWAIT | MSG_NOSIGNAL); if (ret <= 0) goto out_close; /* Process SCTP notifications */ if (msg.msg_flags & MSG_NOTIFICATION) { msg.msg_control = incmsg; msg.msg_controllen = sizeof(incmsg); process_sctp_notification(con, &msg, page_address(con->rx_page) + con->cb.base); mutex_unlock(&con->sock_mutex); return 0; } BUG_ON(con->nodeid == 0); if (ret == len) call_again_soon = 1; cbuf_add(&con->cb, ret); ret = dlm_process_incoming_buffer(con->nodeid, page_address(con->rx_page), con->cb.base, con->cb.len, PAGE_CACHE_SIZE); if (ret == -EBADMSG) { log_print("lowcomms: addr=%p, base=%u, len=%u, " "iov_len=%u, iov_base[0]=%p, read=%d", page_address(con->rx_page), con->cb.base, con->cb.len, len, iov[0].iov_base, r); } if (ret < 0) goto out_close; cbuf_eat(&con->cb, ret); if (cbuf_empty(&con->cb) && !call_again_soon) { __free_page(con->rx_page); con->rx_page = NULL; } if (call_again_soon) goto out_resched; mutex_unlock(&con->sock_mutex); return 0;out_resched: if (!test_and_set_bit(CF_READ_PENDING, &con->flags)) queue_work(recv_workqueue, &con->rwork); mutex_unlock(&con->sock_mutex); return -EAGAIN;out_close: mutex_unlock(&con->sock_mutex); if (ret != -EAGAIN) { close_connection(con, false); /* Reconnect when there is something to send */ } /* Don't return success if we really got EOF */ if (ret == 0) ret = -EAGAIN; return ret;}/* Listening socket is busy, accept a connection */static int tcp_accept_from_sock(struct connection *con){ int result; struct sockaddr_storage peeraddr; struct socket *newsock; int len; int nodeid; struct connection *newcon; struct connection *addcon; memset(&peeraddr, 0, sizeof(peeraddr)); result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, IPPROTO_TCP, &newsock); if (result < 0) return -ENOMEM; mutex_lock_nested(&con->sock_mutex, 0); result = -ENOTCONN; if (con->sock == NULL) goto accept_err; newsock->type = con->sock->type; newsock->ops = con->sock->ops; result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK); if (result < 0) goto accept_err; /* Get the connected socket's peer */ memset(&peeraddr, 0, sizeof(peeraddr)); if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, &len, 2)) { result = -ECONNABORTED; goto accept_err; } /* Get the new node's NODEID */ make_sockaddr(&peeraddr, 0, &len); if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) { log_print("connect from non cluster node"); sock_release(newsock); mutex_unlock(&con->sock_mutex); return -1; } log_print("got connection from %d", nodeid); /* Check to see if we already have a connection to this node. This * could happen if the two nodes initiate a connection at roughly * the same time and the connections cross on the wire. * In this case we store the incoming one in "othercon" */ newcon = nodeid2con(nodeid, GFP_KERNEL); if (!newcon) { result = -ENOMEM; goto accept_err; } mutex_lock_nested(&newcon->sock_mutex, 1); if (newcon->sock) { struct connection *othercon = newcon->othercon; if (!othercon) { othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL); if (!othercon) { log_print("failed to allocate incoming socket"); mutex_unlock(&newcon->sock_mutex); result = -ENOMEM; goto accept_err; } othercon->nodeid = nodeid; othercon->rx_action = receive_from_sock; mutex_init(&othercon->sock_mutex); INIT_WORK(&othercon->swork, process_send_sockets); INIT_WORK(&othercon->rwork, process_recv_sockets); set_bit(CF_IS_OTHERCON, &othercon->flags); } if (!othercon->sock) { newcon->othercon = othercon; othercon->sock = newsock; newsock->sk->sk_user_data = othercon; add_sock(newsock, othercon); addcon = othercon; } else { printk("Extra connection from node %d attempted\n", nodeid); result = -EAGAIN; mutex_unlock(&newcon->sock_mutex); goto accept_err; } } else { newsock->sk->sk_user_data = newcon; newcon->rx_action = receive_from_sock; add_sock(newsock, newcon); addcon = newcon; } mutex_unlock(&newcon->sock_mutex); /* * Add it to the active queue in case we got data * beween processing the accept adding the socket * to the read_sockets list */ if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags)) queue_work(recv_workqueue, &addcon->rwork); mutex_unlock(&con->sock_mutex); return 0;accept_err: mutex_unlock(&con->sock_mutex); sock_release(newsock); if (result != -EAGAIN) log_print("error accepting connection from node: %d", result); return result;}static void free_entry(struct writequeue_entry *e){ __free_page(e->page); kfree(e);}/* Initiate an SCTP association. This is a special case of send_to_sock() in that we don't yet have a peeled-off socket for this association, so we use the listening socket and add the primary IP address of the remote node. */static void sctp_init_assoc(struct connection *con){ struct sockaddr_storage rem_addr; char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; struct msghdr outmessage; struct cmsghdr *cmsg; struct sctp_sndrcvinfo *sinfo; struct connection *base_con; struct writequeue_entry *e; int len, offset; int ret; int addrlen; struct kvec iov[1]; if (test_and_set_bit(CF_INIT_PENDING, &con->flags)) return; if (con->retries++ > MAX_CONNECT_RETRIES) return; log_print("Initiating association with node %d", con->nodeid); if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) { log_print("no address for nodeid %d", con->nodeid); return; } base_con = nodeid2con(0, 0); BUG_ON(base_con == NULL); make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen); outmessage.msg_name = &rem_addr; outmessage.msg_namelen = addrlen; outmessage.msg_control = outcmsg; outmessage.msg_controllen = sizeof(outcmsg); outmessage.msg_flags = MSG_EOR; spin_lock(&con->writequeue_lock); e = list_entry(con->writequeue.next, struct writequeue_entry, list); BUG_ON((struct list_head *) e == &con->writequeue); len = e->len; offset = e->offset; spin_unlock(&con->writequeue_lock); kmap(e->page); /* Send the first block off the write queue */ iov[0].iov_base = page_address(e->page)+offset; iov[0].iov_len = len; cmsg = CMSG_FIRSTHDR(&outmessage); cmsg->cmsg_level = IPPROTO_SCTP; cmsg->cmsg_type = SCTP_SNDRCV; cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); sinfo = CMSG_DATA(cmsg); memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid()); outmessage.msg_controllen = cmsg->cmsg_len; ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len); if (ret < 0) { log_print("Send first packet to node %d failed: %d", con->nodeid, ret); /* Try again later */ clear_bit(CF_CONNECT_PENDING, &con->flags); clear_bit(CF_INIT_PENDING, &con->flags); } else { spin_lock(&con->writequeue_lock); e->offset += ret; e->len -= ret; if (e->len == 0 && e->users == 0) { list_del(&e->list); kunmap(e->page); free_entry(e); } spin_unlock(&con->writequeue_lock); }}/* Connect a new socket to its peer */static void tcp_connect_to_sock(struct connection *con){ int result = -EHOSTUNREACH; struct sockaddr_storage saddr; int addr_len; struct socket *sock; if (con->nodeid == 0) { log_print("attempt to connect sock 0 foiled"); return; } mutex_lock(&con->sock_mutex); if (con->retries++ > MAX_CONNECT_RETRIES) goto out; /* Some odd races can cause double-connects, ignore them */ if (con->sock) { result = 0; goto out; } /* Create a socket to communicate with */ result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, IPPROTO_TCP, &sock); if (result < 0) goto out_err; memset(&saddr, 0, sizeof(saddr)); if (dlm_nodeid_to_addr(con->nodeid, &saddr)) goto out_err; sock->sk->sk_user_data = con; con->rx_action = receive_from_sock; con->connect_action = tcp_connect_to_sock; add_sock(sock, con); make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len); log_print("connecting to %d", con->nodeid); result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len, O_NONBLOCK); if (result == -EINPROGRESS) result = 0; if (result == 0) goto out;out_err: if (con->sock) { sock_release(con->sock); con->sock = NULL; } /* * Some errors are fatal and this list might need adjusting. For other * errors we try again until the max number of retries is reached. */ if (result != -EHOSTUNREACH && result != -ENETUNREACH && result != -ENETDOWN && result != EINVAL && result != -EPROTONOSUPPORT) { lowcomms_connect_sock(con); result = 0; }out: mutex_unlock(&con->sock_mutex); return;}static struct socket *tcp_create_listen_sock(struct connection *con, struct sockaddr_storage *saddr){ struct socket *sock = NULL; int result = 0; int one = 1; int addr_len; if (dlm_local_addr[0]->ss_family == AF_INET) addr_len = sizeof(struct sockaddr_in); else addr_len = sizeof(struct sockaddr_in6); /* Create a socket to communicate with */ result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, IPPROTO_TCP, &sock); if (result < 0) { log_print("Can't create listening comms socket"); goto create_out; } result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)); if (result < 0) { log_print("Failed to set SO_REUSEADDR on socket: %d", result); } sock->sk->sk_user_data = con; con->rx_action = tcp_accept_from_sock; con->connect_action = tcp_connect_to_sock; con->sock = sock; /* Bind to our port */ make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len); result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len); if (result < 0) { log_print("Can't bind to port %d", dlm_config.ci_tcp_port); sock_release(sock); sock = NULL; con->sock = NULL; goto create_out; } result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&one, sizeof(one)); if (result < 0) { log_print("Set keepalive failed: %d", result); } result = sock->ops->listen(sock, 5); if (result < 0) { log_print("Can't listen on port %d", dlm_config.ci_tcp_port); sock_release(sock); sock = NULL; goto create_out; }create_out:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -