ctkgridlicense.c 74.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * nvidia-settings: A tool for configuring the NVIDIA X driver on Unix
 * and Linux systems.
 *
 * Copyright (C) 2017 NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * 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 <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <sys/stat.h> 
#include <sys/types.h>
#include <fcntl.h>
#include <dlfcn.h>

#include <gtk/gtk.h>
#include <NvCtrlAttributesPrivate.h>

#include "ctkutils.h"
#include "ctkhelp.h"
#include "ctkgridlicense.h"
#include "ctkbanner.h"
#include "nv_grid_dbus.h"
37 38 39 40
#include <gdk/gdkkeysyms.h>
#ifdef CTK_GTK3
#include <gdk/gdkkeysyms-compat.h>
#endif
41 42 43

#include <dbus/dbus.h>
#include <unistd.h>
44
#include <errno.h>
45 46 47 48 49 50

#define DEFAULT_UPDATE_GRID_LICENSE_STATUS_INFO_TIME_INTERVAL 1000
#define GRID_CONFIG_FILE            "/etc/nvidia/gridd.conf"
#define GRID_CONFIG_FILE_TEMPLATE   "/etc/nvidia/gridd.conf.template"

static const char * __manage_grid_licenses_help =
51
"Use the Manage GRID License page to obtain licenses "
52
"for GRID vGPU or Quadro Virtual Datacenter Workstation on supported Tesla products.";
53
static const char * __grid_virtual_workstation_help =
54
"Select this option to enable Quadro Virtual Datacenter Workstation license.";
55
static const char * __grid_vapp_help =
56
"Select this option to disable the Quadro Virtual Datacenter Workstation license.";
57
static const char * __license_edition_help =
58
"This section indicates the status of GRID licensing for the system.";
59
static const char * __license_server_help =
60
"Shows the configured GRID license server details.";
61
static const char * __primary_server_address_help =
62 63 64
"Enter the address of your local GRID license server. "
"The address can be a fully-qualified domain name such as gridlicense.example.com, "
"or an IP address such as 10.31.20.45.";
65
static const char * __primary_server_port_help =
66 67
"This field can be left empty, and will default to 7070, "
"which is the default port number used by the NVIDIA GRID license server.";
68
static const char * __secondary_server_help =
69 70 71
"This field is optional. Enter the address of your backup GRID license server. "
"The address can be a fully-qualified domain name such as backuplicense.example.com, "
"or an IP address such as 10.31.20.46.";
72
static const char * __secondary_server_port_help =
73 74
"This field can be left empty, and will default to 7070, "
"which is the default port number used by the NVIDIA GRID license server.";
75
static const char * __apply_button_help =
76
"Clicking the Apply button updates license settings in the gridd.conf file and "
77
"sends update license request to the NVIDIA GRID licensing daemon.";
78 79 80
static const char * __cancel_button_help =
"Clicking the Cancel button sets the text in all textboxes from the gridd.conf file. "
"Any changes you have done will be lost.";
81 82 83

typedef struct 
{
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    void                                                *dbusHandle;
    typeof(dbus_bus_get)                                *getDbus;
    typeof(dbus_error_init)                             *dbusErrorInit;
    typeof(dbus_error_is_set)                           *dbusErrorIsSet;
    typeof(dbus_error_free)                             *dbusErrorFree;
    typeof(dbus_bus_request_name)                       *dbusRequestName;
    typeof(dbus_message_new_method_call)                *dbusMessageNewMethodCall;
    typeof(dbus_message_iter_init)                      *dbusMessageIterInit;
    typeof(dbus_message_iter_init_append)               *dbusMessageIterInitAppend;
    typeof(dbus_message_iter_append_basic)              *dbusMessageIterAppendBasic;
    typeof(dbus_message_iter_get_arg_type)              *dbusMessageIterGetArgType;
    typeof(dbus_message_iter_get_basic)                 *dbusMessageIterGetBasic;
    typeof(dbus_message_unref)                          *dbusMessageUnref;
    typeof(dbus_connection_close)                       *dbusConnectionClose;
    typeof(dbus_connection_flush)                       *dbusConnectionFlush;
    typeof(dbus_connection_send_with_reply)             *dbusConnectionSendWithReply;
    typeof(dbus_connection_send_with_reply_and_block)   *dbusConnectionSendWithReplyAndBlock;
    typeof(dbus_pending_call_block)                     *dbusPendingCallBlock;
    typeof(dbus_pending_call_steal_reply)               *dbusPendingCallStealReply;
103 104 105 106 107 108 109 110
} GridDbus;

struct _DbusData
{
    DBusConnection *conn;
    GridDbus       dbus;
};

111
static void apply_clicked(GtkWidget *widget, gpointer user_data);
112
static void cancel_clicked(GtkWidget *widget, gpointer user_data);
113 114 115 116
static void ctk_manage_grid_license_finalize(GObject *object);
static void ctk_manage_grid_license_class_init(CtkManageGridLicenseClass *);
static void dbusClose(DbusData *dbusData);
static gboolean checkConfigfile(gboolean *writable);
117 118
static gboolean disallow_whitespace(GtkWidget *widget, GdkEvent *event, gpointer user_data);
static gboolean allow_digits(GtkWidget *widget, GdkEvent *event, gpointer user_data);
119
static gboolean enable_disable_ui_controls(GtkWidget *widget, GdkEvent *event, gpointer user_data);
120 121
static void update_gui_from_griddconfig(gpointer user_data);
static gboolean licenseStateQueryFailed = FALSE;
122 123
static void get_licensed_feature_code(gpointer user_data);
static gboolean is_restart_required(gpointer user_data);
124
static gboolean queryLicensedFeatureCode = TRUE;
125
int64_t licensedFeatureCode = NV_GRID_LICENSE_FEATURE_TYPE_VAPP;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

GType ctk_manage_grid_license_get_type(void)
{
    static GType ctk_manage_grid_license_type = 0;

    if (!ctk_manage_grid_license_type) {
        static const GTypeInfo ctk_manage_grid_license_info = {
            sizeof (CtkManageGridLicenseClass),
            NULL, /* base_init */
            NULL, /* base_finalize */
            (GClassInitFunc) ctk_manage_grid_license_class_init,
            NULL, /* class_finalize */
            NULL, /* class_data */
            sizeof (CtkManageGridLicense),
            0,    /* n_preallocs */
            NULL, /* instance_init */
            NULL  /* value_table */
        };

        ctk_manage_grid_license_type =
            g_type_register_static(GTK_TYPE_VBOX, "CtkManageGridLicense",
                                   &ctk_manage_grid_license_info, 0);
    }

    return ctk_manage_grid_license_type;

} /* ctk_manage_grid_license_get_type() */

typedef enum
{
    NV_GRIDD_SERVER_ADDRESS = 0,
    NV_GRIDD_SERVER_PORT,
    NV_GRIDD_FEATURE_TYPE,
    NV_GRIDD_ENABLE_UI,
160 161
    NV_GRIDD_BACKUP_SERVER_ADDRESS,
    NV_GRIDD_BACKUP_SERVER_PORT,
162 163 164 165
    NV_GRIDD_MAX_TOKENS
} CfgParams;

static const char *configParamsList[] = {
166 167 168 169 170 171
    [NV_GRIDD_SERVER_ADDRESS]        = "ServerAddress",
    [NV_GRIDD_SERVER_PORT]           = "ServerPort",
    [NV_GRIDD_FEATURE_TYPE]          = "FeatureType",
    [NV_GRIDD_ENABLE_UI]             = "EnableUI",
    [NV_GRIDD_BACKUP_SERVER_ADDRESS] = "BackupServerAddress",
    [NV_GRIDD_BACKUP_SERVER_PORT]    = "BackupServerPort",
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
};

typedef struct NvGriddConfigParamsRec
{
    char *str[NV_GRIDD_MAX_TOKENS];
} NvGriddConfigParams;

typedef struct ConfigFileLinesRec
{
    char **lines;
    int nLines;
} ConfigFileLines;

static void FreeConfigFileLines(ConfigFileLines *pLines)
{
    int i;

189 190 191 192
    if (pLines != NULL) {
        for (i = 0; i < pLines->nLines; i++) {
            nvfree(pLines->lines[i]);
        }
193

194 195 196
        nvfree(pLines->lines);
        nvfree(pLines);
    }
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
}

static ConfigFileLines *AllocConfigFileLines(void)
{
    return nvalloc(sizeof(ConfigFileLines));
}

static void AddLineToConfigFileLines(ConfigFileLines *pLines,
                                     char *line)
{
    pLines->lines = nvrealloc(pLines->lines, (pLines->nLines + 1) * sizeof(*pLines->lines));

    pLines->lines[pLines->nLines] = line;

    pLines->nLines++;
}


static void FreeNvGriddConfigParams(NvGriddConfigParams *griddConfig)
{
    int i;

219 220 221 222
    if (griddConfig != NULL) {
        for (i = 0; i < ARRAY_LEN(griddConfig->str); i++) {
            nvfree(griddConfig->str[i]);
        }
223

224 225
        nvfree(griddConfig);
    }
226 227 228 229 230 231
}

