Proposed RPC design for Windows CCAPI clients and server

The proposal is for a single user; the solution is replicated for each user logged onto the PC.

Conventions & clarifications

"Client" and "server" refer to the CCAPI client and server.

The CCAPI client acts as both an RPC client and RPC server and the CCAPI server acts as both an RPC client and RPC server.

The Windows username is referred to below as "<USER>."

The Windows Logon Security Identifier is referred to as "<LSID>."

<UUID> means a thread-specific UUID.

<SST> means server start time, a time_t.

A description of client and server authentication has not been added yet.

Design Requirements

Design

The server and each client create an RPC endpoint. The server's endpoint is CCS_<LSID> and the client's endpoint is CCAPI_<UUID>, where each client geta a UUID.

On Windows, the server's ccs_pipe_t type is a char* and is set to the client UUID.

How is the request handled in the server and the reply sent to the client?

One straightforward way is for the reply to be the returned data in the request RPC call (an [out] parameter). That is, data passed from the RPC server to the RPC client. The request handler calls ccs_server_handle_request. Eventually, the server code calls ccs_os_server_send_reply, which saves the reply somewhere. When the server eventually returns to the request handler, the handler returns the saved reply to the client.

But this doesn't work. If two clients A and B ask for the same lock, A will acquire the lock and B will have to wait. But if the single threaded server waits for B's lock, it will never handle A's unlock message. Therefore the server must return to B's request handler and not send a reply to B. So this method will not work.

Instead, there are listener and worker threads in Windows-specific code.

The client's cci_os_ipc function waits for ccs_reply. The client sends the request, including it's UUID, from which the server can construct the endpoint on which to call ccs_reply.

The server's listener thread listens for RPC requests. The request handler puts each request/reply endpoint in a queue and returns to the client.

The server's worker thread removes items from the queue, calls ccs_server_handle_request. ccs_server_handle_request takes both the request data and the client UUID . Eventually ccs_os_server_send_reply is called, with the reply data and client UUID in the reply_pipe. ccs_os_server_send_reply calls ccs_reply on the client's endpoint, which sends the reply to the client.

Is there any security issue with the client listening for RPC calls from the server?

Connections

If the client wants state to be maintained on the server, the client creates a connection. When the connection is closed, the server cleans up any state associated with the connection.

Any given thread in an application process could want to create a connection. When cci_ipc_thread_init is called, the connection thread-local variables are initialized. New connections are created when cci_os_ipc() (via _cci_ipc_send) is called and no connection was previously established. Basically we lazily establish connections so the client doesn't talk to the server until it has to.

Detecting client exit

The server must be able to detect when clients disappear, so the server can free any resources that had been held for the client.

The Windows RPC API does not appear to provide a notification for an endpoint disappearing. It does provide a way to ask if an endpoint is listening. This is useful for polling, but we want a better performing solution than that.

The client has an isAlive function on its endpoint.

To detect the client disappearing without using polling, the server makes an asynchronous call to the isAlive function on the client's endpoint. The isAlive function never returns. When the client exits for any reason, it's endpoint will be closed and the server's function call will return an error. The asynchronous call on the server means no additional threads are used.

Windows provides a number of notification methods to signal I/O completion. Among them are I/O completion ports and callback functions. I chose callback functions because they appear to consume fewer resources.

RPC Endpoint / Function summary

Windows-specific implementation details

Client CCAPI library initialization:

This code runs when the CCAPI DLL is loaded.

Client initialization:

This code runs when cci_os_ipc_thread_init is called:

Server initialization:

[old]

[new]

Establishing a connection:

Client request:

The server's reply to the client's request is not synchronous.

Detecting client exit

Detecting server exit

 

------
Stop:
Start:

/a> 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <pthread.h>

#include "glusterfs.h"
#include "globals.h"
#include "xlator.h"
#include "mem-pool.h"
#include "syncop.h"
#include "libglusterfs-messages.h"
#include "upcall-utils.h"

