Skip to contentSkip to conversation

CLD: convert back to libevent

PATCH In review Jeff Garzik 3 participants 6 messages Dec 31, 2010 – Jan 3, 2011 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 CLD: convert back to libevent

Switch CLD from hand-rolled server poll code, to libevent. Follows similar techniques and rationale as chunkd commit c1aed7464f237e5a6309351bf003162c77d69e27. This reverts ancient commit 90b3b5edcf5aa00577f4395fdbb490ed7e9be824.

---
 cld/Makefile.am |    3 -
 cld/cld.h       |   22 +++----
 cld/server.c    |  161 ++++++++++++++++++++------------------------------------
 cld/session.c   |   69 ++++++++++++++++--------
 4 files changed, 118 insertions(+), 137 deletions(-)
cld/Makefile.am +2 -1
index 9a13ce0..30eea0b 100644
@@ -12,7 +12,8 @@ cld_SOURCES	= cldb.h cld.h \
 		  cldb.c msg.c server.c session.c util.c
 cld_LDADD	= \
 		  ../lib/libhail.la @GLIB_LIBS@ @CRYPTO_LIBS@ \
-		  @SSL_LIBS@ @DB4_LIBS@ @XML_LIBS@ @LIBCURL@
+		  @SSL_LIBS@ @DB4_LIBS@ @XML_LIBS@ @LIBCURL@ \
+		  @EVENT_LIBS@
 
 cldbadm_SOURCES	= cldb.h cldbadm.c
 cldbadm_LDADD	= @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@
cld/cld.h +11 -11
index 4c0099f..17f14b8 100644
@@ -22,8 +22,9 @@
 
 #include <netinet/in.h>
 #include <sys/time.h>
-#include <poll.h>
+#include <event.h>
 #include <glib.h>
+#include <elist.h>
 #include "cldb.h"
 #include <cld_msg_rpc.h>
 #include <cld_common.h>
@@ -59,13 +60,13 @@ struct session {
 
 	uint64_t		last_contact;
 	uint64_t		next_fh;
-	struct cld_timer	timer;
+	struct event		timer;
 
 	uint64_t		next_seqid_in;
 	uint64_t		next_seqid_out;
 
 	GList			*out_q;		/* outgoing pkts (to client) */
-	struct cld_timer	retry_timer;
+	struct event		retry_timer;
 
 	char			user[CLD_MAX_USERNAME];
 
@@ -85,10 +86,10 @@ struct server_stats {
 	unsigned long		garbage;	/* num. garbage pkts dropped */
 };
 
-struct server_poll {
+struct server_socket {
 	int			fd;
-	bool			(*cb)(int fd, short events, void *userdata);
-	void			*userdata;
+	struct event		ev;
+	struct list_head	sockets_node;
 };
 
 struct server {
@@ -103,14 +104,13 @@ struct server {
 
 	struct cldb		cldb;		/* database info */
 
-	GArray			*polls;
-	GArray			*poll_data;
+	struct event_base	*evbase_main;
 
-	GHashTable		*sessions;
+	struct list_head	sockets;
 
-	struct cld_timer_list	timers;
+	GHashTable		*sessions;
 
-	struct cld_timer	chkpt_timer;	/* db4 checkpoint timer */
+	struct event		chkpt_timer;	/* db4 checkpoint timer */
 
 	struct server_stats	stats;		/* global statistics */
 };
cld/server.c +58 -103

302 lines not on this page · raw message

cld/session.c +47 -22

155 lines not on this page · raw message

#1 lore raw

PATCH 2/3 CLD: switch network proto from UDP to TCP

Convert CLD network protocol from UDP to TCP. Server, client lib, and chunkd's cldu module are all updated. tabled's cldu module must be updated also.

The original rationale for UDP use was following Google's lead, based on the advice in the original Chubby paper, describing TCP's back-off policies and other behavior during times of high network congestion.

This seems a bit dubious without further third party evidence, and TCP vastly simplifies our lives. While the code remains open and modular enough to support other protocols (hopefully RDMA or SCTP one day), this upgrade from UDP to TCP promises to make the current codebase much easier to use, while avoiding the "reinvent TCP, by using UDP" problem, which was a rabbit hole threatening CLD.

---
 chunkd/cldu.c        |    6 
 cld/cld.h            |   43 ++++++
 cld/msg.c            |    4 
 cld/server.c         |  356 ++++++++++++++++++++++++++++++++++++---------------
 cld/session.c        |    4 
 configure.ac         |    1 
 include/Makefile.am  |    2 
 include/cld_common.h |    4 
 include/cldc.h       |   24 ++-
 include/ncld.h       |    4 
 include/ubbp.h       |   52 +++++++
 lib/Makefile.am      |    2 
 lib/cldc-dns.c       |    2 
 lib/cldc-tcp.c       |  185 ++++++++++++++++++++++++++
 lib/cldc-udp.c       |  141 --------------------
 lib/cldc.c           |   54 +++----
 16 files changed, 595 insertions(+), 289 deletions(-)
chunkd/cldu.c +3 -3
index 026c523..41f94b5 100644
@@ -165,7 +165,7 @@ static void cldu_sess_event(void *priv, uint32_t what)
 		 */
 		if (cs->nsess) {
 			applog(LOG_ERR, "Session failed, sid " SIDFMT,
-			       SIDARG(cs->nsess->udp->sess->sid));
+			       SIDARG(cs->nsess->tcp->sess->sid));
 		} else {
 			applog(LOG_ERR, "Session open failed");
 		}
@@ -177,7 +177,7 @@ static void cldu_sess_event(void *priv, uint32_t what)
 	} else {
 		if (cs)
 			applog(LOG_INFO, "cldc event 0x%x sid " SIDFMT,
-			       what, SIDARG(cs->nsess->udp->sess->sid));
+			       what, SIDARG(cs->nsess->tcp->sess->sid));
 		else
 			applog(LOG_INFO, "cldc event 0x%x no sid", what);
 	}