static NvGriddConfigParams *AllocNvGriddConfigParams(void)
{
    NvGriddConfigParams *griddConfig = nvalloc(sizeof(*griddConfig));

232
    griddConfig->str[NV_GRIDD_SERVER_ADDRESS]        = nvstrdup("");
233
    griddConfig->str[NV_GRIDD_SERVER_PORT]           = nvstrdup("");
234
    griddConfig->str[NV_GRIDD_FEATURE_TYPE]          = nvstrdup("0");
235
    griddConfig->str[NV_GRIDD_ENABLE_UI]             = nvstrdup("FALSE");
236 237
    griddConfig->str[NV_GRIDD_BACKUP_SERVER_ADDRESS] = nvstrdup("");
    griddConfig->str[NV_GRIDD_BACKUP_SERVER_PORT]    = nvstrdup("");
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

    return griddConfig;
}

/*
 * Return TRUE if 'line' begins with 'item='.
 */
static gboolean LineIsItem(const char *line, const char *item)
{
    const size_t itemLen = strlen(item);

    if (strlen(line) <= itemLen) {
        return FALSE;
    }
    if (strncmp(line, item, itemLen) != 0) {
        return FALSE;
    }
    if (line[itemLen] != '=') {
        return FALSE;
    }
    return TRUE;
}

/*
262 263
 * Update NvGriddConfigParams if the line from gridd.conf describes any
 * nvidia-gridd configuration parameters.
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
 */
static void UpdateGriddConfigFromLine(NvGriddConfigParams *griddConfig,
                                      const char *line)
{
    int item;
    char *tmpLine = remove_spaces(line);

    /* ignore comment lines */
    if (tmpLine[0] == '#') {
        goto done;
    }

    for (item = 0; item < ARRAY_LEN(configParamsList); item++) {

        char *p;
        const char *itemStr = configParamsList[item];

        /* continue if tmpLine does not start with "itemStr=" */
        if (!LineIsItem(tmpLine, itemStr)) {
            continue;
        }

        /* skip past "itemStr="; +1 is for '=' */
        p = tmpLine + strlen(itemStr) + 1;

        /* Empty string, skip parsing */
        if (*p == '\0') {
            continue;
        }

        nvfree(griddConfig->str[item]);
        griddConfig->str[item] = nvstrdup(p);
        goto done;
    }

    /*
     * The gridd.conf syntax supports tokens beyond those in
     * configParamsList[].  Just ignore lines we don't recognize.
     */

done:
    nvfree(tmpLine);
}

static void UpdateGriddConfigFromConfigFileLines(
                                   NvGriddConfigParams *griddConfig,
                                   const ConfigFileLines *pLines)
{
    int i;

    for (i = 0; i < pLines->nLines; i++) {
        UpdateGriddConfigFromLine(griddConfig, pLines->lines[i]);
    }
}

static char *AllocConfigLine(const NvGriddConfigParams *griddConfig,
                             CfgParams item)
{
    return nvstrcat(configParamsList[item], "=", griddConfig->str[item], NULL);
}

/*
326
 * Update the line from gridd.conf with information in NvGriddConfigParams.
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
 *
 * The old line is passed as argument, and the new line is returned.
 * If the line has to be reallocated, the old line is freed.  If the
 * old line does not need to be reallocated, then it is returned
 * as-is.  This allows callers to use it like this:
 *
 * pLines->lines[i] = UpdateLineWithGriddConfig(griddConfig, pLines->lines[i]);
 *
 * As a side effect, update the pItemIsPresent array, recording which items
 * are present when we find a match with line.
 */
static char *UpdateLineWithGriddConfig(const NvGriddConfigParams *griddConfig,
                                       char *line, gboolean *pItemIsPresent)
{
    int item;
    char *tmpLine = remove_spaces(line);

    /* ignore comment lines */
    if (tmpLine[0] == '#') {
        goto done;
    }

    for (item = 0; item < ARRAY_LEN(configParamsList); item++) {

        const char *itemStr = configParamsList[item];

        /* continue if tmpLine does not start with "itemStr=" */
        if (!LineIsItem(tmpLine, itemStr)) {
            continue;
        }

        pItemIsPresent[item] = TRUE;

        nvfree(tmpLine);
        nvfree(line);
        return AllocConfigLine(griddConfig, item);
    }

done:
    nvfree(tmpLine);
    return line;
}

static void UpdateConfigFileLinesFromGriddConfig(
                             const NvGriddConfigParams *griddConfig,
                             ConfigFileLines *pLines)
{
    gboolean itemIsPresent[NV_GRIDD_MAX_TOKENS] = { FALSE };
    int i;

    /* Update the lines in pLines */

    for (i = 0; i < pLines->nLines; i++) {
380 381 382
        /* TODO: "EnableUI" config parameter will never be
         * set by the user through UI, so skip updating the
         * pLines with "EnableUI" parameter */
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
        pLines->lines[i] = UpdateLineWithGriddConfig(griddConfig,
                                                     pLines->lines[i],
                                                     itemIsPresent);
    }

    /* Append any items not updated in the above loop. */    

    for (i = 0; i < ARRAY_LEN(configParamsList); i++) {

        if (itemIsPresent[i]) {
            continue;
        }

        AddLineToConfigFileLines(pLines, AllocConfigLine(griddConfig, i));
    }
}

static void UpdateGriddConfigFromGui(
                      NvGriddConfigParams *griddConfig,
                      CtkManageGridLicense *ctk_manage_grid_license)
{
    const char *tmp;

    /* serverAddress */

    tmp = gtk_entry_get_text(GTK_ENTRY(
                              ctk_manage_grid_license->txt_server_address));
410 411 412 413
    if (strcmp(tmp, griddConfig->str[NV_GRIDD_SERVER_ADDRESS]) != 0) {
        nvfree(griddConfig->str[NV_GRIDD_SERVER_ADDRESS]);
        griddConfig->str[NV_GRIDD_SERVER_ADDRESS] = nvstrdup(tmp ? tmp : "");
    }
414 415 416 417 418

    /* serverPort */

    tmp = gtk_entry_get_text(GTK_ENTRY(
                              ctk_manage_grid_license->txt_server_port));
419 420 421 422 423
    if (strcmp(tmp, griddConfig->str[NV_GRIDD_SERVER_PORT]) != 0) {
        nvfree(griddConfig->str[NV_GRIDD_SERVER_PORT]);
        griddConfig->str[NV_GRIDD_SERVER_PORT] =
            nvstrdup((strcmp(tmp, "") != 0) ? tmp : "7070");
    }
424 425 426

    /* featureType */

427
    switch (ctk_manage_grid_license->feature_type) {
428 429 430 431 432 433 434 435 436 437 438
        case NV_GRID_LICENSE_FEATURE_TYPE_VAPP:
            tmp = "0";
            break;
        case NV_GRID_LICENSE_FEATURE_TYPE_VGPU:
            tmp = "1";
            break;
        case NV_GRID_LICENSE_FEATURE_TYPE_QDWS:
            tmp = "2";
            break;
        default:
            tmp = "0";
439
    }
440 441 442 443
    if (strcmp(tmp, griddConfig->str[NV_GRIDD_FEATURE_TYPE]) != 0) {
        nvfree(griddConfig->str[NV_GRIDD_FEATURE_TYPE]);
        griddConfig->str[NV_GRIDD_FEATURE_TYPE] = nvstrdup(tmp);
    }
444 445

    /* note: nothing in the UI will alter enableUI */
446 447 448 449 450

    /* backupServerAddress */

    tmp = gtk_entry_get_text(GTK_ENTRY(
                              ctk_manage_grid_license->txt_secondary_server_address));
451 452 453 454
    if (strcmp(tmp, griddConfig->str[NV_GRIDD_BACKUP_SERVER_ADDRESS]) != 0) {
        nvfree(griddConfig->str[NV_GRIDD_BACKUP_SERVER_ADDRESS]);
        griddConfig->str[NV_GRIDD_BACKUP_SERVER_ADDRESS] = nvstrdup(tmp ? tmp : "");
    }
455 456 457 458 459

    /* backupServerPort */

    tmp = gtk_entry_get_text(GTK_ENTRY(
                              ctk_manage_grid_license->txt_secondary_server_port));
460 461 462 463
    if (strcmp(tmp, griddConfig->str[NV_GRIDD_BACKUP_SERVER_PORT]) != 0) {
        nvfree(griddConfig->str[NV_GRIDD_BACKUP_SERVER_PORT]);
        griddConfig->str[NV_GRIDD_BACKUP_SERVER_PORT] = nvstrdup(tmp ? tmp : "");
    }
464 465 466
}

/*
467
 * Read the nvidia-gridd config file specified by configFile, returning the
468 469
 * lines in ConfigFileLines.
 */
470
static ConfigFileLines *ReadConfigFileStream(FILE *configFile)
471 472 473 474 475 476 477 478 479 480 481
{
    ConfigFileLines *pLines;
    int eof = FALSE;
    char *line = NULL;

    pLines = AllocConfigFileLines();

    while ((line = fget_next_line(configFile, &eof)) && !eof) {
        AddLineToConfigFileLines(pLines, line);
    }

482 483 484
    return pLines;
}

485
static int ReadConfigFile(ConfigFileLines **ppLines)
486 487 488 489
{
    FILE *configFile = fopen(GRID_CONFIG_FILE, "r");

    if (configFile == NULL) {
490
        return errno;
491 492
    }

493
    *ppLines = ReadConfigFileStream(configFile);
494

495 496
    fclose(configFile);

497
    return 0;
498 499
}