const char *gf_fop_list[GF_FOP_MAXVALUE] = {
        [GF_FOP_NULL]        = "NULL",
        [GF_FOP_STAT]        = "STAT",
        [GF_FOP_READLINK]    = "READLINK",
        [GF_FOP_MKNOD]       = "MKNOD",
        [GF_FOP_MKDIR]       = "MKDIR",
        [GF_FOP_UNLINK]      = "UNLINK",
        [GF_FOP_RMDIR]       = "RMDIR",
        [GF_FOP_SYMLINK]     = "SYMLINK",
        [GF_FOP_RENAME]      = "RENAME",
        [GF_FOP_LINK]        = "LINK",
        [GF_FOP_TRUNCATE]    = "TRUNCATE",
        [GF_FOP_OPEN]        = "OPEN",
        [GF_FOP_READ]        = "READ",
        [GF_FOP_WRITE]       = "WRITE",
        [GF_FOP_STATFS]      = "STATFS",
        [GF_FOP_FLUSH]       = "FLUSH",
        [GF_FOP_FSYNC]       = "FSYNC",
        [GF_FOP_SETXATTR]    = "SETXATTR",
        [GF_FOP_GETXATTR]    = "GETXATTR",
        [GF_FOP_REMOVEXATTR] = "REMOVEXATTR",
        [GF_FOP_OPENDIR]     = "OPENDIR",
        [GF_FOP_FSYNCDIR]    = "FSYNCDIR",
        [GF_FOP_ACCESS]      = "ACCESS",
        [GF_FOP_CREATE]      = "CREATE",
        [GF_FOP_FTRUNCATE]   = "FTRUNCATE",
        [GF_FOP_FSTAT]       = "FSTAT",
        [GF_FOP_LK]          = "LK",
        [GF_FOP_LOOKUP]      = "LOOKUP",
        [GF_FOP_READDIR]     = "READDIR",
        [GF_FOP_INODELK]     = "INODELK",
        [GF_FOP_FINODELK]    = "FINODELK",
        [GF_FOP_ENTRYLK]     = "ENTRYLK",
        [GF_FOP_FENTRYLK]    = "FENTRYLK",
        [GF_FOP_XATTROP]     = "XATTROP",
        [GF_FOP_FXATTROP]    = "FXATTROP",
        [GF_FOP_FSETXATTR]   = "FSETXATTR",
        [GF_FOP_FGETXATTR]   = "FGETXATTR",
        [GF_FOP_RCHECKSUM]   = "RCHECKSUM",
        [GF_FOP_SETATTR]     = "SETATTR",
        [GF_FOP_FSETATTR]    = "FSETATTR",
        [GF_FOP_READDIRP]    = "READDIRP",
        [GF_FOP_GETSPEC]     = "GETSPEC",
        [GF_FOP_FORGET]      = "FORGET",
        [GF_FOP_RELEASE]     = "RELEASE",
        [GF_FOP_RELEASEDIR]  = "RELEASEDIR",
        [GF_FOP_FREMOVEXATTR]= "FREMOVEXATTR",
	[GF_FOP_FALLOCATE]   = "FALLOCATE",
	[GF_FOP_DISCARD]     = "DISCARD",
        [GF_FOP_ZEROFILL]    = "ZEROFILL",
        [GF_FOP_IPC]         = "IPC",
        [GF_FOP_SEEK]        = "SEEK",
        [GF_FOP_LEASE]       = "LEASE",
        [GF_FOP_COMPOUND]    = "COMPOUND",
        [GF_FOP_GETACTIVELK] = "GETACTIVELK",
        [GF_FOP_SETACTIVELK] = "SETACTIVELK",
        [GF_FOP_PUT]         = "PUT",
        [GF_FOP_ICREATE]     = "ICREATE",
        [GF_FOP_NAMELINK]    = "NAMELINK",
};

const char *gf_upcall_list[GF_UPCALL_FLAGS_MAXVALUE] = {
        [GF_UPCALL_NULL] = "NULL",
        [GF_UPCALL] = "UPCALL",
        [GF_UPCALL_CI_STAT] = "CI_IATT",
        [GF_UPCALL_CI_XATTR] = "CI_XATTR",
        [GF_UPCALL_CI_RENAME] = "CI_RENAME",
        [GF_UPCALL_CI_NLINK] = "CI_UNLINK",
        [GF_UPCALL_CI_FORGET] = "CI_FORGET",
        [GF_UPCALL_LEASE_RECALL] = "LEASE_RECALL",
};

/* THIS */

/* This global ctx is a bad hack to prevent some of the libgfapi crashes.
 * This should be removed once the patch on resource pool is accepted
 */
glusterfs_ctx_t *global_ctx = NULL;
pthread_mutex_t global_ctx_mutex = PTHREAD_MUTEX_INITIALIZER;
xlator_t global_xlator;
static pthread_key_t this_xlator_key;
static pthread_key_t synctask_key;
static pthread_key_t uuid_buf_key;
static char          global_uuid_buf[GF_UUID_BUF_SIZE];
static pthread_key_t lkowner_buf_key;
static char          global_lkowner_buf[GF_LKOWNER_BUF_SIZE];
static pthread_key_t leaseid_buf_key;
static int gf_global_mem_acct_enable = 1;
static pthread_once_t globals_inited = PTHREAD_ONCE_INIT;


int
gf_global_mem_acct_enable_get (void)
{
	return gf_global_mem_acct_enable;
}

int
gf_global_mem_acct_enable_set (int val)
{
	gf_global_mem_acct_enable = val;
	return 0;
}

