iPXE
Functions | Variables
guestinfo.c File Reference

VMware GuestInfo settings. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ipxe/init.h>
#include <ipxe/settings.h>
#include <ipxe/netdevice.h>
#include <ipxe/guestrpc.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static int guestinfo_fetch_type (struct settings *settings, struct setting *setting, const struct setting_type *type, void *data, size_t len, int *found)
 Fetch value of typed GuestInfo setting.
static int guestinfo_fetch (struct settings *settings, struct setting *setting, void *data, size_t len)
 Fetch value of GuestInfo setting.
static void guestinfo_init (void)
 Initialise GuestInfo settings.
struct init_fn guestinfo_init_fn __init_fn (INIT_NORMAL)
 GuestInfo settings initialiser.
static int guestinfo_net_probe (struct net_device *netdev)
 Create per-netdevice GuestInfo settings.
static void guestinfo_net_remove (struct net_device *netdev)
 Remove per-netdevice GuestInfo settings.

Variables

static int guestinfo_channel
 GuestInfo GuestRPC channel.
static struct settings_operations guestinfo_settings_operations
 GuestInfo settings operations.
static struct settings guestinfo_settings
 GuestInfo settings.
struct net_driver
guestinfo_net_driver 
__net_driver
 GuestInfo per-netdevice driver.

Detailed Description

VMware GuestInfo settings.

Definition in file guestinfo.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER  )
static int guestinfo_fetch_type ( struct settings settings,
struct setting setting,
const struct setting_type type,
void *  data,
size_t  len,
int *  found 
) [static]

Fetch value of typed GuestInfo setting.

Parameters:
settingsSettings block
settingSetting to fetch
typeSetting type to attempt (or NULL for default)
dataBuffer to fill with setting data
lenLength of buffer
Return values:
foundSetting found in GuestInfo
lenLength of setting data, or negative error

Definition at line 52 of file guestinfo.c.

References assert, DBGC, DBGC2, EIO, ENOMEM, find_setting(), free, guestinfo_channel, guestrpc_command(), info, info_len, setting::name, settings::name, setting_type::name, NULL, settings::parent, ret, setting_parse(), snprintf(), strerror(), strlen(), setting::type, and zalloc().

Referenced by guestinfo_fetch().

                                                                       {
        const char *parent_name = settings->parent->name;
        char command[ 24 /* "info-get guestinfo.ipxe." */ +
                      strlen ( parent_name ) + 1 /* "." */ +
                      strlen ( setting->name ) + 1 /* "." */ +
                      ( type ? strlen ( type->name ) : 0 ) + 1 /* NUL */ ];
        struct setting *predefined;
        char *info;
        int info_len;
        int check_len;
        int ret;

        /* Construct info-get command */
        snprintf ( command, sizeof ( command ),
                   "info-get guestinfo.ipxe.%s%s%s%s%s",
                   parent_name, ( parent_name[0] ? "." : "" ), setting->name,
                   ( type ? "." : "" ), ( type ? type->name : "" ) );

        /* Check for existence and obtain length of GuestInfo value */
        info_len = guestrpc_command ( guestinfo_channel, command, NULL, 0 );
        if ( info_len < 0 ) {
                ret = info_len;
                goto err_get_info_len;
        }

        /* Mark as found */
        *found = 1;

        /* Determine default type if necessary */
        if ( ! type ) {
                predefined = find_setting ( setting->name );
                type = ( predefined ? predefined->type : &setting_type_string );
        }
        assert ( type != NULL );

        /* Allocate temporary block to hold GuestInfo value */
        info = zalloc ( info_len + 1 /* NUL */ );
        if ( ! info ) {
                DBGC ( settings, "GuestInfo %p could not allocate %d bytes\n",
                       settings, info_len );
                ret = -ENOMEM;
                goto err_alloc;
        }
        info[info_len] = '\0';

        /* Fetch GuestInfo value */
        check_len = guestrpc_command ( guestinfo_channel, command,
                                       info, info_len );
        if ( check_len < 0 ) {
                ret = check_len;
                goto err_get_info;
        }
        if ( check_len != info_len ) {
                DBGC ( settings, "GuestInfo %p length mismatch (expected %d, "
                       "got %d)\n", settings, info_len, check_len );
                ret = -EIO;
                goto err_get_info;
        }
        DBGC2 ( settings, "GuestInfo %p found %s = \"%s\"\n",
                settings, &command[9] /* Skip "info-get " */, info );

        /* Parse GuestInfo value according to type */
        ret = setting_parse ( type, info, data, len );
        if ( ret < 0 ) {
                DBGC ( settings, "GuestInfo %p could not parse \"%s\" as %s: "
                       "%s\n", settings, info, type->name, strerror ( ret ) );
                goto err_parse;
        }

 err_parse:
 err_get_info:
        free ( info );
 err_alloc:
 err_get_info_len:
        return ret;
}
static int guestinfo_fetch ( struct settings settings,
struct setting setting,
void *  data,
size_t  len 
) [static]