500 501 502 503 504 505 506 507 508
static void WriteConfigFileStream(FILE *configFile, const ConfigFileLines *pLines)
{
    int i;

    for (i = 0; i < pLines->nLines; i++) {
        fprintf(configFile, "%s\n", pLines->lines[i]);
    }
}

509
static int WriteConfigFile(const ConfigFileLines *pLines)
510 511 512 513 514 515
{
    FILE *configFile;

    configFile = fopen(GRID_CONFIG_FILE, "w");

    if (configFile == NULL) {
516
        return errno;
517 518
    }

519
    WriteConfigFileStream(configFile, pLines);
520 521 522

    fclose(configFile);

523
    return 0;
524 525 526 527
}


/*
528
 * Update the nvidia-gridd config file with the current GUI state.
529 530
 *
 */
531
static int UpdateConfigFile(CtkManageGridLicense *ctk_manage_grid_license)
532
{
533 534 535
    ConfigFileLines *pLines = NULL;
    NvGriddConfigParams *griddConfig = NULL;
    int retErr;
536 537

    /* Read gridd.conf */
538 539 540
    retErr = ReadConfigFile(&pLines);
    if (retErr != 0) {
        goto done;
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    }

    /* Create a griddConfig */
    griddConfig = AllocNvGriddConfigParams();

    /* Update the griddConfig with the lines from gridd.conf */
    UpdateGriddConfigFromConfigFileLines(griddConfig, pLines);

    /* Update the griddConfig with the state of the nvidia-settings GUI */
    UpdateGriddConfigFromGui(griddConfig, ctk_manage_grid_license);

    /* Update the lines of gridd.conf with griddConfig */
    UpdateConfigFileLinesFromGriddConfig(griddConfig, pLines);

    /* Write the lines of gridd.conf to file */
556
    retErr = WriteConfigFile(pLines);
557

558
done:
559 560 561
    FreeConfigFileLines(pLines);
    FreeNvGriddConfigParams(griddConfig);

562
    return retErr;
563 564 565 566 567 568 569 570 571 572
}

/*
 * Create an NvGriddConfigParams by parsing the configuration file and
 * populating NvGriddConfigParams.  The caller should call
 * FreeNvGriddConfigParams() when done with it.
 *
 */
static NvGriddConfigParams *GetNvGriddConfigParams(void)
{
573 574 575
    ConfigFileLines *pLines = NULL;
    NvGriddConfigParams *griddConfig = NULL;
    int retErr;
576

577 578 579
    /* Create a griddConfig */
    griddConfig = AllocNvGriddConfigParams();

580
    /* Read gridd.conf */
581 582 583
    retErr = ReadConfigFile(&pLines);
    if (retErr != 0) {
        goto done;
584 585 586 587 588
    }

    /* Update the griddConfig with the lines from gridd.conf */
    UpdateGriddConfigFromConfigFileLines(griddConfig, pLines);

589
done:
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
    FreeConfigFileLines(pLines);

    return griddConfig;
}


static void ctk_manage_grid_license_class_init(CtkManageGridLicenseClass *ctk_object_class)
{
    GObjectClass *gobject_class = (GObjectClass *)ctk_object_class;
    gobject_class->finalize = ctk_manage_grid_license_finalize;
}


static void ctk_manage_grid_license_finalize(GObject *object)
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(object);

    dbusClose(ctk_manage_grid_license->dbusData);
}


/*
 * dlclose libdbus-1.so.3
 */
static void dbusClose(DbusData *dbusData)
{
    if (dbusData) {
        if (dbusData->dbus.dbusHandle) {
            dlclose(dbusData->dbus.dbusHandle);
        }
        nvfree(dbusData);
    }
}


/*
 * load D-Bus symbols from libdbus-1.so.3
 */
static gboolean dbusLoadSymbols(DbusData *dbusData)
{
    const char *missingSym = " ";
    
    dbusData->dbus.dbusHandle = dlopen("libdbus-1.so.3", RTLD_LAZY);
    
    if (!dbusData->dbus.dbusHandle)
    {
        nv_error_msg("Couldn't open libdbus-1.so.3");
        return FALSE;
    }

#define LOAD_SYM(name, sym)                                      \
    dbusData->dbus.name = dlsym(dbusData->dbus.dbusHandle, sym); \
    if (!dbusData->dbus.name) {                                  \
        missingSym = sym;                                        \
        goto dbus_end;                                           \
    }
    LOAD_SYM(getDbus, "dbus_bus_get");
    LOAD_SYM(dbusErrorInit, "dbus_error_init");
    LOAD_SYM(dbusErrorIsSet, "dbus_error_is_set");
    LOAD_SYM(dbusErrorFree, "dbus_error_free");
    LOAD_SYM(dbusRequestName, "dbus_bus_request_name");
    LOAD_SYM(dbusMessageNewMethodCall, "dbus_message_new_method_call");
    LOAD_SYM(dbusMessageIterInit, "dbus_message_iter_init");
    LOAD_SYM(dbusMessageIterInitAppend, "dbus_message_iter_init_append");
    LOAD_SYM(dbusMessageIterAppendBasic, "dbus_message_iter_append_basic");
    LOAD_SYM(dbusMessageIterGetArgType, "dbus_message_iter_get_arg_type");
    LOAD_SYM(dbusMessageIterGetBasic, "dbus_message_iter_get_basic");
    LOAD_SYM(dbusMessageUnref, "dbus_message_unref");
    LOAD_SYM(dbusConnectionClose, "dbus_connection_close");
    LOAD_SYM(dbusConnectionFlush, "dbus_connection_flush");
    LOAD_SYM(dbusConnectionSendWithReply, "dbus_connection_send_with_reply");
661
    LOAD_SYM(dbusConnectionSendWithReplyAndBlock, "dbus_connection_send_with_reply_and_block");
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    LOAD_SYM(dbusPendingCallBlock, "dbus_pending_call_block");
    LOAD_SYM(dbusPendingCallStealReply, "dbus_pending_call_steal_reply");
#undef LOAD_SYM

    return TRUE;

dbus_end:
    nv_error_msg("Required libdbus symbol %s was not found", missingSym);
    dbusClose(dbusData);

    return FALSE;
}


/*
677 678 679 680 681
 * send_message_to_gridd() - This function is for communication between
 * nvidia-gridd and nvidia-settings using DBus.
 * Nvidia-settings sends different command messages to either query info
 * such as license state or to notify nvidia-gridd to update license
 * info changes.
682
 */
683 684 685
static gboolean
send_message_to_gridd(CtkManageGridLicense *ctk_manage_grid_license,
                      gint param,
686
                      gint *pStatus)
687
{
688
    gboolean ret = FALSE;
689

690
    DBusMessage *msg, *reply;
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    DBusMessageIter args;
    DBusError err;

    DbusData *dbusData = ctk_manage_grid_license->dbusData;
    DBusConnection* conn = dbusData->conn;

    /* initialise the errors */
    dbusData->dbus.dbusErrorInit(&err);

    /* a new method call */
    msg = dbusData->dbus.dbusMessageNewMethodCall(NV_GRID_DBUS_TARGET, // target for the method call
                                       NV_GRID_DBUS_OBJECT, // object to call on
                                       NV_GRID_DBUS_INTERFACE, // interface to call on
                                       NV_GRID_DBUS_METHOD); // method name
    if (NULL == msg) {
        return FALSE;
    }
    /* append arguments */
    dbusData->dbus.dbusMessageIterInitAppend(msg, &args);
710

711 712
    if (!dbusData->dbus.dbusMessageIterAppendBasic(&args,
                                                   DBUS_TYPE_INT32, &param)) {
713
        goto done;
714 715
    }

716 717 718 719 720
    /* send a message and block for a default time period
       while waiting for a reply and returns NULL on failure with an error code.*/
    reply = dbusData->dbus.dbusConnectionSendWithReplyAndBlock(conn,
                                      msg, -1, &err);  // -1 is default timeout
    if ((reply == NULL) || (dbusData->dbus.dbusErrorIsSet(&err))) {
721
        goto done;
722 723 724
    }

    /* read the parameters */
725
    if (!dbusData->dbus.dbusMessageIterInit(reply, &args)) {
726 727 728 729 730
        nv_error_msg("GRID License dbus communication: Message has no arguments!\n");
    } else if (DBUS_TYPE_INT32 !=
               dbusData->dbus.dbusMessageIterGetArgType(&args)) {
        nv_error_msg("GRID License dbus communication: Argument is not int!\n");
    } else {
731
        dbusData->dbus.dbusMessageIterGetBasic(&args, pStatus);
732 733
    }

734 735
    ret = TRUE;
done:
736 737
    /* free reply */
    dbusData->dbus.dbusMessageUnref(msg);
738 739 740 741

    return ret;
}

742
    
743 744 745 746 747 748 749
/*
 * update_manage_grid_license_state_info() - update manage_grid_license state
 */

static gboolean update_manage_grid_license_state_info(gpointer user_data)
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(user_data);
750
    gchar *licenseStatusMessage = "";
751 752
    gboolean ret = TRUE;

753 754 755
    int licenseStatus = NV_GRID_UNLICENSED_VGPU;
    int licenseState = NV_GRID_UNLICENSED;
    int griddFeatureType = ctk_manage_grid_license->feature_type;
756