void
glusterfs_this_destroy (void *ptr)
{
        FREE (ptr);
}

static struct xlator_cbks global_cbks = {
        .forget                 = NULL,
        .release                = NULL,
        .releasedir             = NULL,
        .invalidate             = NULL,
        .client_destroy         = NULL,
        .client_disconnect      = NULL,
        .ictxmerge              = NULL,
        .ictxsize               = NULL,
        .fdctxsize              = NULL,
};

/* This is required to get through the check in graph.c */
static struct xlator_fops global_fops = {
};

static int
global_xl_reconfigure (xlator_t *this, dict_t *options)
{
        int ret = -1;
        gf_boolean_t bool_opt = _gf_false;

        /* This is not added in volume dump, hence adding the options in log
           would be helpful for debugging later */
        dict_dump_to_log (options);

        GF_OPTION_RECONF ("measure-latency", bool_opt, options, bool, out);
        this->ctx->measure_latency = bool_opt;

        GF_OPTION_RECONF ("metrics-dump-path", this->ctx->config.metrics_dumppath,
                          options, str, out);

        /* TODO: add more things here */
        ret = 0;
out:
        return ret;
}

static int
global_xl_init (xlator_t *this)
{
        int ret = -1;
        gf_boolean_t bool_opt = false;

        GF_OPTION_INIT ("measure-latency", bool_opt, bool, out);
        this->ctx->measure_latency = bool_opt;

        GF_OPTION_INIT ("metrics-dump-path", this->ctx->config.metrics_dumppath,
                        str, out);

        ret = 0;

out:
        return ret;
}

static void
global_xl_fini (xlator_t *this)
{
        return;
}

struct volume_options global_xl_options[] = {
        { .key   = {"measure-latency"},
          .type  = GF_OPTION_TYPE_BOOL,
          .default_value = "no",
          .op_version = {GD_OP_VERSION_4_0_0},
          .flags = OPT_FLAG_SETTABLE,
          .tags = {"global", "context"},
          .description = "Use this option to toggle measuring latency"
        },
        { .key   = {"metrics-dump-path"},
          .type  = GF_OPTION_TYPE_STR,
          .default_value = "{{gluster_workdir}}/metrics",
          .op_version = {GD_OP_VERSION_4_0_0},
          .flags = OPT_FLAG_SETTABLE,
          .tags = {"global", "context"},
          .description = "Use this option to set the metrics dump path"
        },

        { .key = {NULL},},
};

static volume_opt_list_t global_xl_opt_list;

int
glusterfs_this_init ()
{
        int ret = 0;
        ret = pthread_key_create (&this_xlator_key, glusterfs_this_destroy);
        if (ret != 0) {
                gf_msg ("", GF_LOG_WARNING, ret,
                        LG_MSG_PTHREAD_KEY_CREATE_FAILED, "failed to create "
                        "the pthread key");
                return ret;
        }

        global_xlator.name = "glusterfs";
        global_xlator.type = GF_GLOBAL_XLATOR_NAME;
        global_xlator.cbks = &global_cbks;
        global_xlator.fops = &global_fops;
        global_xlator.reconfigure = global_xl_reconfigure;
        global_xlator.init = global_xl_init;
        global_xlator.fini = global_xl_fini;

        INIT_LIST_HEAD (&global_xlator.volume_options);
        INIT_LIST_HEAD (&global_xl_opt_list.list);
        global_xl_opt_list.given_opt = global_xl_options;

        list_add_tail (&global_xl_opt_list.list, &global_xlator.volume_options);

        return ret;
}


xlator_t **
__glusterfs_this_location ()
{
        xlator_t **this_location = NULL;
        int        ret = 0;

        this_location = pthread_getspecific (this_xlator_key);

        if (!this_location) {
                this_location = CALLOC (1, sizeof (*this_location));
                if (!this_location)
                        goto out;

                ret = pthread_setspecific (this_xlator_key, this_location);
                if (ret != 0) {
                        FREE (this_location);
                        this_location = NULL;
                        goto out;
                }
        }
out:
        if (this_location) {
                if (!*this_location)
                        *this_location = &global_xlator;
        }
        return this_location;
}


xlator_t *
glusterfs_this_get ()
{
        xlator_t **this_location = NULL;

        this_location = __glusterfs_this_location ();
        if (!this_location)
                return &global_xlator;

        return *this_location;
}


int
glusterfs_this_set (xlator_t *this)
{
        xlator_t **this_location = NULL;

        this_location = __glusterfs_this_location ();
        if (!this_location)
                return -ENOMEM;

        *this_location = this;

        return 0;
}

/* SYNCOPCTX */
static pthread_key_t syncopctx_key;