Fetch value of GuestInfo setting.

Parameters:
settingsSettings block
settingSetting to fetch
dataBuffer to fill with setting data
lenLength of buffer
Return values:
lenLength of setting data, or negative error

Definition at line 141 of file guestinfo.c.

References ENOENT, for_each_table_entry, guestinfo_fetch_type(), NULL, ret, SETTING_TYPES, and type.

                                                      {
        struct setting_type *type;
        int found = 0;
        int ret;

        /* Try default type first */
        ret = guestinfo_fetch_type ( settings, setting, NULL,
                                     data, len, &found );
        if ( found )
                return ret;

        /* Otherwise, try all possible types */
        for_each_table_entry ( type, SETTING_TYPES ) {
                ret = guestinfo_fetch_type ( settings, setting, type,
                                             data, len, &found );
                if ( found )
                        return ret;
        }

        /* Not found */
        return -ENOENT;
}
static void guestinfo_init ( void  ) [static]

Initialise GuestInfo settings.

Definition at line 180 of file guestinfo.c.

References DBG, guestinfo_channel, guestrpc_open(), NULL, rc, register_settings(), and strerror().

                                    {
        int rc;

        /* Open GuestRPC channel */
        guestinfo_channel = guestrpc_open();
        if ( guestinfo_channel < 0 ) {
                rc = guestinfo_channel;
                DBG ( "GuestInfo could not open channel: %s\n",
                      strerror ( rc ) );
                return;
        }

        /* Register root GuestInfo settings */
        if ( ( rc = register_settings ( &guestinfo_settings, NULL,
                                        "vmware" ) ) != 0 ) {
                DBG ( "GuestInfo could not register settings: %s\n",
                      strerror ( rc ) );
                return;
        }
}
struct init_fn guestinfo_init_fn __init_fn ( INIT_NORMAL  ) [read]

GuestInfo settings initialiser.

static int guestinfo_net_probe ( struct net_device netdev) [static]

Create per-netdevice GuestInfo settings.

Parameters:
netdevNetwork device
Return values:
rcReturn status code

Definition at line 212 of file guestinfo.c.

References DBGC, ENOMEM, free, guestinfo_channel, net_device::name, netdev_settings(), NULL, rc, register_settings(), settings_init(), strerror(), and zalloc().

                                                             {
        struct settings *settings;
        int rc;

        /* Do nothing unless we have a GuestInfo channel available */
        if ( guestinfo_channel < 0 )
                return 0;

        /* Allocate and initialise settings block */
        settings = zalloc ( sizeof ( *settings ) );
        if ( ! settings ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        settings_init ( settings, &guestinfo_settings_operations, NULL, NULL );

        /* Register settings */
        if ( ( rc = register_settings ( settings, netdev_settings ( netdev ),
                                        "vmware" ) ) != 0 ) {
                DBGC ( settings, "GuestInfo %p could not register for %s: %s\n",
                       settings, netdev->name, strerror ( rc ) );
                goto err_register;
        }
        DBGC ( settings, "GuestInfo %p registered for %s\n",
               settings, netdev->name );

        return 0;

 err_register:
        free ( settings );
 err_alloc:
        return rc;
}
static void guestinfo_net_remove ( struct net_device netdev) [static]

Remove per-netdevice GuestInfo settings.

Parameters:
netdevNetwork device

Definition at line 251 of file guestinfo.c.

References settings::children, DBGC, free, list_for_each_entry, net_device::name, netdev_settings(), settings::op, settings::parent, settings::siblings, and unregister_settings().

                                                               {
        struct settings *parent = netdev_settings ( netdev );
        struct settings *settings;

        list_for_each_entry ( settings, &parent->children, siblings ) {
                if ( settings->op == &guestinfo_settings_operations ) {
                        DBGC ( settings, "GuestInfo %p unregistered for %s\n",
                               settings, netdev->name );
                        unregister_settings ( settings );
                        free ( settings );
                        return;
                }
        }
}

Variable Documentation

int guestinfo_channel [static]

GuestInfo GuestRPC channel.

Definition at line 39 of file guestinfo.c.

Referenced by guestinfo_fetch_type(), guestinfo_init(), and guestinfo_net_probe().

Initial value:
 {
        .fetch = guestinfo_fetch,
}

GuestInfo settings operations.

Definition at line 167 of file guestinfo.c.

struct settings guestinfo_settings [static]
Initial value:

GuestInfo settings.

Definition at line 172 of file guestinfo.c.

struct net_driver guestinfo_net_driver __net_driver
Initial value:
 {
        .name = "GuestInfo",
        .probe = guestinfo_net_probe,
        .remove = guestinfo_net_remove,
}

GuestInfo per-netdevice driver.

Definition at line 267 of file guestinfo.c.