757 758 759 760 761
    /* Send license state and feature type request */
    if ((!(send_message_to_gridd(ctk_manage_grid_license, LICENSE_STATE_REQUEST,
                                 &licenseState))) ||
        (!(send_message_to_gridd(ctk_manage_grid_license, LICENSE_FEATURE_TYPE_REQUEST,
                                 &griddFeatureType)))) {
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
        licenseStatusMessage = "Unable to query license state information "
                               "from the NVIDIA GRID "
                               "licensing daemon.\n"
                               "Please make sure nvidia-gridd and "
                               "dbus-daemon are running.\n";
        gtk_label_set_text(GTK_LABEL(ctk_manage_grid_license->label_license_state),
                           licenseStatusMessage);
        /* Disable text controls on UI. */
        gtk_widget_set_sensitive(ctk_manage_grid_license->txt_server_address, FALSE);
        gtk_widget_set_sensitive(ctk_manage_grid_license->txt_server_port, FALSE);
        gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_address, FALSE);
        gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_port, FALSE);
        /* Disable Apply/Cancel button. */
        gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, FALSE);
        gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, FALSE);
        /* Disable toggle buttons */
778 779 780 781
        if (ctk_manage_grid_license->radio_btn_qdws && ctk_manage_grid_license->radio_btn_vapp) {
            gtk_widget_set_sensitive(ctk_manage_grid_license->radio_btn_qdws, FALSE);
            gtk_widget_set_sensitive(ctk_manage_grid_license->radio_btn_vapp, FALSE);
        }
782 783 784 785 786 787 788 789 790 791

        licenseStateQueryFailed = TRUE;
        return ret;
    }

    if (licenseStateQueryFailed == TRUE) {
        /*Failure to communicate with nvidia-gridd in previous attempt.
         Refresh UI with parameters from the griddConfig.*/
        update_gui_from_griddconfig(ctk_manage_grid_license);
        licenseStateQueryFailed = FALSE;
792 793
    }

794 795 796
    /* Set the license feature type fetched from nvidia-gridd.*/
    ctk_manage_grid_license->gridd_feature_type = griddFeatureType;

797
    if (licenseState == NV_GRID_UNLICENSED) {
798
        queryLicensedFeatureCode = TRUE;
799
        switch (ctk_manage_grid_license->feature_type) {
800 801
            case NV_GRID_LICENSE_FEATURE_TYPE_VAPP:
                licenseStatus = NV_GRID_UNLICENSED_VAPP;
802 803 804
                if (is_restart_required(ctk_manage_grid_license)) {
                    licenseStatus = NV_GRID_LICENSE_RESTART_REQUIRED_VAPP;
                }
805 806 807
                break;
            case NV_GRID_LICENSE_FEATURE_TYPE_QDWS:
                licenseStatus = NV_GRID_UNLICENSED_QDWS_SELECTED;
808 809 810
                if (licensedFeatureCode == NV_GRID_LICENSE_FEATURE_TYPE_QDWS) {
                    licenseStatus = NV_GRID_UNLICENSED_REQUEST_DETAILS;
                }
811 812 813 814 815 816
                break;
            case NV_GRID_LICENSE_FEATURE_TYPE_VGPU:
                licenseStatus = NV_GRID_UNLICENSED_VGPU;
                break;
            default:
                break;
817 818
        }
    }
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
    else {
        if ((licenseState == NV_GRID_LICENSED) ||
            (licenseState == NV_GRID_LICENSE_RENEWING) ||
            (licenseState == NV_GRID_LICENSE_RENEW_FAILED)) {
            switch (ctk_manage_grid_license->feature_type) {
                case NV_GRID_LICENSE_FEATURE_TYPE_VAPP:
                    licenseStatus = NV_GRID_LICENSE_ACQUIRED_QDWS;  // Default status in licensed state on non-vGPU case
                    if (ctk_manage_grid_license->feature_type == ctk_manage_grid_license->gridd_feature_type) {
                        licenseStatus = NV_GRID_LICENSE_RESTART_REQUIRED;
                    }
                    break;
                case NV_GRID_LICENSE_FEATURE_TYPE_QDWS:
                    licenseStatus = NV_GRID_LICENSE_ACQUIRED_QDWS;
                    if (ctk_manage_grid_license->feature_type != ctk_manage_grid_license->gridd_feature_type) {
                        /* On licensed non-vGPU setup, mismatch in feature type fetched from nvidia-gridd service and
                            the feature type read from gridd.conf/UI controls indicates user has edited the feature type
                            directly in the gridd.conf file and hence those changes will not be reflected until the system
                            is rebooted. Set appropriate license status to indicate the same. */
                        licenseStatus = NV_GRID_LICENSE_RESTART_REQUIRED;
                    }
                    break;
                case NV_GRID_LICENSE_FEATURE_TYPE_VGPU:
                    licenseStatus = NV_GRID_LICENSE_ACQUIRED_VGPU;
                    break;
                default:
                    break;
            }
846
            if (queryLicensedFeatureCode == TRUE) {
847
                get_licensed_feature_code(ctk_manage_grid_license);
848
                queryLicensedFeatureCode = FALSE;
849
            }
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
        }
        else if (licenseState == NV_GRID_LICENSE_REQUESTING) {
            switch (ctk_manage_grid_license->feature_type) {
                case NV_GRID_LICENSE_FEATURE_TYPE_VAPP:
                    licenseStatus = NV_GRID_LICENSE_REQUESTING_QDWS;    // Default status in license requesting state on non-vGPU case
                    if (ctk_manage_grid_license->feature_type == ctk_manage_grid_license->gridd_feature_type) {
                        licenseStatus = NV_GRID_UNLICENSED_VAPP;
                    }
                    break;
                case NV_GRID_LICENSE_FEATURE_TYPE_QDWS:
                    licenseStatus = NV_GRID_LICENSE_REQUESTING_QDWS;
                    if (ctk_manage_grid_license->feature_type != ctk_manage_grid_license->gridd_feature_type) {
                        /* On non-vGPU setup with license requesting state, mismatch in feature type fetched from nvidia-gridd service and
                            the feature type read from gridd.conf/UI controls indicates user has edited the feature type
                            directly in the gridd.conf file. So set appropriate license status message. */
                        licenseStatus = NV_GRID_UNLICENSED_QDWS_SELECTED;
                    }
                    break;
                case NV_GRID_LICENSE_FEATURE_TYPE_VGPU:
                    licenseStatus = NV_GRID_LICENSE_REQUESTING_VGPU;
                    break;
                default:
                    break;
            }
        }
        else if (licenseState == NV_GRID_LICENSE_FAILED) {
            switch (ctk_manage_grid_license->feature_type) {
                case NV_GRID_LICENSE_FEATURE_TYPE_VAPP:
                    licenseStatus = NV_GRID_LICENSE_FAILED_QDWS;    // Default status in license failed state on non-vGPU case
                    if (ctk_manage_grid_license->feature_type == ctk_manage_grid_license->gridd_feature_type) {
                        licenseStatus = NV_GRID_UNLICENSED_VAPP;
                    }
                    break;
                case NV_GRID_LICENSE_FEATURE_TYPE_QDWS:
                    licenseStatus = NV_GRID_LICENSE_FAILED_QDWS;
                    if (ctk_manage_grid_license->feature_type != ctk_manage_grid_license->gridd_feature_type) {
                        /* On non-vGPU setup with license failed state, mismatch in feature type fetched from nvidia-gridd service and
                            the feature type read from gridd.conf/UI controls indicates user has edited the feature type
                            directly in the gridd.conf file. So set appropriate license status message. */
                        licenseStatus = NV_GRID_UNLICENSED_QDWS_SELECTED;
                    }
                    break;
                case NV_GRID_LICENSE_FEATURE_TYPE_VGPU:
                    licenseStatus = NV_GRID_LICENSE_FAILED_VGPU;
                    break;
                default:
                    break;
            }
        }
        else if (licenseState == NV_GRID_LICENSE_EXPIRED) {
            switch (ctk_manage_grid_license->feature_type) {
                case NV_GRID_LICENSE_FEATURE_TYPE_VAPP:
                    licenseStatus = NV_GRID_LICENSE_EXPIRED_QDWS;    // Default status in license expired state on non-vGPU case
                    if (ctk_manage_grid_license->feature_type == ctk_manage_grid_license->gridd_feature_type) {
                        licenseStatus = NV_GRID_LICENSE_RESTART_REQUIRED;
                    }
                    break;
                case NV_GRID_LICENSE_FEATURE_TYPE_QDWS:
                    licenseStatus = NV_GRID_LICENSE_EXPIRED_QDWS;
                    if (ctk_manage_grid_license->feature_type != ctk_manage_grid_license->gridd_feature_type) {
                        /* On non-vGPU setup with expired license, mismatch in feature type fetched from nvidia-gridd service and
                            the feature type read from gridd.conf/UI controls indicates user has edited the feature type
                            directly in the gridd.conf file and hence those changes will not be reflected until the system
                            is rebooted. Set appropriate license status to indicate the same. */
                        licenseStatus = NV_GRID_LICENSE_RESTART_REQUIRED;
                    }
                    break;
                case NV_GRID_LICENSE_FEATURE_TYPE_VGPU:
                    licenseStatus = NV_GRID_LICENSE_EXPIRED_VGPU;
                    break;
                default:
                    break;
            }
        }
924 925
    }

