1st working ib integrated ctdb
[amitay/samba.git] / ctdb / ib / ibw_ctdb.c
index 5152062d246aa4f33edb5229fc6a51f13d0a9ac2..53293810ca8311c77329416a4e98674eb1f1d1d9 100644 (file)
 #include "ibwrapper.h"
 #include "ibw_ctdb.h"
 
+int ctdb_ibw_node_connect(struct ibw_ctx *ictx, struct ctdb_node *node)
+{
+       struct sockaddr_in sock_out;
+
+       memset(&sock_out, 0, sizeof(struct sockaddr_in));
+       inet_pton(AF_INET, node->address.address, &sock_out.sin_addr);
+       sock_out.sin_port = htons(node->address.port);
+       sock_out.sin_family = PF_INET;
+
+       if (ibw_connect(ictx, &sock_out, node)) {
+               DEBUG(0, ("ctdb_ibw_node_connect: ibw_connect failed - retrying in 1 sec...\n"));
+               /* try again once a second */
+               event_add_timed(node->ctdb->ev, node, timeval_current_ofs(1, 0), 
+                       ctdb_ibw_node_connect_event, node);
+               return -1;
+       }
+
+       /* continues at ibw_ctdb.c/IBWC_CONNECTED in good case */
+       return 0;
+}
+
+void ctdb_ibw_node_connect_event(struct event_context *ev, struct timed_event *te, 
+       struct timeval t, void *private)
+{
+       struct ctdb_node *node = talloc_get_type(private, struct ctdb_node);
+       struct ibw_ctx *ictx = talloc_get_type(node->ctdb->private, struct ibw_ctx);
+
+       ctdb_ibw_node_connect(ictx, node);
+}
+
 int ctdb_ibw_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
 {
        if (ctx!=NULL) {
@@ -76,11 +106,17 @@ int ctdb_ibw_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
                        if (node!=NULL)
                                node->ctdb->upcalls->node_dead(node);
                        talloc_free(conn);
+                       /* normal + intended disconnect => not reconnecting in this layer */
                } break;
                case IBWC_ERROR: {
-/*                     struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node);
+                       struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node);
                        if (node!=NULL)
-                               node->ctdb->upcalls->node_dead(node);*/
+                               node->private = NULL; /* not to use again */
+
+                       DEBUG(10, ("IBWC_ERROR, reconnecting immediately...\n"));
+                       talloc_free(conn);
+                       event_add_timed(node->ctdb->ev, node, timeval_current_ofs(1, 0),
+                               ctdb_ibw_node_connect_event, node);
                } break;
                default:
                        assert(0);
@@ -94,17 +130,20 @@ int ctdb_ibw_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
 int ctdb_ibw_receive_handler(struct ibw_conn *conn, void *buf, int n)
 {
        struct ctdb_context *ctdb = talloc_get_type(conn->ctx->ctx_userdata, struct ctdb_context);
+       void    *buf2; /* future TODO: a solution for removal of this */
 
        assert(ctdb!=NULL);
+       assert(buf!=NULL);
+       assert(conn!=NULL);
        assert(conn->state==IBWC_CONNECTED);
 
-       /* TODO: shall I short-circuit this in ibwrapper? */
-       /* maybe when everything go fine... */
+       /* so far "buf" is an ib-registered memory area
+        * and being reused for next receive
+        * noticed that HL requires talloc-ed memory to be stolen */
+       buf2 = talloc_zero_size(conn, n);
+       memcpy(buf2, buf, n);
 
-       /* TODO2: !!! here I can provide conn->conn_userdata (with no perf. penalty) -
-        * as struct ctdb_node in case the connection
-        * has been built up by ibw_connect !!! */
-       ctdb->upcalls->recv_pkt(ctdb, (uint8_t *)buf, (uint32_t)n);
+       ctdb->upcalls->recv_pkt(ctdb, (uint8_t *)buf2, (uint32_t)n);
 
        return 0;
 }