/* A socket is created, we know its type - check if we should set a * new stackname, and do so. */ static int apply_extname(int socket_type) { char name[MAX_EXTNAME_LEN]; int ok; /* Need to have loaded the config from disk */ ensure_config_loaded(); LOG("apply_extname(%d) tid=%d", socket_type, (int)pthread_self()); /* Find the name for this thread */ ok = get_extname_for_thread(pthread_self(), name); LOG("get_extname_for_thread returned %d %s", ok, name); if( ok >= 0 ) { /* Store the current name, so we can restore it later */ ok = onload_stackname_save(); /* Adjust the name to account for socket type */ alter_name_for_socket(socket_type, name); /* Adjust the name to account for CPU */ alter_name_for_cpu(name); /* Check for 'do not accelerate' name, then set appropriate name */ if( !strncasecmp("_", name, 2) ) ok = onload_set_stackname(ONLOAD_THIS_THREAD, ONLOAD_SCOPE_GLOBAL, ONLOAD_DONT_ACCELERATE); else ok = onload_set_stackname(ONLOAD_THIS_THREAD, ONLOAD_SCOPE_GLOBAL, name); LOG("onload_set_stackname(\"%s\"): %d (%d)", name, ok, errno); } return ok; }
int main(void) { int s1; int s2; int r; struct onload_stat * stat; struct sockaddr_in servaddr; stat = malloc(sizeof(struct onload_stat)); /* new sockets created in this thread will not be accelerated */ if( onload_set_stackname(ONLOAD_THIS_THREAD, ONLOAD_SCOPE_THREAD, ONLOAD_DONT_ACCELERATE) ) perror("Error setting stackname:"); /* set up a basic UDP socket */ s1 = socket(AF_INET, SOCK_DGRAM, 0); r = onload_fd_stat(s1, stat); outputStatInfo(r, stat); close(s1); /* revert to accelerating sockets */ if( onload_set_stackname(ONLOAD_ALL_THREADS, ONLOAD_SCOPE_NOCHANGE, "") ) perror("Error setting stackname:"); /* set up a basic UDP socket */ s1 = socket(AF_INET, SOCK_DGRAM, 0); r = onload_fd_stat(s1, stat); outputStatInfo(r, stat); close(s1); /* give the stack a name */ if( onload_set_stackname(ONLOAD_ALL_THREADS, ONLOAD_SCOPE_GLOBAL, "s_name") ) perror("Error setting stackname:"); /* set up two basic UDP sockets */ s1 = socket(AF_INET, SOCK_DGRAM, 0); r = onload_fd_stat(s1, stat); outputStatInfo(r, stat); s2 = socket(AF_INET, SOCK_DGRAM, 0); r = onload_fd_stat(s2, stat); outputStatInfo(r, stat); close(s1); close(s2); /* tidy up */ free(stat); }
JNIEXPORT jint JNICALL Java_OnloadExt_SetStackName ( JNIEnv *env, jclass cls, jint who, jint scope, jstring stackname ) { static int have_dont_accel = 0; static jstring dont_accel; jint rval; if( ! have_dont_accel ) { jfieldID fid = (*env)->GetStaticFieldID(env, cls, "ONLOAD_DONT_ACCELERATE", "Ljava/lang/String;"); dont_accel = (*env)->GetStaticObjectField(env, cls, fid); have_dont_accel = 1; } if( stackname == dont_accel ) rval = onload_set_stackname( who, scope, ONLOAD_DONT_ACCELERATE ); else { char* native_stackname = JNU_GetStringNativeChars(env, stackname); rval = onload_set_stackname( who, scope, native_stackname ); free( (void*) native_stackname ); } return rval; }
static void do_server(void) { int sl; int sa1; int sa2; int r; int i; struct onload_stat * stat; struct sockaddr_in saddr; stat = malloc(sizeof(struct onload_stat)); /* create a named stack for the socket */ if( onload_set_stackname(ONLOAD_ALL_THREADS, ONLOAD_SCOPE_GLOBAL, "initial") ) perror("Error setting stackname:"); /* set up a basic listening TCP socket */ sl = socket(AF_INET, SOCK_STREAM, 0); bzero(&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(20001); bind(sl, (struct sockaddr *) &saddr, sizeof(saddr)); listen(sl, 10); /* accept the first connection */ printf("Accepting first connection\n"); sa1 = accept(sl, (struct sockaddr *) NULL, NULL); r = onload_fd_stat(sa1, stat); outputStatInfo(r, stat); /* create a new named stack to move the socket to */ if( onload_set_stackname(ONLOAD_ALL_THREADS, ONLOAD_SCOPE_GLOBAL, "stack1") ) perror("Error setting stackname:"); /* move the socket into the current (stack1) stack */ printf("Moving first connection\n"); onload_move_fd(sa1); r = onload_fd_stat(sa1, stat); outputStatInfo(r, stat); /* accept the second connection */ printf("Accepting second connection\n"); sa2 = accept(sl, (struct sockaddr *) NULL, NULL); r = onload_fd_stat(sa2, stat); outputStatInfo(r, stat); /* create a new named stack to move the socket to */ if( onload_set_stackname(ONLOAD_ALL_THREADS, ONLOAD_SCOPE_GLOBAL, "stack2") ) perror("Error setting stackname:"); /* move the socket into the current (stack2) stack */ printf("Moving second connection\n"); onload_move_fd(sa2); r = onload_fd_stat(sa2, stat); outputStatInfo(r, stat); /* tidy up */ close(sl); close(sa1); close(sa2); free(stat); }