static void
syncopctx_key_destroy (void *ptr)
{
	struct syncopctx *opctx = ptr;

	if (opctx) {
		if (opctx->groups)
			GF_FREE (opctx->groups);

		GF_FREE (opctx);
	}

	return;
}

void *
syncopctx_getctx ()
{
	void *opctx = NULL;

	opctx = pthread_getspecific (syncopctx_key);

	return opctx;
}

int
syncopctx_setctx (void *ctx)
{
	int ret = 0;

	ret = pthread_setspecific (syncopctx_key, ctx);

	return ret;
}

static int
syncopctx_init (void)
{
	int ret;

	ret = pthread_key_create (&syncopctx_key, syncopctx_key_destroy);

	return ret;
}

/* SYNCTASK */

int
synctask_init ()
{
        int  ret = 0;

        ret = pthread_key_create (&synctask_key, NULL);

        return ret;
}

void *
synctask_get ()
{
        void   *synctask = NULL;

        synctask = pthread_getspecific (synctask_key);

        return synctask;
}


int
synctask_set (void *synctask)
{
        int     ret = 0;

        pthread_setspecific (synctask_key, synctask);

        return ret;
}

//UUID_BUFFER

void
glusterfs_uuid_buf_destroy (void *ptr)
{
        FREE (ptr);
}

int
glusterfs_uuid_buf_init ()
{
        int ret = 0;

        ret = pthread_key_create (&uuid_buf_key,
                                  glusterfs_uuid_buf_destroy);
        return ret;
}

char *
glusterfs_uuid_buf_get ()
{
        char *buf;
        int ret = 0;

        buf = pthread_getspecific (uuid_buf_key);
        if(!buf) {
                buf = MALLOC (GF_UUID_BUF_SIZE);
                ret = pthread_setspecific (uuid_buf_key, (void *) buf);
                if (ret)
                        buf = global_uuid_buf;
        }
        return buf;
}

/* LKOWNER_BUFFER */

void
glusterfs_lkowner_buf_destroy (void *ptr)
{
        FREE (ptr);
}

int
glusterfs_lkowner_buf_init ()
{
        int ret = 0;

        ret = pthread_key_create (&lkowner_buf_key,
                                  glusterfs_lkowner_buf_destroy);
        return ret;
}

char *
glusterfs_lkowner_buf_get ()
{
        char *buf;
        int ret = 0;

        buf = pthread_getspecific (lkowner_buf_key);
        if(!buf) {
                buf = MALLOC (GF_LKOWNER_BUF_SIZE);
                ret = pthread_setspecific (lkowner_buf_key, (void *) buf);
                if (ret)
                        buf = global_lkowner_buf;
        }
        return buf;
}

/* Leaseid buffer */
void
glusterfs_leaseid_buf_destroy (void *ptr)
{
        FREE (ptr);
}

int
glusterfs_leaseid_buf_init ()
{
        int ret = 0;

        ret = pthread_key_create (&leaseid_buf_key,
                                  glusterfs_leaseid_buf_destroy);
        return ret;
}

char *
glusterfs_leaseid_buf_get ()
{
        char *buf = NULL;
        int   ret = 0;

        buf = pthread_getspecific (leaseid_buf_key);
        if (!buf) {
                buf = CALLOC (1, GF_LEASE_ID_BUF_SIZE);
                ret = pthread_setspecific (leaseid_buf_key, (void *) buf);
                if (ret) {
                        FREE (buf);
                        buf = NULL;
                }
        }
        return buf;
}

char *
glusterfs_leaseid_exist ()
{
        return pthread_getspecific (leaseid_buf_key);
}

static void
gf_globals_init_once ()
{
        int ret = 0;

        ret = glusterfs_this_init ();
        if (ret) {
                gf_msg ("", GF_LOG_CRITICAL, 0, LG_MSG_TRANSLATOR_INIT_FAILED,
                        "ERROR: glusterfs-translator init failed");
                goto out;
        }

        ret = glusterfs_uuid_buf_init ();
        if(ret) {
                gf_msg ("", GF_LOG_CRITICAL, 0, LG_MSG_UUID_BUF_INIT_FAILED,
                        "ERROR: glusterfs uuid buffer init failed");
                goto out;
        }

        ret = glusterfs_lkowner_buf_init ();
        if(ret) {
                gf_msg ("", GF_LOG_CRITICAL, 0, LG_MSG_LKOWNER_BUF_INIT_FAILED,
                        "ERROR: glusterfs lkowner buffer init failed");
                goto out;
        }

        ret = glusterfs_leaseid_buf_init ();
        if (ret) {
                gf_msg ("", GF_LOG_CRITICAL, 0, LG_MSG_LEASEID_BUF_INIT_FAILED,