net.c revision 0993b0142e797366ce7db07dd8c71b27b2426068
/* Copyright (c) 2001, Stanford University
* All rights reserved
*
* See the file LICENSE.txt for information on redistributing this software.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <memory.h>
#include <signal.h>
#ifdef WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <process.h>
#else
#include <unistd.h>
#endif
#include "cr_mem.h"
#include "cr_error.h"
#include "cr_string.h"
#include "cr_url.h"
#include "cr_net.h"
#include "cr_netserver.h"
#include "cr_pixeldata.h"
#include "cr_environment.h"
#include "cr_endian.h"
#include "cr_bufpool.h"
#include "cr_threads.h"
#include "net_internals.h"
#define CR_MINIMUM_MTU 1024
/* Allow up to four processes per node. . . */
#define CR_QUADRICS_LOWEST_RANK 0
#define CR_QUADRICS_HIGHEST_RANK 3
static struct {
int initialized; /* flag */
/* Number of connections using each type of interface: */
int use_tcpip;
int use_ib;
int use_file;
int use_udp;
int use_gm;
int use_sdp;
int use_teac;
int use_tcscomm;
int use_hgcm;
int num_clients; /* total number of clients (unused?) */
#ifdef CHROMIUM_THREADSAFE
#endif
} cr_net;
/**
* Helper routine used by both crNetConnectToServer() and crNetAcceptClient().
* Call the protocol-specific Init() and Connection() functions.
*
*/
static void
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, struct VBOXUHGSMI *pHgsmi
#endif
)
{
{
}
{
}
{
/* file with byte-swapping */
}
{
}
{
}
#ifdef VBOX_WITH_HGCM
{
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, pHgsmi
#endif
);
}
#endif
#ifdef GM_SUPPORT
{
}
#endif
#ifdef TEAC_SUPPORT
{
}
#endif
#ifdef TCSCOMM_SUPPORT
{
}
#endif
#ifdef SDP_SUPPORT
{
}
#endif
#ifdef IB_SUPPORT
{
crDebug("Calling crIBInit()");
crDebug("Done Calling crIBInit()");
}
#endif
#ifdef HP_MULTICAST_SUPPORT
{
}
#endif
else
{
}
}
/**
* Establish a connection with a server.
* \param server the server to connect to, in the form
* "protocol://servername:port" where the port specifier
* is optional and if the protocol is missing it is assumed
* to be "tcpip".
* \param default_port the port to connect to, if port not specified in the
* server URL string.
* \param mtu desired maximum transmission unit size (in bytes)
* \param broker either 1 or 0 to indicate if connection is brokered through
* the mothership
*/
CRConnection * crNetConnectToServer( const char *server, unsigned short default_port, int mtu, int broker
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, struct VBOXUHGSMI *pHgsmi
#endif
)
{
unsigned short port;
crDebug( "In crNetConnectToServer( \"%s\", port=%d, mtu=%d, broker=%d )",
if (mtu < CR_MINIMUM_MTU)
{
crError( "You tried to connect to server \"%s\" with an mtu of %d, "
}
/* Tear the URL apart into relevant portions. */
}
/* If the host name is "localhost" replace it with the _real_ name
* of the localhost. If we don't do this, there seems to be
* confusion in the mothership as to whether or not "localhost" and
* "foo.bar.com" are the same machine.
*/
(void) rv;
}
/* XXX why is this here??? I think it could be moved into the
* crTeacConnection() function with no problem. I.e. change the
* connection's port, teac_rank and tcscomm_rank there. (BrianP)
*/
/* For Quadrics protocols, treat "port" as "rank" */
if ( port > CR_QUADRICS_HIGHEST_RANK ) {
crWarning( "Invalid crserver rank, %d, defaulting to %d\n",
}
}
crDebug( "Connecting to %s on port %d, with protocol %s",
#ifdef SDP_SUPPORT
/* This makes me ill, but we need to "fix" the hostname for sdp. MCH */
char* temp;
}
#endif
if (!conn)
return NULL;
/* init the non-zero fields */
/* XXX why are these here??? Move them into the crTeacConnection()
* and crTcscommConnection() functions.
*/
/* now, just dispatch to the appropriate protocol's initialization functions. */
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, pHgsmi
#endif
);
if (!crNetConnect( conn ))
{
crDebug("crNetConnectToServer() failed, freeing the connection");
#ifdef CHROMIUM_THREADSAFE
#endif
return NULL;
}
return conn;
}
/**
* Send a message to the receiver that another connection is needed.
* We send a CR_MESSAGE_NEWCLIENT packet, then call crNetServerConnect.
*/
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, struct VBOXUHGSMI *pHgsmi
#endif
)
{
/*
unsigned int len = sizeof(CRMessageNewClient);
CRMessageNewClient msg;
CRASSERT( conn );
if (conn->swap)
msg.header.type = (CRMessageType) SWAP32(CR_MESSAGE_NEWCLIENT);
else
msg.header.type = CR_MESSAGE_NEWCLIENT;
crNetSend( conn, NULL, &msg, len );
*/
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, pHgsmi
#endif
);
}
/**
* Accept a connection from a client.
* \param protocol the protocol to use (such as "tcpip" or "gm")
* \param hostname optional hostname of the expected client (may be NULL)
* \param port number of the port to accept on
* \param mtu maximum transmission unit
* \param broker either 1 or 0 to indicate if connection is brokered through
* the mothership
* \return new CRConnection object, or NULL
*/
{
if (!conn)
return NULL;
/* init the non-zero fields */
/* now, just dispatch to the appropriate protocol's initialization functions. */
crDebug("In crNetAcceptClient( protocol=\"%s\" port=%d mtu=%d )",
/* special case */
{
char filename[4096];
char protocol_only[4096];
{
}
/* call the protocol-specific init routines */ // ktd (add)
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, NULL
#endif
); // ktd (add)
}
else {
/* call the protocol-specific init routines */
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, NULL
#endif
);
}
return conn;
}
/**
* Close and free given connection.
*/
void
{
#ifdef CHROMIUM_THREADSAFE
#endif
}
/**
* Start the ball rolling. give functions to handle incoming traffic
* (usually placing blocks on a queue), and a handler for dropped
* connections.
*/
{
if ( cr_net.initialized )
{
/*crDebug( "Networking already initialized!" );*/
}
else
{
#ifdef WINDOWS
/* @todo: do we actually need that WSA stuff with VBox at all? */
int err;
if (err != 0)
crError("Couldn't initialize sockets on WINDOWS");
#endif
cr_net.use_tcscomm = 0;
cr_net.num_clients = 0;
#ifdef CHROMIUM_THREADSAFE
#endif
}
{
/* check if function is already in the list */
{
{
/* we've already seen this function -- do nothing */
break;
}
}
/* not in list, so insert at the head */
if (!rfl)
{
}
}
{
/* check if function is already in the list */
{
{
/* we've already seen this function -- do nothing */
break;
}
}
/* not in list, so insert at the head */
if (!cfl)
{
}
}
}
/* Free up stuff */
void crNetTearDown()
{
void *tmp;
if (!cr_net.initialized) return;
#ifdef CHROMIUM_THREADSAFE
#endif
/* Note, other protocols used by chromium should free up stuff too,
* but VBox doesn't use them, so no other checks.
*/
{
}
{
}
cr_net.initialized = 0;
#ifdef CHROMIUM_THREADSAFE
#endif
}
{
CRConnection **c;
c = crTCPIPDump( num );
if ( c ) return c;
c = crDevnullDump( num );
if ( c ) return c;
c = crFileDump( num );
if ( c ) return c;
#ifdef VBOX_WITH_HGCM
c = crVBoxHGCMDump( num );
if ( c ) return c;
#endif
#ifdef GM_SUPPORT
if ( c ) return c;
#endif
#ifdef IB_SUPPORT
if ( c ) return c;
#endif
#ifdef SDP_SUPPORT
if ( c ) return c;
#endif
*num = 0;
return NULL;
}
/*
* Allocate a network data buffer. The size will be the mtu size specified
* earlier to crNetConnectToServer() or crNetAcceptClient().
*
* Buffers that will eventually be transmitted on a connection
* *must* be allocated using this interface. This way, we can
* automatically pin memory and tag blocks, and we can also use
* our own buffer pool management.
*/
{
}
/**
* This returns a buffer (which was obtained from crNetAlloc()) back
* to the network layer so that it may be reused.
*/
{
}
void
{
list->numMessages = 0;
#ifdef CHROMIUM_THREADSAFE
#endif
}
/**
* Add a message node to the end of the message list.
* \param list the message list
* \param msg points to start of message buffer
* \param len length of message, in bytes
* \param conn connection associated with message (may be NULL)
*/
void
{
#ifdef CHROMIUM_THREADSAFE
#endif
/* insert at tail */
else
list->numMessages++;
#ifdef CHROMIUM_THREADSAFE
#endif
}
/**
* Remove first message node from message list and return it.
* Don't block.
* \return 1 if message was dequeued, 0 otherwise.
*/
static int
{
int retval;
#ifdef CHROMIUM_THREADSAFE
#endif
/* unlink the node */
/* empty list */
}
if (conn)
list->numMessages--;
retval = 1;
}
else {
*len = 0;
retval = 0;
}
#ifdef CHROMIUM_THREADSAFE
#endif
return retval;
}
/**
* Remove message from tail of list. Block until non-empty if needed.
* \param list the message list
* \param msg returns start of message
* \param len returns message length, in bytes
* \param conn returns connection associated with message (may be NULL)
*/
void
CRConnection **conn)
{
#ifdef CHROMIUM_THREADSAFE
#endif
#ifdef CHROMIUM_THREADSAFE
}
#else
#endif
/* unlink the node */
/* empty list */
}
if (conn)
list->numMessages--;
#ifdef CHROMIUM_THREADSAFE
#endif
}
/**
* Send a set of commands on a connection. Pretty straightforward, just
* error checking, byte counting, and a dispatch to the protocol's
* "send" implementation.
* The payload will be prefixed by a 4-byte length field.
*
* \param conn the network connection
* \param bufp if non-null the buffer was provided by the network layer
* and will be returned to the 'free' pool after it's sent.
* \param start points to first byte to send, which must point to a CRMessage
* object!
* \param len number of bytes to send
*/
void
{
if ( bufp ) {
/* The region from [start .. start + len - 1] must lie inside the
* buffer pointed to by *bufp.
*/
}
#ifdef DEBUG
{
crError( "crNetSend: send_credits=%u, looks like there is a leak (max=%u)",
}
#endif
}
/**
* Like crNetSend(), but the network layer is free to discard the data
* if something goes wrong. In particular, the UDP layer might discard
* the data in the event of transmission errors.
*/
{
if ( bufp ) {
}
#ifdef DEBUG
{
crError( "crNetBarf: send_credits=%u, looks like there is a "
}
#endif
}
/**
* Send a block of bytes across the connection without any sort of
* \param conn the network connection
* \param buf points to first byte to send
* \param len number of bytes to send
*/
{
}
/**
* Connect to a server, as specified by the 'name' and 'buffer_size' fields
* of the CRNetServer parameter.
* When done, the CrNetServer's conn field will be initialized.
*/
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, struct VBOXUHGSMI *pHgsmi
#endif
)
{
ns->buffer_size, 0
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, pHgsmi
#endif
);
}
/**
* Actually set up the specified connection.
* Apparently, this is only called from the crNetConnectToServer function.
*/
{
}
/**
* Tear down a network connection (close the socket, etc).
*/
{
#ifdef CHROMIUM_THREADSAFE
#endif
}
/**
* Actually set up the specified connection.
* Apparently, this is only called from the crNetConnectToServer function.
*/
{
}
/**
* Do a blocking receive on a particular connection. This only
* really works for TCPIP, but it's really only used (right now) by
* the mothership client library.
*/
{
{
crError( "Can't do a crNetSingleReceive on anything other than TCPIP." );
}
}
/**
* Receive a chunk of a CR_MESSAGE_MULTI_BODY/TAIL transmission.
* \param conn the network connection
* \param msg the incoming multi-part message
* \param len number of bytes in the message
*/
static void
{
/* Check if there's enough room in the multi-buffer to append 'len' bytes */
{
{
}
/* grow the buffer by 2x until it's big enough */
{
}
}
{
/* OK, we've collected the last chunk of the multi-part message */
conn,
/* clean this up before calling the user */
}
/* Don't do this too early! */
}
/**
* Increment the connection's send_credits by msg->credits.
*/
static void
unsigned int len )
{
}
#ifdef IN_GUEST
/**
* Called by the main receive function when we get a CR_MESSAGE_WRITEBACK
* message. Writeback is used to implement glGet*() functions.
*/
static void
{
int *writeback;
(*writeback)--;
}
/**
* Called by the main receive function when we get a CR_MESSAGE_READBACK
* message. Used to implement glGet*() functions.
*/
static void
{
/* minus the header, the destination pointer,
* *and* the implicit writeback pointer at the head. */
int *writeback;
void *dest_ptr;
(*writeback)--;
}
#endif
/**
* This is used by the SPUs that do packing (such as Pack, Tilesort and
* Replicate) to process ReadPixels messages. We can't call this directly
* from the message loop below because the SPU's have other housekeeping
* to do for ReadPixels (such as decrementing counters).
*/
void
{
char *dest_ptr;
/* set dest_ptr value */
/* store pixel data in app's memory */
rp->skipPixels == 0 &&
/* no special packing is needed */
}
else {
/* need special packing */
packing.imageHeight = 0;
packing.skipImages = 0;
}
}
/**
* If an incoming message is not consumed by any of the connection's
* receive callbacks, this function will get called.
*
* XXX Make this function static???
*/
void
{
{
case CR_MESSAGE_GATHER:
break;
case CR_MESSAGE_MULTI_BODY:
case CR_MESSAGE_MULTI_TAIL:
return;
case CR_MESSAGE_FLOW_CONTROL:
return;
case CR_MESSAGE_OPCODES:
case CR_MESSAGE_OOB:
{
/*CRMessageOpcodes *ops = (CRMessageOpcodes *) msg;
*unsigned char *data_ptr = (unsigned char *) ops + sizeof( *ops) + ((ops->numOpcodes + 3 ) & ~0x03);
*crDebugOpcodes( stdout, data_ptr-1, ops->numOpcodes ); */
}
break;
case CR_MESSAGE_READ_PIXELS:
WARN(( "Can't handle read pixels" ));
return;
case CR_MESSAGE_WRITEBACK:
#ifdef IN_GUEST
#else
WARN(("CR_MESSAGE_WRITEBACK not expected\n"));
#endif
return;
case CR_MESSAGE_READBACK:
#ifdef IN_GUEST
#else
WARN(("CR_MESSAGE_READBACK not expected\n"));
#endif
return;
case CR_MESSAGE_CRUT:
/* nothing */
break;
default:
/* We can end up here if anything strange happens in
* the GM layer. In particular, if the user tries to
* send unpinned memory over GM it gets sent as all
* 0xAA instead. This can happen when a program exits
* ungracefully, so the GM is still DMAing memory as
* it is disappearing out from under it. We can also
* end up here if somebody adds a message type, and
* doesn't put it in the above case block. That has
* an obvious fix. */
{
char string[128];
WARN(("crNetDefaultRecv: received a bad message: type=%d buf=[%s]\n"
"Did you add a new message type and forget to tell "
"crNetDefaultRecv() about it?\n",
}
}
/* If we make it this far, it's not a special message, so append it to
* the end of the connection's list of received messages.
*/
}
/**
* Default handler for receiving data. Called via crNetRecv().
* Typically, the various implementations of the network layer call this.
* \param msg this is the address of the message (of <len> bytes) the
* first part of which is a CRMessage union.
*/
void
{
{
{
/* Message was consumed by somebody (maybe a SPU).
* All done.
*/
return;
}
}
/* Append the message to the connection's message list. It'll be
* consumed later (by crNetPeekMessage or crNetGetMessage and
* then freed with a call to crNetFree()). At this point, the buffer
* *must* have been allocated with crNetAlloc!
*/
}
/**
* Return number of messages queued up on the given connection.
*/
int
{
}
/**
* Get the next message in the connection's message list. These are
* message that have already been received. We do not try to read more
* bytes from the network connection.
*
* The crNetFree() function should be called when finished with the message!
*
* \param conn the network connection
* \param message returns a pointer to the next message
* \return length of message (header + payload, in bytes)
*/
unsigned int
{
unsigned int len;
return len;
else
return 0;
}
/**
* Get the next message from the given network connection. If there isn't
* one already in the linked list of received messages, call crNetRecv()
* until we get something.
*
* \param message returns pointer to the message
* \return total length of message (header + payload, in bytes)
*/
unsigned int
{
/* Keep getting work to do */
for (;;)
{
if (len)
return len;
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
#endif
);
}
/* silence compiler */
return 0;
#endif
}
/**
* Read a \n-terminated string from a connection. Replace the \n with \0.
* Useful for reading from the mothership.
* \note This is an extremely inefficient way to read a string!
*
* \param conn the network connection
* \param buf buffer in which to place results
*/
{
char *temp, c;
return;
{
}
for (;;)
{
if (c == '\n')
{
*temp = '\0';
return;
}
*(temp++) = c;
}
}
#ifdef IN_GUEST
{
#ifdef VBOX_WITH_HGCM
return crVBoxHGCMHostCapsGet();
#endif
WARN(("HostCaps supportted for HGCM only!"));
return 0;
}
#endif
/**
* The big boy -- call this function to see (non-blocking) if there is
* any pending work. If there is, the networking layer's "work received"
* handler will be called, so this function only returns a flag. Work
* is assumed to be placed on queues for processing by the handler.
*/
int crNetRecv(
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
#endif
)
{
int found_work = 0;
found_work += crTCPIPRecv();
#ifdef VBOX_WITH_HGCM
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
#endif
);
#endif
#ifdef SDP_SUPPORT
found_work += crSDPRecv();
#endif
#ifdef IB_SUPPORT
found_work += crIBRecv();
#endif
found_work += crUDPTCPIPRecv();
found_work += crFileRecv();
#ifdef GM_SUPPORT
found_work += crGmRecv();
#endif
#ifdef TEAC_SUPPORT
found_work += crTeacRecv();
#endif
#ifdef TCSCOMM_SUPPORT
if ( cr_net.use_tcscomm )
found_work += crTcscommRecv();
#endif
return found_work;
}
/**
*/
void
crNetSetRank( int my_rank )
{
#ifdef TEAC_SUPPORT
#endif
#ifdef TCSCOMM_SUPPORT
#endif
}
/**
*/
void
{
#ifdef TEAC_SUPPORT
#endif
#ifdef TCSCOMM_SUPPORT
#endif
}
/**
*/
void
{
#ifdef TEAC_SUPPORT
#endif
#ifdef TCSCOMM_SUPPORT
#endif
}
/**
*/
void
{
#ifdef TEAC_SUPPORT
#endif
}