ctdb-protocol: Add marshalling for ctdb_req_keepalive
authorAmitay Isaacs <amitay@gmail.com>
Wed, 28 Jun 2017 06:50:53 +0000 (16:50 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:26 +0000 (14:59 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/protocol/protocol_api.h
ctdb/protocol/protocol_debug.c
ctdb/protocol/protocol_keepalive.c [new file with mode: 0644]
ctdb/tests/cunit/protocol_test_101.sh
ctdb/tests/src/protocol_common_ctdb.c
ctdb/tests/src/protocol_common_ctdb.h
ctdb/tests/src/protocol_ctdb_compat_test.c
ctdb/tests/src/protocol_ctdb_test.c
ctdb/wscript

index d6acefbcbcc5b24996043a1106f7357af8ba4c91..522b009b151dada54db7c64222fc5492a763ee31 100644 (file)
@@ -636,6 +636,20 @@ int ctdb_req_message_data_pull(uint8_t *buf, size_t buflen,
                               TALLOC_CTX *mem_ctx,
                               struct ctdb_req_message_data *c);
 
+/* From protocol/protocol_keepalive.c */
+
+size_t ctdb_req_keepalive_len(struct ctdb_req_header *h,
+                             struct ctdb_req_keepalive *c);
+
+int ctdb_req_keepalive_push(struct ctdb_req_header *h,
+                           struct ctdb_req_keepalive *c,
+                           uint8_t *buf, size_t *buflen);
+
+int ctdb_req_keepalive_pull(uint8_t *buf, size_t buflen,
+                           struct ctdb_req_header *h,
+                           TALLOC_CTX *mem_ctx,
+                           struct ctdb_req_keepalive *c);
+
 /* From protocol/protocol_event.c */
 
 void ctdb_event_header_fill(struct ctdb_event_header *h, uint32_t reqid);
index a448fad9895c426f4829c2bee4ff966283aee632..9cca76c5107b3765cd0b9c7ae239f4d3fd08722d 100644 (file)
@@ -461,6 +461,14 @@ static void ctdb_reply_control_print(struct ctdb_reply_control *c, FILE *fp)
        fprintf(fp, "\n");
 }
 
+static void ctdb_req_keepalive_print(struct ctdb_req_keepalive *c, FILE *fp)
+{
+       fprintf(fp, "Data\n");
+       fprintf(fp, "  version:0x%"PRIx32, c->version);
+       fprintf(fp, "  uptime:%"PRIu32, c->uptime);
+       fprintf(fp, "\n");
+}
+
 /*
  * Parse routines
  */
@@ -585,6 +593,21 @@ static void ctdb_reply_control_parse(uint8_t *buf, size_t buflen, FILE *fp,
        ctdb_reply_control_print(&c, fp);
 }
 
+static void ctdb_req_keepalive_parse(uint8_t *buf, size_t buflen, FILE *fp,
+                                    TALLOC_CTX *mem_ctx)
+{
+       struct ctdb_req_keepalive c;
+       int ret;
+
+       ret = ctdb_req_keepalive_pull(buf, buflen, NULL, mem_ctx, &c);
+       if (ret != 0) {
+               fprintf(fp, "Failed to parse CTDB_REQ_KEEPALIVE\n");
+               return;
+       }
+
+       ctdb_req_keepalive_print(&c, fp);
+}
+
 /*
  * Packet print
  */
@@ -650,6 +673,7 @@ void ctdb_packet_print(uint8_t *buf, size_t buflen, FILE *fp)
                        break;
 
                case CTDB_REQ_KEEPALIVE:
+                       ctdb_req_keepalive_parse(buf, buflen, fp, mem_ctx);
                        break;
 
                default:
diff --git a/ctdb/protocol/protocol_keepalive.c b/ctdb/protocol/protocol_keepalive.c
new file mode 100644 (file)
index 0000000..3a1fc0e
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+   CTDB protocol marshalling
+
+   Copyright (C) Amitay Isaacs  2017
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "system/network.h"
+
+#include <talloc.h>
+#include <tdb.h>
+
+#include "protocol.h"
+#include "protocol_api.h"
+#include "protocol_private.h"
+
+size_t ctdb_req_keepalive_len(struct ctdb_req_header *h,
+                             struct ctdb_req_keepalive *c)
+{
+       return ctdb_req_header_len(h) +
+               ctdb_uint32_len(&c->version) +
+               ctdb_uint32_len(&c->uptime);
+}
+
+int ctdb_req_keepalive_push(struct ctdb_req_header *h,
+                           struct ctdb_req_keepalive *c,
+                           uint8_t *buf, size_t *buflen)
+{
+       size_t length, offset = 0, np;
+
+       length = ctdb_req_keepalive_len(h, c);
+       if (*buflen < length) {
+               *buflen = length;
+               return EMSGSIZE;
+       }
+
+       h->length = *buflen;
+       ctdb_req_header_push(h, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&c->version, buf+offset, &np);
+       offset += np;
+
+       ctdb_uint32_push(&c->uptime, buf+offset, &np);
+       offset += np;
+
+       return 0;
+}
+
+int ctdb_req_keepalive_pull(uint8_t *buf, size_t buflen,
+                           struct ctdb_req_header *h,
+                           TALLOC_CTX *mem_ctx,
+                           struct ctdb_req_keepalive *c)
+{
+       struct ctdb_req_header header;
+       size_t offset = 0, np;
+       int ret;
+
+       ret = ctdb_req_header_pull(buf, buflen, &header, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       if (h != NULL) {
+               *h = header;
+       }
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->version, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       ret = ctdb_uint32_pull(buf+offset, buflen-offset, &c->uptime, &np);
+       if (ret != 0) {
+               return ret;
+       }
+       offset += np;
+
+       return 0;
+}
index 15c4150b3cb958accf75027cdf154da4cf16971d..800c6b598f7d8d44e195eb5c6124ce4ec9eb8d02 100755 (executable)
@@ -54,6 +54,7 @@ output=$(
     generate_message_output "ctdb_message_data"
     generate_message_output "ctdb_req_message"
     echo "ctdb_req_message_data"
+    echo "ctdb_req_keepalive"
 )
 
 ok "$output"
index be9e2560cc69c8bd57c5b198c78f8161dc4f7d7d..26b1ad65d80a3904c3da1f13b1384adae33897b8 100644 (file)
@@ -1948,3 +1948,17 @@ void verify_ctdb_req_message_data(struct ctdb_req_message_data *c,
        assert(c->srvid == c2->srvid);
        verify_tdb_data(&c->data, &c2->data);
 }
+
+void fill_ctdb_req_keepalive(TALLOC_CTX *mem_ctx,
+                                   struct ctdb_req_keepalive *c)
+{
+       c->version = rand32();
+       c->uptime = rand32();
+}
+
+void verify_ctdb_req_keepalive(struct ctdb_req_keepalive *c,
+                                     struct ctdb_req_keepalive *c2)
+{
+       assert(c->version == c2->version);
+       assert(c->uptime == c2->uptime);
+}
index f5bd16a8ef7ce6f98f8092ae81e1cec28d796082..cf22418b623e1369d24df22b9611a87b8cdcf104 100644 (file)
@@ -89,4 +89,9 @@ void fill_ctdb_req_message_data(TALLOC_CTX *mem_ctx,
 void verify_ctdb_req_message_data(struct ctdb_req_message_data *c,
                                  struct ctdb_req_message_data *c2);
 
+void fill_ctdb_req_keepalive(TALLOC_CTX *mem_ctx,
+                            struct ctdb_req_keepalive *c);
+void verify_ctdb_req_keepalive(struct ctdb_req_keepalive *c,
+                              struct ctdb_req_keepalive *c2);
+
 #endif /* __CTDB_PROTOCOL_COMMON_CTDB_H__ */
index 896c82e67d68226dedcc07a3b0ef3632231bf250..8357283d3729316067cdd30c26558b7718bb6745 100644 (file)
@@ -28,6 +28,7 @@
 #include "protocol/protocol_call.c"
 #include "protocol/protocol_control.c"
 #include "protocol/protocol_message.c"
+#include "protocol/protocol_keepalive.c"
 
 #include "tests/src/protocol_common.h"
 #include "tests/src/protocol_common_ctdb.h"
@@ -1042,6 +1043,70 @@ static int ctdb_req_message_data_pull_old(uint8_t *buf, size_t buflen,
        return 0;
 }
 
+struct ctdb_req_keepalive_wire {
+       struct ctdb_req_header hdr;
+       uint32_t version;
+       uint32_t uptime;
+};
+
+static size_t ctdb_req_keepalive_len_old(struct ctdb_req_header *h,
+                                        struct ctdb_req_keepalive *c)
+{
+       return sizeof(struct ctdb_req_keepalive_wire);
+}
+
+static int ctdb_req_keepalive_push_old(struct ctdb_req_header *h,
+                                      struct ctdb_req_keepalive *c,
+                                      uint8_t *buf, size_t *buflen)
+{
+       struct ctdb_req_keepalive_wire *wire =
+               (struct ctdb_req_keepalive_wire *)buf;
+       size_t length;
+
+       length = ctdb_req_keepalive_len_old(h, c);
+       if (*buflen < length) {
+               *buflen = length;
+               return EMSGSIZE;
+       }
+
+       h->length = *buflen;
+       ctdb_req_header_push_old(h, (uint8_t *)&wire->hdr);
+
+       wire->version = c->version;
+       wire->uptime = c->uptime;
+
+       return 0;
+}
+
+static int ctdb_req_keepalive_pull_old(uint8_t *buf, size_t buflen,
+                                      struct ctdb_req_header *h,
+                                      TALLOC_CTX *mem_ctx,
+                                      struct ctdb_req_keepalive *c)
+{
+       struct ctdb_req_keepalive_wire *wire =
+               (struct ctdb_req_keepalive_wire *)buf;
+       size_t length;
+       int ret;
+
+       length = sizeof(struct ctdb_req_keepalive_wire);
+       if (buflen < length) {
+               return EMSGSIZE;
+       }
+
+       if (h != NULL) {
+               ret = ctdb_req_header_pull_old((uint8_t *)&wire->hdr, buflen,
+                                              h);
+               if (ret != 0) {
+                       return ret;
+               }
+       }
+
+       c->version = wire->version;
+       c->uptime = wire->uptime;
+
+       return 0;
+}
+
 
 COMPAT_CTDB1_TEST(struct ctdb_req_header, ctdb_req_header);
 
@@ -1057,6 +1122,8 @@ COMPAT_CTDB6_TEST(struct ctdb_reply_control, ctdb_reply_control, CTDB_REPLY_CONT
 COMPAT_CTDB7_TEST(struct ctdb_req_message, ctdb_req_message, CTDB_REQ_MESSAGE);
 COMPAT_CTDB4_TEST(struct ctdb_req_message_data, ctdb_req_message_data, CTDB_REQ_MESSAGE);
 
+COMPAT_CTDB4_TEST(struct ctdb_req_keepalive, ctdb_req_keepalive, CTDB_REQ_KEEPALIVE);
+
 #define NUM_CONTROLS   151
 
 int main(int argc, char *argv[])
@@ -1110,5 +1177,7 @@ int main(int argc, char *argv[])
        }
        COMPAT_TEST_FUNC(ctdb_req_message_data)();
 
+       COMPAT_TEST_FUNC(ctdb_req_keepalive)();
+
        return 0;
 }
index e563b31812baddb0fcabe8344ad12031c16149d5..450f5c2aef4a10645eb5b8760702ba5d3add95f7 100644 (file)
@@ -25,6 +25,7 @@
 #include "protocol/protocol_call.c"
 #include "protocol/protocol_control.c"
 #include "protocol/protocol_message.c"
+#include "protocol/protocol_keepalive.c"
 #include "protocol/protocol_packet.c"
 
 #include "tests/src/protocol_common.h"
@@ -305,6 +306,9 @@ PROTOCOL_CTDB7_TEST(struct ctdb_req_message, ctdb_req_message,
 PROTOCOL_CTDB4_TEST(struct ctdb_req_message_data, ctdb_req_message_data,
                        CTDB_REQ_MESSAGE);
 
+PROTOCOL_CTDB4_TEST(struct ctdb_req_keepalive, ctdb_req_keepalive,
+                       CTDB_REQ_KEEPALIVE);
+
 int main(int argc, char *argv[])
 {
        uint32_t opcode;
@@ -366,5 +370,7 @@ int main(int argc, char *argv[])
        }
        TEST_FUNC(ctdb_req_message_data)();
 
+       TEST_FUNC(ctdb_req_keepalive)();
+
        return 0;
 }
index bbea77397fe469d2096ab7031bc6035efee34d81..a35b138eba4c42dbd81e0bdb0bbf53db4100b9d0 100644 (file)
@@ -396,6 +396,7 @@ def build(bld):
                                              protocol_call.c
                                              protocol_message.c
                                              protocol_control.c
+                                             protocol_keepalive.c
                                              protocol_client.c
                                              protocol_debug.c
                                              protocol_util.c