926
    switch (licenseStatus) {
927
    case NV_GRID_UNLICENSED_VGPU:
928
          licenseStatusMessage = "Your system does not have a valid GRID vGPU license.\n"
929 930
              "Enter license server details and apply.";
          break;
931 932 933
    case NV_GRID_UNLICENSED_VAPP:
          licenseStatusMessage = "Your system is currently configured for "
              "GRID Virtual Apps.";
934
          break;
935 936
    case NV_GRID_UNLICENSED_QDWS_SELECTED:
          licenseStatusMessage = "Your system is currently configured for GRID Virtual Apps.\n"
937 938
             "Enter license server details and apply.";
          break;
939
    case NV_GRID_LICENSE_ACQUIRED_VGPU:
940
          licenseStatusMessage = "Your system is licensed for GRID vGPU.";
941
          break;
942 943 944
    case NV_GRID_LICENSE_ACQUIRED_QDWS:
          licenseStatusMessage = "Your system is licensed for Quadro Virtual Datacenter "
              "Workstation.";
945 946
          break;
    case NV_GRID_LICENSE_REQUESTING_VGPU:
947
          licenseStatusMessage = "Acquiring license for GRID vGPU.\n"
948 949
              "Your system does not have a valid GRID vGPU license.";
          break;
950 951 952 953
    case NV_GRID_LICENSE_REQUESTING_QDWS:
          licenseStatusMessage = "Acquiring license for Quadro Virtual Datacenter "
              "Workstation.\n"
              " Your system is currently configured for GRID Virtual Apps.";
954 955
          break;
    case NV_GRID_LICENSE_FAILED_VGPU:
956
          licenseStatusMessage = "Failed to acquire GRID vGPU license.";
957
          break;
958 959 960 961
    case NV_GRID_LICENSE_FAILED_QDWS:
          licenseStatusMessage = "Failed to acquire Quadro Virtual Datacenter "
              "Worstation license.\n"
              " Your system is currently configured for GRID Virtual Apps.";
962 963
          break;
    case NV_GRID_LICENSE_EXPIRED_VGPU:
964
          licenseStatusMessage = "GRID vGPU license has expired.\n"
965
              "Your system does not have a valid GRID vGPU license.";
966
          break;
967 968
    case NV_GRID_LICENSE_EXPIRED_QDWS:
          licenseStatusMessage = "License for Quadro Virtual Datacenter Workstation "
969 970 971
              "has expired.\n"
              "Your system does not have a valid Quadro Virtual Datacenter "
              "Workstation license.";
972 973
          break;
    case NV_GRID_LICENSE_RESTART_REQUIRED:
974 975 976
          licenseStatusMessage = "Restart your system for GRID Virtual Apps.\n"
              "Your system is currently licensed for Quadro Virtual Datacenter "
              "Workstation.";
977
          break;
978 979 980
    case NV_GRID_LICENSE_RESTART_REQUIRED_VAPP:
          licenseStatusMessage = "Restart your system for GRID Virtual Apps.";
          break;
981
    case NV_GRID_UNLICENSED_REQUEST_DETAILS:
982
    default:
983
          licenseStatusMessage = "Your system does not have a valid GRID license.\n"
984
              "Enter license server details and apply.";
985 986 987 988
          break;
    }

    gtk_label_set_text(GTK_LABEL(ctk_manage_grid_license->label_license_state),
989
                       licenseStatusMessage);
990
    return ret;
991 992
}

993 994 995 996 997 998 999
/*
 * is_restart_required() - Check if restart is required for new feature to be enabled.
 */
static gboolean is_restart_required(gpointer user_data)
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(user_data);
    gboolean ret = FALSE;
1000

1001
    /* Once licensed, system reboot required if there is mismatch between feature type
1002
        updated from UI/gridd.conf and feature code of the feature that is licensed on this system. */
1003
    if ((licensedFeatureCode) &&
1004
        (licensedFeatureCode != ctk_manage_grid_license->feature_type)) {
1005 1006 1007 1008
        ret = TRUE;
    }
    return ret;
}
1009 1010

/*
1011
 * apply_clicked() - Called when the user clicks on the "Apply" button.
1012 1013
 */

1014
static void apply_clicked(GtkWidget *widget, gpointer user_data)
1015 1016 1017
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(user_data);
    gboolean ret;
1018
    int err = 0;
1019
    gint status = 0;
1020 1021
    gboolean configFileAvailable;
    gboolean writable = FALSE;
1022

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    /* Check available config file */
    configFileAvailable = checkConfigfile(&writable);
    if (configFileAvailable && writable) {
        /* Add information to gridd.conf file */
        err = UpdateConfigFile(ctk_manage_grid_license);
        if (err == 0) {
            /* Send update request to nvidia-gridd */
            ret = send_message_to_gridd(ctk_manage_grid_license,
                                        LICENSE_DETAILS_UPDATE_REQUEST,
                                        &status);
            if ((!ret) || (status != LICENSE_DETAILS_UPDATE_SUCCESS)) {
                GtkWidget *dlg, *parent;

                parent = ctk_get_parent_window(GTK_WIDGET(ctk_manage_grid_license));

                dlg = gtk_message_dialog_new(GTK_WINDOW(parent),
                                             GTK_DIALOG_MODAL,
                                             GTK_MESSAGE_WARNING,
                                             GTK_BUTTONS_OK,
                                             "Unable to send license information "
                                             "update request to the NVIDIA GRID "
                                             "licensing daemon.\n"
                                             "Please make sure nvidia-gridd and "
                                             "dbus-daemon are running and retry applying the "
                                             "license settings.\n");
                gtk_dialog_run(GTK_DIALOG(dlg));
                gtk_widget_destroy(dlg);
            }
        } else {
1052 1053 1054 1055 1056
            GtkWidget *dlg, *parent;

            parent = ctk_get_parent_window(GTK_WIDGET(ctk_manage_grid_license));
            dlg = gtk_message_dialog_new(GTK_WINDOW(parent),
                                         GTK_DIALOG_MODAL,
1057
                                         GTK_MESSAGE_ERROR,
1058
                                         GTK_BUTTONS_OK,
1059 1060
                                         "Unable to update GRID license configuration "
                                         "file (%s): %s", GRID_CONFIG_FILE, strerror(err));
1061 1062
            gtk_dialog_run(GTK_DIALOG(dlg));
            gtk_widget_destroy(dlg);
1063
        }
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    } else if (configFileAvailable && !(writable)) {
        GtkWidget *dlg, *parent;

        parent = ctk_get_parent_window(GTK_WIDGET(ctk_manage_grid_license));
        dlg = gtk_message_dialog_new(GTK_WINDOW(parent),
                                     GTK_DIALOG_MODAL,
                                     GTK_MESSAGE_ERROR,
                                     GTK_BUTTONS_OK,
                                     "You do not have enough "
                                     "permissions to edit '%s' file.", GRID_CONFIG_FILE);
        gtk_dialog_run(GTK_DIALOG(dlg));
        gtk_widget_destroy(dlg);
1076 1077 1078 1079 1080 1081 1082 1083
    } else {
        GtkWidget *dlg, *parent;

        parent = ctk_get_parent_window(GTK_WIDGET(ctk_manage_grid_license));
        dlg = gtk_message_dialog_new(GTK_WINDOW(parent),
                                     GTK_DIALOG_MODAL,
                                     GTK_MESSAGE_ERROR,
                                     GTK_BUTTONS_OK,
1084 1085
                                     "'%s' file does not exist.\n You do not have "
                                     "permissions to create this file.", GRID_CONFIG_FILE);
1086 1087
        gtk_dialog_run(GTK_DIALOG(dlg));
        gtk_widget_destroy(dlg);
1088
    }
1089 1090 1091 1092 1093 1094

    /* Disable Apply/Cancel button. */
    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, FALSE);
    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, FALSE);
}

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
/*
 * get_licensed_feature_code() - Get the feature code of the feature that is licensed on this system.
 */
static void get_licensed_feature_code(gpointer user_data)
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(user_data);
    nvmlGridLicensableFeatures_t *gridLicensableFeatures;
    gint ret, i;

    ret = NvCtrlNvmlGetGridLicenseAttributes(ctk_manage_grid_license->target,
                                             NV_CTRL_ATTR_NVML_GPU_GRID_LICENSABLE_FEATURES,
                                             &gridLicensableFeatures);
    if (ret != NvCtrlSuccess) {
        return;
    }

    for (i = 0; i < gridLicensableFeatures->licensableFeaturesCount; i++)
    {
        if (gridLicensableFeatures->gridLicensableFeatures[i].featureState != 0)
        {
            licensedFeatureCode = gridLicensableFeatures->gridLicensableFeatures[i].featureCode;
            break;
        }
    }
    nvfree(gridLicensableFeatures);
}

1122 1123 1124 1125 1126
/*
 * cancel_clicked() - Called when the user clicks on the "Cancel" button.
 */

static void cancel_clicked(GtkWidget *widget, gpointer user_data)
1127 1128 1129 1130 1131 1132 1133 1134 1135
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(user_data);

    /* Discard user input and revert back to configuration from the nvidia-gridd config file */
    update_gui_from_griddconfig(ctk_manage_grid_license);

}