@@ -372,7 +372,7 @@ static int cldu_set_cldc(struct cld_session *cs, int newactive)
 	}
 
 	applog(LOG_INFO, "New CLD session created, sid " SIDFMT,
-	       SIDARG(cs->nsess->udp->sess->sid));
+	       SIDARG(cs->nsess->tcp->sess->sid));
 
 	/*
 	 * First, make sure the base directory exists.
cld/cld.h +42 -1

71 lines not on this page · raw message

cld/msg.c +0 -4
index dd8cf03..8d83e74 100644
@@ -29,10 +29,6 @@
 #include <cld_msg_rpc.h>
 #include "cld.h"
 
-enum {
-	CLD_MAX_UDP_SEG		= 1024,
-};
-
 struct pathname_info {
 	const char	*dir;
 	size_t		dir_len;
cld/server.c +258 -98

541 lines not on this page · raw message

cld/session.c +2 -2
index 9887aaa..a38874a 100644
@@ -550,7 +550,7 @@ static int sess_retry_output(struct session *sess, time_t *next_retry_out)
 				  			op->pkt_len));
 		}
 
-		rc = udp_tx(sess->sock_fd, (struct sockaddr *) &sess->addr,
+		rc = tcp_tx(sess->sock_fd, (struct sockaddr *) &sess->addr,
 			    sess->addr_len, op->pkt_data, op->pkt_len);
 		if (rc)
 			break;
@@ -715,7 +715,7 @@ bool sess_sendmsg(struct session *sess,
 	     tmp_list = g_list_next(tmp_list)) {
 		struct session_outpkt *op =
 			(struct session_outpkt *) tmp_list->data;
-		udp_tx(sess->sock_fd, (struct sockaddr *) &sess->addr,
+		tcp_tx(sess->sock_fd, (struct sockaddr *) &sess->addr,
 			sess->addr_len, op->pkt_data, op->pkt_len);
 	}
 
configure.ac +1 -0
index 9cfad23..d93a9a5 100644
@@ -69,6 +69,7 @@ AC_CHECK_HEADERS(sys/sendfile.h sys/filio.h)
 AC_CHECK_HEADER(db.h,[],exit 1)
 
 dnl Checks for typedefs, structures, and compiler characteristics.
+AC_C_BIGENDIAN
 dnl AC_TYPE_SIZE_T
 dnl AC_TYPE_PID_T
 
include/Makefile.am +1 -1
index 967352a..70f56a9 100644
@@ -4,6 +4,6 @@ EXTRA_DIST =		\
 	elist.h chunk_msg.h chunksrv.h chunk-private.h objcache.h
 
 include_HEADERS =	\
-	cldc.h cld_common.h ncld.h chunkc.h chunk_msg.h		\
+	ubbp.h cldc.h cld_common.h ncld.h chunkc.h chunk_msg.h		\
 	hail_log.h hstor.h anet.h
 
include/cld_common.h +4 -0
index 84b1ec6..efb5911 100644
@@ -31,6 +31,10 @@
 
 #define CLD_ALIGN8(n) ((8 - ((n) & 7)) & 7)
 
+enum {
+	CLD_RAW_MSG_SZ		= 4096,
+};
+
 struct cld_timer {
 	bool			fired;
 	bool			on_list;
include/cldc.h +16 -8
index c64eef9..f98d151 100644
@@ -25,6 +25,7 @@
 #include <cld_msg_rpc.h>
 #include <cld_common.h>
 #include <hail_log.h>
+#include <ubbp.h>
 
 struct cldc_session;
 
@@ -142,13 +143,20 @@ struct cldc_host {
 	unsigned short	port;
 };
 
-/** A UDP implementation of the CLD client protocol */
-struct cldc_udp {
+/** A TCP implementation of the CLD client protocol */
+struct cldc_tcp {
 	uint8_t		addr[64];		/* server address */
 	size_t		addr_len;
 
 	int		fd;
 
+	struct ubbp_header ubbp;
+	unsigned int	ubbp_read;
+
+	char		raw_pkt[CLD_RAW_MSG_SZ];
+	unsigned int	raw_size;
+	unsigned int	raw_read;
+
 	struct cldc_session *sess;
 
 	int		(*cb)(struct cldc_session *, void *);
@@ -215,12 +223,12 @@ extern void cldc_copts_get_data(const struct cldc_call_opts *copts,
 extern void cldc_copts_get_metadata(const struct cldc_call_opts *copts,
 				    struct cldc_node_metadata *md);
 
-/* cldc-udp */
-extern void cldc_udp_free(struct cldc_udp *udp);
-extern int cldc_udp_new(const char *hostname, int port,
-		 struct cldc_udp **udp_out);
-extern int cldc_udp_receive_pkt(struct cldc_udp *udp);
-extern int cldc_udp_pkt_send(void *private,
+/* cldc-tcp */
+extern void cldc_tcp_free(struct cldc_tcp *tcp);
+extern int cldc_tcp_new(const char *hostname, int port,
+		 struct cldc_tcp **tcp_out);
+extern int cldc_tcp_receive_pkt_data(struct cldc_tcp *tcp);
+extern int cldc_tcp_pkt_send(void *private,
 			  const void *addr, size_t addrlen,
 			  const void *buf, size_t buflen);
 
include/ncld.h +2 -2
index aad88b7..21b6e36 100644
@@ -40,8 +40,8 @@ struct ncld_sess {
 	int			errc;
 	GList			*handles;
 	int			to_thread[2];
-	struct cldc_udp		*udp;
-	struct cld_timer	udp_timer;
+	struct cldc_tcp		*tcp;
+	struct cld_timer	tcp_timer;
 	struct cld_timer_list	tlist;
 	void			(*event)(void *, unsigned int);
 	void			*event_arg;
include/ubbp.h new file +52 -0

52 lines not on this page · raw message

lib/Makefile.am +1 -1
index 616b881..8be1835 100644
@@ -23,7 +23,7 @@ lib_LTLIBRARIES		= libhail.la
 libhail_la_SOURCES	=	\
 	atcp.c			\
 	cldc.c			\
-	cldc-udp.c		\
+	cldc-tcp.c		\
 	cldc-dns.c		\
 	common.c		\
 	libtimer.c		\
lib/cldc-dns.c +1 -1
index 92f875d..e987c01 100644
@@ -63,7 +63,7 @@ int cldc_saveaddr(struct cldc_host *hp,
 
 	memset(&hints, 0, sizeof(struct addrinfo));
 	hints.ai_family = PF_UNSPEC;
-	hints.ai_socktype = SOCK_DGRAM;
+	hints.ai_socktype = SOCK_STREAM;
 
 	rc = getaddrinfo(hostname, portstr, &hints, &res0);
 	if (rc) {
lib/cldc-tcp.c new file +185 -0

185 lines not on this page · raw message

lib/cldc-udp.c deleted +0 -141

141 lines not on this page · raw message

lib/cldc.c +27 -27

150 lines not on this page · raw message

#2 lore raw

PATCH 3/3 CLD: enable replication on server and client

Enable master/slave replication in CLD, by db4's replication manager.

On the server side, you may input a list of peer servers via DNS SRV records, or manually on the command line for testing. Each peer participates in replication elections, and functions as a slave until the replication engine decides it is to be elected master. Under the hood, db4 does use PAXOS in versions >= 4.7. All CLD network operations are already wrapped inside db4 transactions, so this merely extends current reliability. The server requires a quorum of 5 peers by default, but this is easily changeable.

For the most part, this piece of code has been around for a little while. It has now been updated for all recent hail changes. The new bits are client side work that enables replication/failover to work smoothly and reliably.

On the client side, the main change is passing around a list of cldc_host. All users of the lib built this internally, and then hobbled themselves by calling a singleton hostname/port interface. This limitation has been removed, properly exposing the entire list of CLD servers to the CLD client API.

CLD clients will make TCP connections to each CLD server, in priority order. Connections failing will move on to the next server. Ditto with in-session server death; clients will move back into seeking-a-master state. CLD clients always seek the master for all operations; no attempt is made to divide the load by sending clients to slave servers.

This is all moving towards the goal of having clients connect to a cell (aka a cluster), rather than a host. CLD users will specify

	cld://mycluster.example.com/tabled/1234/ABCD

and the client lib will perform a DNS SRV lookup on "mycluster.example.com". The DNS SRV lookup returns one or more hosts and priorities, each of which are in turn looked-up with getaddrinfo(3).

As an added reliability feature, stolen from many current P2P applications, if you reach at least one CLD server, you can download a host list from that server. This makes sure you can bootstrap into the cloud regardless of how poorly your systems are running :) Bittorrent users are already familiar with "peer exchange."

Unlike the previous two patches, this code is hot off the presses, and probably still has bugs (hence the "NOT-signed-off-by" below).

---
 chunkd/cldu.c   |   13 +-
 cld/cld.h       |   22 +++
 cld/cldb.c      |   69 ++++++++++--
 cld/cldb.h      |    9 +
 cld/cldbadm.c   |    6 -
 cld/server.c    |  313 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 include/cldc.h  |    3 
 include/ncld.h  |    3 
 lib/cldc-tcp.c  |   21 +++
 lib/cldc.c      |   22 ---
 tools/cldcli.c  |    4 
 tools/cldfuse.c |    5 
 12 files changed, 423 insertions(+), 67 deletions(-)
chunkd/cldu.c +6 -7
index 41f94b5..b17643e 100644
@@ -47,6 +47,8 @@ struct cld_session {
 	int			actx;		/* Active host cldv[actx] */
 	struct cld_host		cldv[N_CLD];
 
+	GList			*host_list;
+
 	int			event_pipe[2];
 	struct event		ev;
 	struct event		ev_timer;
@@ -357,7 +359,7 @@ static int cldu_set_cldc(struct cld_session *cs, int newactive)
 		applog(LOG_INFO, "Selected CLD host %s port %u",
 		       hp->host, hp->port);
 
-	cs->nsess = ncld_sess_open(hp->host, hp->port, &error,
+	cs->nsess = ncld_sess_open(cs->host_list, &error,
 				   cldu_sess_event, cs, "tabled", "tabled",
 				   &cldu_hail_log);
 	if (cs->nsess == NULL) {
@@ -505,8 +507,7 @@ int cld_begin(const char *thishost, uint32_t nid, char *infopath,
 			goto err_addr;
 		}
 
-		/* copy host_list into cld_session host array,
-		 * taking ownership of alloc'd strings along the way
+		/* copy host_list into cld_session host array
 		 */
 		i = 0;
 		for (tmp = host_list; tmp; tmp = tmp->next) {
@@ -514,15 +515,13 @@ int cld_begin(const char *thishost, uint32_t nid, char *infopath,
 			if (i < N_CLD) {
 				memcpy(&cs->cldv[i].h, hp,
 				       sizeof(struct cldc_host));
+				cs->cldv[i].h.host = strdup(hp->host);
 				cs->cldv[i].known = 1;
 				i++;
-			} else {
-				free(hp->host);
 			}
-			free(hp);
 		}
 
-		g_list_free(host_list);
+		cs->host_list = host_list;
 	}
 
 	if (pipe(cs->event_pipe) < 0) {
cld/cld.h +22 -0
index b1f9bbf..c5c38b5 100644
@@ -44,6 +44,15 @@ enum {
 	SFL_FOREGROUND		= (1 << 0),	/* run in foreground */
 };
 
+enum st_cldb {
+	ST_CLDB_INIT,
+	ST_CLDB_OPEN,
+	ST_CLDB_ACTIVE,
+	ST_CLDB_MASTER,
+	ST_CLDB_SLAVE,
+	ST_CLDBNUM
+};
+
 struct atcp_read {
 	void			*buf;
 	unsigned int		buf_size;
@@ -142,6 +151,19 @@ struct server {
 
 	struct event		chkpt_timer;	/* db4 checkpoint timer */
 
+	unsigned short		rep_port;	/* db4 replication port */
+
+	char			*myhost;
+	char			*force_myhost;
+	GList			*rep_remotes;
+
+	unsigned int		n_peers;	/* total peers in cell */
+
+	int			ev_pipe[2];	/* internal event pipe */
+	struct event		pipe_ev;
+
+	enum st_cldb		state_cldb;	/* db & replication state */
+
 	struct server_stats	stats;		/* global statistics */
 };
 
cld/cldb.c +61 -8

119 lines not on this page · raw message

cld/cldb.h +8 -1
index 0d8e618..0fec486 100644
@@ -104,6 +104,11 @@ enum db_event {
 	CLDB_EV_NONE, CLDB_EV_CLIENT, CLDB_EV_MASTER, CLDB_EV_ELECTED
 };
 
+struct db_remote {	/* remotes for cldb_init */
+	char		*host;
+	unsigned short	port;
+};
+
 struct cldb {
 	bool		is_master;
 	bool		keyed;			/* using encryption? */
@@ -130,7 +135,9 @@ struct cldb {
 
 extern int cldb_init(struct cldb *cldb, const char *db_home, const char *db_password,
 	      unsigned int env_flags, const char *errpfx, bool do_syslog,
-	      unsigned int flags, void (*cb)(enum db_event));
+	      GList *remotes, char *rep_host, unsigned short rep_port,
+	      int n_peers, void (*cb)(enum db_event));
+extern int cldb_up(struct cldb *cldb, unsigned int flags);
 extern void cldb_down(struct cldb *cldb);
 extern void cldb_fini(struct cldb *cldb);
 
cld/cldbadm.c +4 -2
index 7176f3b..1c0f26a 100644
@@ -78,7 +78,8 @@ int main(int argc, char *argv[])
 	}
 
 	if (cldb_init(&cld_adm.cldb, cld_adm.data_dir, NULL,
-		      DB_RECOVER, "cldbadm", false, 0, NULL))
+		      DB_RECOVER, "cldbadm", false,
+		      NULL, NULL, 0, 0, NULL))
 		goto err_dbopen;
 
 	switch (cld_adm.mode) {
@@ -143,7 +144,8 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
  */
 int cldb_init(struct cldb *cldb, const char *db_home, const char *db_password,
 	      unsigned int env_flags, const char *errpfx, bool do_syslog,
-	      unsigned int flags, void (*cb)(enum db_event))
+	      GList *remotes, char *rep_host, unsigned short rep_port,
+	      int n_peers, void (*cb)(enum db_event))
 {
 
 	return 0;
cld/server.c +293 -20

429 lines not on this page · raw message

include/cldc.h +1 -2
index f98d151..c4f0a64 100644
@@ -225,8 +225,7 @@ extern void cldc_copts_get_metadata(const struct cldc_call_opts *copts,
 
 /* cldc-tcp */
 extern void cldc_tcp_free(struct cldc_tcp *tcp);
-extern int cldc_tcp_new(const char *hostname, int port,
-		 struct cldc_tcp **tcp_out);
+extern int cldc_tcp_new(GList *host_list, struct cldc_tcp **tcp_out);
 extern int cldc_tcp_receive_pkt_data(struct cldc_tcp *tcp);
 extern int cldc_tcp_pkt_send(void *private,
 			  const void *addr, size_t addrlen,
include/ncld.h +1 -2
index 21b6e36..e46a4e7 100644
@@ -30,7 +30,6 @@
 #include <cldc.h>
 
 struct ncld_sess {
-	char			*host;
 	unsigned short		port;
 	GMutex			*mutex;
 	GCond			*cond;
@@ -71,7 +70,7 @@ struct ncld_read {
 	int		errc;
 };
 
-extern struct ncld_sess *ncld_sess_open(const char *host, int port,
+extern struct ncld_sess *ncld_sess_open(GList *host_list,
 	int *error, void (*event)(void *, unsigned int), void *ev_arg,
 	const char *cld_user, const char *cld_key, struct hail_log *log);
 extern struct ncld_fh *ncld_open(struct ncld_sess *s, const char *fname,
lib/cldc-tcp.c +20 -1
index 63a753b..96d78e2 100644
@@ -42,7 +42,7 @@ void cldc_tcp_free(struct cldc_tcp *tcp)
 	free(tcp);
 }
 
-int cldc_tcp_new(const char *hostname, int port,
+static int cldc_tcp_new_cxn(const char *hostname, int port,
 		 struct cldc_tcp **tcp_out)
 {
 	struct cldc_tcp *tcp;
@@ -99,6 +99,25 @@ int cldc_tcp_new(const char *hostname, int port,
 	return 0;
 }
 
+int cldc_tcp_new(GList *host_list, struct cldc_tcp **tcp_out)
+{
+	struct cldc_host *hp;
+	GList *tmp;
+	int rc = -ENOENT;
+
+	tmp = host_list;
+	while (tmp) {
+		hp = tmp->data;
+		tmp = tmp->next;
+
+		rc = cldc_tcp_new_cxn(hp->host, hp->port, tcp_out);
+		if (rc == 0)
+			break;
+	}
+
+	return rc;
+}
+
 int cldc_tcp_receive_pkt_data(struct cldc_tcp *tcp)
 {
 	static char buf[CLD_RAW_MSG_SZ];	/* BUG: static buf */
lib/cldc.c +5 -17
index f0c3b59..8d99bf2 100644
@@ -1326,6 +1326,7 @@ char *cldc_dirent_name(struct cld_dirent_cur *dc)
 	return s;
 }
 
+#if 0
 /*
  * On error, return the code (not negated code like a kernel function would).
  */
@@ -1374,6 +1375,7 @@ static int ncld_gethost(char **hostp, unsigned short *portp,
 
 	return 0;
 }
+#endif /* 0 */
 
 static void ncld_tcp_timer_event(struct cld_timer *timer)
 {
@@ -1600,8 +1602,7 @@ static int ncld_wait_session(struct ncld_sess *nsess)
  * this function returns, with the session going down, for example.
  * This is kind of dirty, but oh well. Maybe we'll fix this later.
  *
- * @param host Host name (NULL if resolving SRV records)
- * @param port Port
+ * @param host_list Host list
  * @param error Buffer for the error code
  * @param ev_func Session event function (ok to be NULL)
  * @param ev_arg User-supplied argument to the session event function
@@ -1609,7 +1610,7 @@ static int ncld_wait_session(struct ncld_sess *nsess)
  * @param cld_key The user key to be used to authentication
  * @param log The application log descriptor (ok to be NULL)
  */
-struct ncld_sess *ncld_sess_open(const char *host, int port, int *error,
+struct ncld_sess *ncld_sess_open(GList *host_list, int *error,
 				 void (*ev_func)(void *, unsigned int),
 				 void *ev_arg,
 				 const char *cld_user, const char *cld_key,
@@ -1643,16 +1644,6 @@ struct ncld_sess *ncld_sess_open(const char *host, int port, int *error,
 	if (!nsess->cond)
 		goto out_cond;
 
-	if (!host) {
-		err = ncld_getsrv(&nsess->host, &nsess->port, log);
-		if (err)
-			goto out_srv;
-	} else {
-		err = ncld_gethost(&nsess->host, &nsess->port, host, port);
-		if (err)
-			goto out_srv;
-	}
-
 	nsess->event = ev_func;
 	nsess->event_arg = ev_arg;
 
@@ -1661,7 +1652,7 @@ struct ncld_sess *ncld_sess_open(const char *host, int port, int *error,
 		goto out_pipe_to;
 	}
 
-	if (cldc_tcp_new(nsess->host, nsess->port, &nsess->tcp)) {
+	if (cldc_tcp_new(host_list, &nsess->tcp)) {
 		err = 1023;
 		goto out_tcp;
 	}
@@ -1704,8 +1695,6 @@ out_tcp:
 	close(nsess->to_thread[0]);
 	close(nsess->to_thread[1]);
 out_pipe_to:
-	free(nsess->host);
-out_srv:
 	g_cond_free(nsess->cond);
 out_cond:
 	g_mutex_free(nsess->mutex);
@@ -2273,7 +2262,6 @@ void ncld_sess_close(struct ncld_sess *nsess)
 	close(nsess->to_thread[1]);
 	g_cond_free(nsess->cond);
 	g_mutex_free(nsess->mutex);
-	free(nsess->host);
 	free(nsess);
 }
 
tools/cldcli.c +1 -3
index d347bf3..5321986 100644
@@ -682,7 +682,6 @@ static void prompt(void)
 int main (int argc, char *argv[])
 {
 	char linebuf[CLD_PATH_MAX + 1];
-	struct cldc_host *dr;
 	error_t aprc;
 	int error;
 
@@ -720,9 +719,8 @@ int main (int argc, char *argv[])
 
 	printf("Waiting for session startup...\n");
 	fflush(stdout);
-	dr = host_list->data;
 
-	nsess = ncld_sess_open(dr->host, dr->port, &error, sess_event, NULL,
+	nsess = ncld_sess_open(host_list, &error, sess_event, NULL,
 			     our_user, our_user, &cli_log);
 	if (!nsess) {
 		if (error < 1000) {
tools/cldfuse.c +1 -4
index 2cba917..395f4a3 100644
@@ -348,7 +348,6 @@ static int cldfuse_process_arg(void *data, const char *arg, int key,
 int main(int argc, char *argv[])
 {
 	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
-	struct cldc_host *dr;
 	int error;
 
 	if (fuse_opt_parse(&args, NULL, cldfuse_opts, cldfuse_process_arg)) {
@@ -372,9 +371,7 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	dr = param.host_list->data;
-
-	sess = ncld_sess_open(dr->host, dr->port, &error, sess_event, NULL,
+	sess = ncld_sess_open(param.host_list, &error, sess_event, NULL,
 			     "cldfuse", "cldfuse", &cldfuse_log);
 	if (!sess) {
 		if (error < 1000) {
#3 lore raw

Re: [PATCH 2/3] CLD: switch network proto from UDP to TCP

In reply to Jeff Garzik

lib/cldc-tcp.c line 175

Quoted 11 lines · On Fri, 31 Dec 2010 05:57:28 -0500 Jeff Garzik <[email protected]> wrote:
lib/cldc-tcp.c new file +11 -0
+	struct cldc_tcp *tcp = private;
+	ssize_t rc;
+	struct ubbp_header ubbp;
+
+	memcpy(ubbp.magic, "CLD1", 4);
+	ubbp.op_size = (buflen << 8) | 1;
+#ifdef WORDS_BIGENDIAN
+	swab32(ubbp.op_size);
+#endif
+
+	rc = write(tcp->fd, &ubbp, sizeof(ubbp));

Why not this:

	unsigned int n;

	n = (buflen << 8) | 1;
	ubbp.op_size = GUINT32_TO_LE(n);

-- P

#4 lore raw

Re: [PATCH 2/3] CLD: switch network proto from UDP to TCP

Quoted 17 lines · Pete Zaitcev

lib/cldc-tcp.c line 175

Quoted 11 lines · On Fri, 31 Dec 2010 05:57:28 -0500 Jeff Garzik <[email protected]> wrote:
lib/cldc-tcp.c new file +11 -0
+	struct cldc_tcp *tcp = private;
+	ssize_t rc;
+	struct ubbp_header ubbp;
+
+	memcpy(ubbp.magic, "CLD1", 4);
+	ubbp.op_size = (buflen << 8) | 1;
+#ifdef WORDS_BIGENDIAN
+	swab32(ubbp.op_size);
+#endif
+
+	rc = write(tcp->fd, &ubbp, sizeof(ubbp));

Why not this:

Quoted 4 lines
	unsigned int n;

	n = (buflen << 8) | 1;
	ubbp.op_size = GUINT32_TO_LE(n);

Nice.
Avoiding those pesky in-function #ifdefs makes the code more readable.

IMHO, this is kinder still on the eyes of reviewers, since the types of "n" and "rc" stay even closer to each definition/first-use:

  	unsigned int n = (buflen << 8) | 1;
  	ubbp.op_size = GUINT32_TO_LE(n);
        ssize_t rc = write(tcp->fd, &ubbp, sizeof(ubbp));

But if your coding guidelines require the all-vars-decl'd-at-top style (seriously anachronistic and more prone to merge conflicts), then I guess that's not an option... Though you may want to reconsider the policy, if your goal is portability:

For the record, compilers that reject decl-after-stmt are not relevant any more. At least, there are so few that I've been able to keep that sort of construct in coreutils for the past several years. I used to maintain a c99-to-c89 patch, but removed even that almost two years ago.

#5 lore raw

Re: [PATCH 2/3] CLD: switch network proto from UDP to TCP

In reply to Pete Zaitcev
Quoted 17 lines · Pete Zaitcev

lib/cldc-tcp.c

Quoted 11 lines · On Fri, 31 Dec 2010 05:57:28 -0500 Jeff Garzik<[email protected]> wrote:
lib/cldc-tcp.c new file +11 -0
+	struct cldc_tcp *tcp = private;
+	ssize_t rc;
+	struct ubbp_header ubbp;
+
+	memcpy(ubbp.magic, "CLD1", 4);
+	ubbp.op_size = (buflen<<  8) | 1;
+#ifdef WORDS_BIGENDIAN
+	swab32(ubbp.op_size);
+#endif
+
+	rc = write(tcp->fd,&ubbp, sizeof(ubbp));

Why not this:

Quoted 4 lines
	unsigned int n;

	n = (buflen<<  8) | 1;
	ubbp.op_size = GUINT32_TO_LE(n);

Yep.

I used the #ifdef on the read(2) side, where I did not want to create an additional var... then I copied that onto the write(2) side, where it is less efficient as you point out.

Jeff