static void update_gui_from_griddconfig(gpointer user_data)
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(user_data);
    NvGriddConfigParams *griddConfig = NULL;
    const char *textBoxServerStr;

    griddConfig = GetNvGriddConfigParams();
    if (!griddConfig) {
        nv_error_msg("Null griddConfig. \n");
        /* If griddConfig is Null, clear out all the textboxes. */
        gtk_entry_set_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_address), "");
        gtk_entry_set_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_port), "");
        gtk_entry_set_text(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_address), "");
        gtk_entry_set_text(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_port), "");
    } else {
        /* Set the text in all the textboxes from the griddconfig. */
        gtk_entry_set_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_address),
                           griddConfig->str[NV_GRIDD_SERVER_ADDRESS]);
        gtk_entry_set_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_port),
                           griddConfig->str[NV_GRIDD_SERVER_PORT]);
        gtk_entry_set_text(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_address),
                           griddConfig->str[NV_GRIDD_BACKUP_SERVER_ADDRESS]);
        gtk_entry_set_text(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_port),
                           griddConfig->str[NV_GRIDD_BACKUP_SERVER_PORT]);
1159 1160 1161 1162 1163 1164 1165 1166
        /* set default value for feature type based on the user configured parameter or virtualization mode */
        /* Check Feature type "2" for Quadro Virtual Datacenter Workstation. */
        if (strcmp(griddConfig->str[NV_GRIDD_FEATURE_TYPE], "2") == 0) {
            ctk_manage_grid_license->feature_type = NV_GRID_LICENSE_FEATURE_TYPE_QDWS;
        }
        else {
            ctk_manage_grid_license->feature_type = NV_GRID_LICENSE_FEATURE_TYPE_VAPP;
        }
1167

1168 1169 1170 1171 1172
        /* Override feature type when on vGPU */
        if (ctk_manage_grid_license->license_edition_state == NV_CTRL_ATTR_NVML_GPU_VIRTUALIZATION_MODE_VGPU)
        {
            ctk_manage_grid_license->feature_type = NV_GRID_LICENSE_FEATURE_TYPE_VGPU;
        }
1173

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
        /* Set license edition toggle button active */
        if (ctk_manage_grid_license->radio_btn_qdws && ctk_manage_grid_license->radio_btn_vapp) {
            if (ctk_manage_grid_license->feature_type == NV_GRID_LICENSE_FEATURE_TYPE_QDWS) {
                /* Set 'Quadro Virtual Datacenter Workstation' toggle button active */
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ctk_manage_grid_license->radio_btn_qdws), TRUE);
            }
            else {
                /* Set 'GRID Virtual Apps' toggle button active */
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ctk_manage_grid_license->radio_btn_vapp), TRUE);
            }
        }

        /* Enable Primary server address/port textboxes. */
        gtk_widget_set_sensitive(ctk_manage_grid_license->txt_server_address, TRUE);
        gtk_widget_set_sensitive(ctk_manage_grid_license->txt_server_port, TRUE);
        /* Enable toggle buttons. */
1190 1191 1192 1193
        if (ctk_manage_grid_license->radio_btn_qdws && ctk_manage_grid_license->radio_btn_vapp) {
            gtk_widget_set_sensitive(ctk_manage_grid_license->radio_btn_qdws, TRUE);
            gtk_widget_set_sensitive(ctk_manage_grid_license->radio_btn_vapp, TRUE);
        }
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207

        textBoxServerStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_address));
        /* Enable/Disable Secondary server address/port textboxes if Primary server address textbox string is empty. */
        if (strcmp(textBoxServerStr, "") == 0) {
            gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_address, FALSE);
            gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_port, FALSE);
        } else {
            gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_address, TRUE);
            gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_port, TRUE);
        }
        /* Disable Apply/Cancel button. */
        gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, FALSE);
        gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, FALSE);
    }
1208 1209 1210

    if(griddConfig)
        FreeNvGriddConfigParams(griddConfig);
1211 1212 1213 1214 1215 1216
}

static void license_edition_toggled(GtkWidget *widget, gpointer user_data)
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(user_data);
    gboolean enabled;
1217
    gchar *statusBarMsg = "";
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    NvGriddConfigParams *griddConfig;
    const char *textBoxServerStr, *textBoxServerPortStr, *textBoxSecondaryServerStr, *textBoxSecondaryServerPortStr;

    griddConfig = GetNvGriddConfigParams();
    if (!griddConfig) {
        return;
    }

    textBoxServerStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_address));
    textBoxServerPortStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_port));
    textBoxSecondaryServerStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_address));
    textBoxSecondaryServerPortStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_port));
1230 1231 1232 1233 1234 1235 1236 1237

    enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
    if (!enabled) {
        /* Ignore 'disable' events. */
        return;
    }
    user_data = g_object_get_data(G_OBJECT(widget), "button_id");

1238
    if (GPOINTER_TO_INT(user_data) == NV_GRID_LICENSE_FEATURE_TYPE_QDWS) {
1239
        gtk_widget_set_sensitive(ctk_manage_grid_license->box_server_info, TRUE);
1240
        statusBarMsg = "You selected Quadro Virtual Datacenter Workstation Edition.";
1241
        ctk_manage_grid_license->feature_type =
1242
            NV_GRID_LICENSE_FEATURE_TYPE_QDWS;
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
        /* Enable Apply/Cancel button if the feature type selection has changed*/
        if (strcmp(griddConfig->str[NV_GRIDD_FEATURE_TYPE], "2") != 0) {
            /* Disable Apply/Cancel button if Primary server address textbox string is empty. */
            if (strcmp(textBoxServerStr, "") == 0) {
                gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, FALSE);
                gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, FALSE);
            } else {
                gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, TRUE);
                gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, TRUE);
            }
        } else {
            /* feature type selection has not changed. But Enable Apply/Cancel button when user make changes
                in any of the textbox entries to retain those changes. */
            if ((strcmp(griddConfig->str[NV_GRIDD_SERVER_ADDRESS], textBoxServerStr) != 0) ||
                ((strcmp(griddConfig->str[NV_GRIDD_BACKUP_SERVER_ADDRESS], textBoxSecondaryServerStr) != 0) ||
                (strcmp(griddConfig->str[NV_GRIDD_BACKUP_SERVER_PORT], textBoxSecondaryServerPortStr) != 0) ||
                (strcmp(griddConfig->str[NV_GRIDD_SERVER_PORT], textBoxServerPortStr) != 0))) {
                    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, TRUE);
                    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, TRUE);
            } else {
                    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, FALSE);
                    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, FALSE);
            }
        }

1268
    }  else if (GPOINTER_TO_INT(user_data) == NV_GRID_LICENSE_FEATURE_TYPE_VAPP) {
1269
        gtk_widget_set_sensitive(ctk_manage_grid_license->box_server_info, FALSE);
1270
        ctk_manage_grid_license->feature_type = 
1271 1272
            NV_GRID_LICENSE_FEATURE_TYPE_VAPP;
        statusBarMsg = "You selected GRID Virtual Apps Edition.";
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
        /* Enable Apply/Cancel button if the feature type selection has changed*/
        if (strcmp(griddConfig->str[NV_GRIDD_FEATURE_TYPE], "0") != 0) {
            gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, TRUE);
            gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, TRUE);
        } else {
            /* feature type selection has not changed. But Enable Apply/Cancel button when user make changes
                in any of the textbox entries to retain those changes. */
            if ((strcmp(griddConfig->str[NV_GRIDD_SERVER_ADDRESS], textBoxServerStr) != 0) ||
                ((strcmp(griddConfig->str[NV_GRIDD_BACKUP_SERVER_ADDRESS], textBoxSecondaryServerStr) != 0) ||
                (strcmp(griddConfig->str[NV_GRIDD_BACKUP_SERVER_PORT], textBoxSecondaryServerPortStr) != 0) ||
                (strcmp(griddConfig->str[NV_GRIDD_SERVER_PORT], textBoxServerPortStr) != 0))) {
                    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, TRUE);
                    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, TRUE);
            } else {
                    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, FALSE);
                    gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, FALSE);
            }
        }
1291 1292 1293
    }
    /* update status bar message */
    ctk_config_statusbar_message(ctk_manage_grid_license->ctk_config,
1294 1295
                                 "%s", statusBarMsg);
    update_manage_grid_license_state_info(ctk_manage_grid_license);
1296
    FreeNvGriddConfigParams(griddConfig);
1297 1298
}

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
static gboolean disallow_whitespace(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
    GdkEventKey *key_event;

    if (event->type == GDK_KEY_PRESS) {
        key_event = (GdkEventKey *) event;

        if (isspace(key_event->keyval)) {
            return TRUE;
        }
    }

    return FALSE;
}

1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
static gboolean enable_disable_ui_controls(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
    CtkManageGridLicense *ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(user_data);
    NvGriddConfigParams *griddConfig;
    const char *textBoxServerStr, *textBoxServerPortStr, *textBoxSecondaryServerStr, *textBoxSecondaryServerPortStr;

    griddConfig = GetNvGriddConfigParams();
    if (!griddConfig)
        return TRUE;

    if (event->type == GDK_KEY_RELEASE) {

        // Read license strings from textboxes.
        textBoxServerStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_address));
        textBoxServerPortStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_server_port));
        textBoxSecondaryServerStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_address));
        textBoxSecondaryServerPortStr = gtk_entry_get_text(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_port));

        /* Enable apply/cancel button if either:
1333 1334
            Primary server address/port textbox string doesn't match with the Primary server string from the nvidia-gridd config file or
            Secondary server address/port textbox string doesn't match with the Secondary server string from the nvidia-gridd config file. */
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
        if ((strcmp(griddConfig->str[NV_GRIDD_SERVER_ADDRESS], textBoxServerStr) != 0) ||
            ((strcmp(griddConfig->str[NV_GRIDD_BACKUP_SERVER_ADDRESS], textBoxSecondaryServerStr) != 0) ||
            (strcmp(griddConfig->str[NV_GRIDD_BACKUP_SERVER_PORT], textBoxSecondaryServerPortStr) != 0) ||
            (strcmp(griddConfig->str[NV_GRIDD_SERVER_PORT], textBoxServerPortStr) != 0))) {
                gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, TRUE);
                gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, TRUE);
        } else {
                gtk_widget_set_sensitive(ctk_manage_grid_license->btn_apply, FALSE);
                gtk_widget_set_sensitive(ctk_manage_grid_license->btn_cancel, FALSE);
        }

        /* Disable Secondary server address/port textboxes if Primary server address text box string is empty
            to notify user that Primary server address is mandatory. */
        if (strcmp(textBoxServerStr, "") == 0) {
            gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_address, FALSE);
            gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_port, FALSE);
        } else {
            gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_address, TRUE);
            gtk_widget_set_sensitive(ctk_manage_grid_license->txt_secondary_server_port, TRUE);
        }
    }

    FreeNvGriddConfigParams(griddConfig);

    return FALSE;
}

1362 1363 1364 1365 1366 1367
static gboolean allow_digits(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
    GdkEventKey *key_event;

    if (event->type == GDK_KEY_PRESS) {
        key_event = (GdkEventKey *) event;
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
        switch (key_event->keyval) {
        case GDK_Left:
        case GDK_KP_Left:
        case GDK_Down:
        case GDK_KP_Down:
        case GDK_Right:
        case GDK_KP_Right:
        case GDK_Up:
        case GDK_KP_Up:
        case GDK_Page_Down:
        case GDK_KP_Page_Down:
        case GDK_Page_Up:
        case GDK_KP_Page_Up:
        case GDK_BackSpace:
        case GDK_Delete:
1383 1384 1385
        case GDK_Tab:
        case GDK_KP_Tab:
        case GDK_ISO_Left_Tab:
1386
            // Allow all control keys
1387
            return FALSE;
1388 1389 1390 1391 1392
        default:
            // Allow digits
            if (isdigit(key_event->keyval)) {
                return FALSE;
            }
1393 1394 1395 1396 1397
        }
    }

    return TRUE;
}
1398 1399 1400 1401 1402

static gboolean checkConfigfile(gboolean *writable)
{
    struct stat st;
    gboolean ret = FALSE;
1403 1404 1405 1406 1407 1408
    FILE *configFile = NULL;
    FILE *templateFile = NULL;

    /* Check if user can create gridd.conf file */
    configFile = fopen(GRID_CONFIG_FILE, "r+");
    if (configFile) {
1409 1410
        *writable = TRUE;
    } else {
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
        /* check if file is readable */
        configFile = fopen(GRID_CONFIG_FILE, "r");
        if (!configFile) {
            /* file does not exist so create new file */
            configFile = fopen(GRID_CONFIG_FILE, "w");
            if (configFile) {
                *writable = TRUE;
            } else {
                goto done;
            }
1421
        } else {
1422
            *writable = FALSE;
1423 1424 1425 1426
        }
    }

    /* Check if config file available */
1427
    if ((fstat(fileno(configFile), &st) == 0) && (st.st_size == 0)) {
1428
        if (*writable) {
1429 1430 1431 1432 1433 1434
            ConfigFileLines *pLines;
            templateFile = fopen(GRID_CONFIG_FILE_TEMPLATE, "r");

            if (templateFile == NULL) {
                nv_error_msg("Config file '%s' had size zero.",
                             GRID_CONFIG_FILE);
1435 1436
            } else {
                pLines = ReadConfigFileStream(templateFile);
1437

1438
                WriteConfigFileStream(configFile, pLines);
1439

1440 1441
                FreeConfigFileLines(pLines);
            }
1442 1443 1444 1445 1446 1447 1448
        } else {
            nv_error_msg("Config file '%s' had size zero.",
                         GRID_CONFIG_FILE);
        }
    }
    ret = TRUE;
done:
1449 1450
    if (templateFile) {
        fclose(templateFile);
1451
    }
1452 1453
    if (configFile) {
        fclose(configFile);
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
    }
   return ret; 
}



GtkWidget* ctk_manage_grid_license_new(CtrlTarget *target,
                                       CtkConfig *ctk_config)
{
    GObject *object;
    CtkManageGridLicense *ctk_manage_grid_license = NULL;
    GtkWidget *hbox, *vbox, *vbox1, *vbox2 = NULL, *vbox3;
    GtkWidget *table;
    GtkWidget *eventbox;
    GtkWidget *banner, *label, *frame;
    gchar *str = NULL;
    int64_t mode;
    GSList *slist = NULL;
    gint ret;
    DBusError err;
    DBusConnection* conn;
    DbusData *dbusData;
    int64_t gridLicenseSupported;
    NvGriddConfigParams *griddConfig;

    /* make sure we have a handle */

    g_return_val_if_fail((target != NULL) &&
                         (target->h != NULL), NULL);

    /*
     * check if Manage GRID license page available.
     */

    ret = NvCtrlNvmlGetAttribute(target,
                                 NV_CTRL_ATTR_NVML_GPU_GRID_LICENSE_SUPPORTED,
                                 &gridLicenseSupported);

    if ((ret != NvCtrlSuccess) ||
        ((ret == NvCtrlSuccess) &&
         gridLicenseSupported ==
              NV_CTRL_ATTR_NVML_GPU_GRID_LICENSE_SUPPORTED_FALSE)) {
        return NULL;
    }

    /* Query the virtualization mode */
    ret = NvCtrlNvmlGetAttribute(target,
                                 NV_CTRL_ATTR_NVML_GPU_VIRTUALIZATION_MODE,
                                 &mode);

1504
    if (ret != NvCtrlSuccess) {
1505 1506
        return NULL;
    }
1507 1508

    /* GRID M6 is licensable gpu so we want to allow users to choose
1509
     * Quadro Virtual Datacenter Workstation and GRID Virtual Apps on baremetal setup.
1510 1511
     * When virtualization mode is NV_CTRL_ATTR_NVML_GPU_VIRTUALIZATION_MODE_NONE
     * treat it same way like NV_CTRL_ATTR_NVML_GPU_VIRTUALIZATION_MODE_PASSTHROUGH.
1512
     * So that it will show the Quadro Virtual Datacenter Workstation interface in case of
1513 1514 1515 1516 1517 1518
     * baremetal setup.
     */
    if (mode == NV_CTRL_ATTR_NVML_GPU_VIRTUALIZATION_MODE_NONE) {
        mode = NV_CTRL_ATTR_NVML_GPU_VIRTUALIZATION_MODE_PASSTHROUGH;
    }

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
    /* DBUS calls are used for quering the current license status  */
    dbusData = nvalloc(sizeof(*dbusData));

    ret = dbusLoadSymbols(dbusData);
    
    if (!ret) {
        return NULL;
    }
    dbusData->dbus.dbusErrorInit(&err);
    
    /* connect to the system bus */
    conn = dbusData->dbus.getDbus(DBUS_BUS_SYSTEM, &err);
    if (dbusData->dbus.dbusErrorIsSet(&err)) {
        dbusData->dbus.dbusErrorFree(&err);
    }
    if (NULL == conn) {
        return NULL;
    }
    dbusData->conn = conn;

    /* request the bus name */
1540 1541 1542
    dbusData->dbus.dbusRequestName(conn, NV_GRID_DBUS_CLIENT,
                                   DBUS_NAME_FLAG_REPLACE_EXISTING,
                                   &err);
1543 1544 1545 1546 1547 1548
    if (dbusData->dbus.dbusErrorIsSet(&err)) {
        dbusData->dbus.dbusErrorFree(&err);
    }

    /* Initialize config parameters */
    griddConfig = GetNvGriddConfigParams();
1549
    if (griddConfig &&
1550
        (strcmp(griddConfig->str[NV_GRIDD_ENABLE_UI], "TRUE") != 0)) {
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
        return NULL;
    }
    

    /* create the CtkManageGridLicense object */

    object = g_object_new(CTK_TYPE_MANAGE_GRID_LICENSE, NULL);
    
    ctk_manage_grid_license = CTK_MANAGE_GRID_LICENSE(object);
    ctk_manage_grid_license->ctk_config = ctk_config;
    ctk_manage_grid_license->license_edition_state = mode;
    ctk_manage_grid_license->dbusData = dbusData;
1563
    ctk_manage_grid_license->feature_type = 0;
1564
    ctk_manage_grid_license->target = target;
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

    /* set container properties for the CtkManageGridLicense widget */

    gtk_box_set_spacing(GTK_BOX(ctk_manage_grid_license), 5);

    /* banner */

    banner = ctk_banner_image_new(BANNER_ARTWORK_SERVER_LICENSING);
    gtk_box_pack_start(GTK_BOX(object), banner, FALSE, FALSE, 0);

    vbox = gtk_vbox_new(FALSE, 5);
    gtk_box_pack_start(GTK_BOX(object), vbox, FALSE, FALSE, 0);

    vbox1 = gtk_vbox_new(FALSE, 5);
1579
    vbox2 = gtk_vbox_new(FALSE, 0);
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
    frame = gtk_frame_new("");
    gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER(frame), vbox1);

    if (mode == NV_CTRL_ATTR_NVML_GPU_VIRTUALIZATION_MODE_PASSTHROUGH) {
        label = gtk_label_new("License Edition:");
        hbox = gtk_hbox_new(FALSE, 0);
        eventbox = gtk_event_box_new();
        gtk_container_add(GTK_CONTAINER(eventbox), label);
        ctk_config_set_tooltip(ctk_config, eventbox, __license_edition_help);

        gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, TRUE, 5);
        gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 5);
        vbox3 = gtk_vbox_new(FALSE, 5);
        gtk_container_add(GTK_CONTAINER(vbox1), vbox3);
        gtk_container_set_border_width(GTK_CONTAINER(vbox3), 5);
1596

1597
        ctk_manage_grid_license->radio_btn_qdws = gtk_radio_button_new_with_label(NULL,
1598
                                                  "Quadro Virtual Datacenter Workstation");
1599 1600 1601
        slist = gtk_radio_button_get_group(GTK_RADIO_BUTTON(ctk_manage_grid_license->radio_btn_qdws));
        gtk_box_pack_start(GTK_BOX(vbox3), ctk_manage_grid_license->radio_btn_qdws, FALSE, FALSE, 0);
        g_object_set_data(G_OBJECT(ctk_manage_grid_license->radio_btn_qdws), "button_id",
1602
                GINT_TO_POINTER(NV_GRID_LICENSE_FEATURE_TYPE_QDWS));
1603
        g_signal_connect(G_OBJECT(ctk_manage_grid_license->radio_btn_qdws), "toggled",
1604 1605 1606
                         G_CALLBACK(license_edition_toggled),
                         (gpointer) ctk_manage_grid_license);

1607 1608 1609
        ctk_manage_grid_license->radio_btn_vapp = gtk_radio_button_new_with_label(slist, "GRID Virtual Apps");
        gtk_box_pack_start(GTK_BOX(vbox3), ctk_manage_grid_license->radio_btn_vapp, FALSE, FALSE, 0);
        g_object_set_data(G_OBJECT(ctk_manage_grid_license->radio_btn_vapp), "button_id",
1610
                GINT_TO_POINTER(NV_GRID_LICENSE_FEATURE_TYPE_VAPP));
1611

1612
        g_signal_connect(G_OBJECT(ctk_manage_grid_license->radio_btn_vapp), "toggled",
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
                         G_CALLBACK(license_edition_toggled),
                         (gpointer) ctk_manage_grid_license);

    }

    /* Show current license status message */
    ctk_manage_grid_license->label_license_state = gtk_label_new("Unknown");
    hbox = gtk_hbox_new(FALSE, 0);
    eventbox = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(eventbox),
                      ctk_manage_grid_license->label_license_state);
    ctk_config_set_tooltip(ctk_config, eventbox, __license_edition_help);
    gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 5);
    
    frame = gtk_frame_new("");
    gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER(frame), vbox2);
    
1632 1633 1634 1635 1636
    /* License Server */
    label = gtk_label_new("License Server:");
    hbox = gtk_hbox_new(FALSE, 0);
    eventbox = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(eventbox), label);
1637
    ctk_config_set_tooltip(ctk_config, eventbox, __license_server_help);
1638 1639 1640 1641 1642 1643 1644 1645 1646
    gtk_box_pack_start(GTK_BOX(vbox2), hbox, FALSE, FALSE, 0);

    gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, TRUE, 5);
    vbox3 = gtk_vbox_new(FALSE, 0);
    gtk_container_set_border_width(GTK_CONTAINER(vbox3), 5);
    gtk_box_pack_start(GTK_BOX(vbox2), vbox3, FALSE, FALSE, 5);

    table = gtk_table_new(7, 5, FALSE);
    gtk_box_pack_start(GTK_BOX(vbox3), table, FALSE, FALSE, 0);
1647 1648 1649
    gtk_table_set_row_spacings(GTK_TABLE(table), 3);
    gtk_table_set_col_spacings(GTK_TABLE(table), 15);
    gtk_container_set_border_width(GTK_CONTAINER(table), 5);
1650 1651 1652 1653 1654


    /* Primary License Server Address */

    label = gtk_label_new("Primary Server:");
1655 1656 1657 1658
    ctk_manage_grid_license->txt_server_address = gtk_entry_new();
    hbox = gtk_hbox_new(FALSE, 0);
    eventbox = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(eventbox), label);
1659
    ctk_config_set_tooltip(ctk_config, eventbox, __primary_server_address_help);
1660 1661 1662
    gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, TRUE, 0);
    gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, 1, 2,
                     GTK_FILL, GTK_FILL | GTK_EXPAND, 0, 0);
1663 1664 1665
    g_signal_connect(GTK_ENTRY(ctk_manage_grid_license->txt_server_address), "key-press-event",
                     G_CALLBACK(disallow_whitespace),
                     (gpointer) ctk_manage_grid_license);
1666 1667 1668
    g_signal_connect(GTK_ENTRY(ctk_manage_grid_license->txt_server_address), "key-release-event",
                     G_CALLBACK(enable_disable_ui_controls),
                     (gpointer) ctk_manage_grid_license);
1669 1670 1671

    /* value */
    hbox = gtk_hbox_new(FALSE, 0);
1672 1673
    gtk_box_pack_start(GTK_BOX(hbox),
                       ctk_manage_grid_license->txt_server_address,
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
                       FALSE, FALSE, 0);
    gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 1, 2,
                     GTK_FILL, GTK_FILL | GTK_EXPAND, 5, 0);

    /* Port Number */
    label = gtk_label_new("Port Number:");

    hbox = gtk_hbox_new(FALSE, 5);
    eventbox = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(eventbox), label);
1684
    ctk_config_set_tooltip(ctk_config, eventbox, __primary_server_port_help);
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
    gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, TRUE, 0);
    gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, 2, 3,
                     GTK_FILL, GTK_FILL | GTK_EXPAND, 0, 0);

    /* value */
    ctk_manage_grid_license->txt_server_port = gtk_entry_new();
    hbox = gtk_hbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox),
                       ctk_manage_grid_license->txt_server_port,
                       FALSE, FALSE, 0);
    gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 2, 3,
                     GTK_FILL, GTK_FILL | GTK_EXPAND, 5, 0);
1697 1698 1699
    g_signal_connect(GTK_ENTRY(ctk_manage_grid_license->txt_server_port), "key-press-event",
                     G_CALLBACK(allow_digits),
                     (gpointer) ctk_manage_grid_license);
1700 1701 1702 1703
    g_signal_connect(GTK_ENTRY(ctk_manage_grid_license->txt_server_port), "key-release-event",
                     G_CALLBACK(enable_disable_ui_controls),
                     (gpointer) ctk_manage_grid_license);

1704 1705 1706
    /* Backup Server Address */
    label = gtk_label_new("Secondary Server:");
    ctk_manage_grid_license->txt_secondary_server_address = gtk_entry_new();
1707

1708 1709 1710 1711 1712 1713 1714 1715
    hbox = gtk_hbox_new(FALSE, 0);
    eventbox = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(eventbox), label);
    ctk_config_set_tooltip(ctk_config, eventbox,
                           __secondary_server_help);
    gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, TRUE, 0);
    gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, 5, 6,
                     GTK_FILL, GTK_FILL | GTK_EXPAND, 0, 0);
1716 1717 1718
    g_signal_connect(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_address), "key-press-event",
                     G_CALLBACK(disallow_whitespace),
                     (gpointer) ctk_manage_grid_license);
1719 1720 1721
    g_signal_connect(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_address), "key-release-event",
                     G_CALLBACK(enable_disable_ui_controls),
                     (gpointer) ctk_manage_grid_license);
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743

    /* value */
    hbox = gtk_hbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox),
                       ctk_manage_grid_license->txt_secondary_server_address,
                       FALSE, FALSE, 0);
    gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 5, 6,
                     GTK_FILL, GTK_FILL | GTK_EXPAND, 5, 0);

    /* Port Number */
    label = gtk_label_new("Port Number:");

    hbox = gtk_hbox_new(FALSE, 5);
    eventbox = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(eventbox), label);
    ctk_config_set_tooltip(ctk_config, eventbox, __secondary_server_port_help);
    gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, TRUE, 0);
    gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, 6, 7,
                     GTK_FILL, GTK_FILL | GTK_EXPAND, 0, 0);

    /* value */
    ctk_manage_grid_license->txt_secondary_server_port = gtk_entry_new();
1744

1745 1746 1747 1748 1749 1750
    hbox = gtk_hbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox),
                       ctk_manage_grid_license->txt_secondary_server_port,
                       FALSE, FALSE, 0);
    gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 6, 7,
                     GTK_FILL, GTK_FILL | GTK_EXPAND, 5, 0);
1751 1752 1753
    g_signal_connect(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_port), "key-press-event",
                     G_CALLBACK(allow_digits),
                     (gpointer) ctk_manage_grid_license);
1754 1755 1756
    g_signal_connect(GTK_ENTRY(ctk_manage_grid_license->txt_secondary_server_port), "key-release-event",
                     G_CALLBACK(enable_disable_ui_controls),
                     (gpointer) ctk_manage_grid_license);
Sachin Chitte's avatar