SOURCES: openssh-4.9p1-hpn13v2.diff (NEW) - raw from http://www.ps...

glen glen at pld-linux.org
Fri Apr 4 09:32:58 CEST 2008


Author: glen                         Date: Fri Apr  4 07:32:58 2008 GMT
Module: SOURCES                       Tag: HEAD
---- Log message:
- raw from http://www.psc.edu/networking/projects/hpn-ssh/openssh-4.9p1-hpn13v2.diff.gz

---- Files affected:
SOURCES:
   openssh-4.9p1-hpn13v2.diff (NONE -> 1.1)  (NEW)

---- Diffs:

================================================================
Index: SOURCES/openssh-4.9p1-hpn13v2.diff
diff -u /dev/null SOURCES/openssh-4.9p1-hpn13v2.diff:1.1
--- /dev/null	Fri Apr  4 09:32:58 2008
+++ SOURCES/openssh-4.9p1-hpn13v2.diff	Fri Apr  4 09:32:53 2008
@@ -0,0 +1,2361 @@
+diff -NupwB openssh-4.9p1/auth2.c openssh-4.9p1-hpn13/auth2.c
+--- openssh-4.9p1/auth2.c	2007-10-26 00:26:16.000000000 -0400
++++ openssh-4.9p1-hpn13/auth2.c	2008-04-01 13:03:59.000000000 -0400
+@@ -44,6 +44,7 @@
+ #include "dispatch.h"
+ #include "pathnames.h"
+ #include "buffer.h"
++#include "canohost.h"
+ 
+ #ifdef GSSAPI
+ #include "ssh-gss.h"
+@@ -67,6 +68,9 @@ extern Authmethod method_hostbased;
+ extern Authmethod method_gssapi;
+ #endif
+ 
++static int log_flag = 0;
++
++
+ Authmethod *authmethods[] = {
+ 	&method_none,
+ 	&method_pubkey,
+@@ -150,6 +154,11 @@ input_userauth_request(int type, u_int32
+ 	service = packet_get_string(NULL);
+ 	method = packet_get_string(NULL);
+ 	debug("userauth-request for user %s service %s method %s", user, service, method);
++	if (!log_flag) {
++		logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s", 
++		      get_remote_ipaddr(), get_remote_port(), user);
++		log_flag = 1;
++	}
+ 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
+ 
+ 	if ((style = strchr(user, ':')) != NULL)
+diff -NupwB openssh-4.9p1/buffer.c openssh-4.9p1-hpn13/buffer.c
+--- openssh-4.9p1/buffer.c	2006-08-04 22:39:39.000000000 -0400
++++ openssh-4.9p1-hpn13/buffer.c	2008-04-01 13:03:59.000000000 -0400
+@@ -127,7 +127,9 @@ restart:
+ 
+ 	/* Increase the size of the buffer and retry. */
+ 	newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
+-	if (newlen > BUFFER_MAX_LEN)
++	/* need it to be slightly larger than the MAX LEN for this */
++	/* still investigating *why* but this works for now -cjr */
++	if (newlen > (BUFFER_MAX_LEN_HPN + BUFFER_MAX_LEN)) 
+ 		fatal("buffer_append_space: alloc %u not supported",
+ 		    newlen);
+ 	buffer->buf = xrealloc(buffer->buf, 1, newlen);
+diff -NupwB openssh-4.9p1/buffer.h openssh-4.9p1-hpn13/buffer.h
+--- openssh-4.9p1/buffer.h	2006-08-04 22:39:39.000000000 -0400
++++ openssh-4.9p1-hpn13/buffer.h	2008-04-01 13:03:59.000000000 -0400
+@@ -16,6 +16,9 @@
+ #ifndef BUFFER_H
+ #define BUFFER_H
+ 
++/* move the following to a more appropriate place and name */
++#define BUFFER_MAX_LEN_HPN          0x4000000  /* 64MB */
++
+ typedef struct {
+ 	u_char	*buf;		/* Buffer for data. */
+ 	u_int	 alloc;		/* Number of bytes allocated for data. */
+diff -NupwB openssh-4.9p1/channels.c openssh-4.9p1-hpn13/channels.c
+--- openssh-4.9p1/channels.c	2008-02-10 06:25:24.000000000 -0500
++++ openssh-4.9p1-hpn13/channels.c	2008-04-01 13:03:59.000000000 -0400
+@@ -311,6 +311,7 @@ channel_new(char *ctype, int type, int r
+ 	c->local_window_max = window;
+ 	c->local_consumed = 0;
+ 	c->local_maxpacket = maxpack;
++	c->dynamic_window = 0;
+ 	c->remote_id = -1;
+ 	c->remote_name = xstrdup(remote_name);
+ 	c->remote_window = 0;
+@@ -765,11 +766,35 @@ channel_pre_open_13(Channel *c, fd_set *
+ 		FD_SET(c->sock, writeset);
+ }
+ 
++int channel_tcpwinsz () {
++        u_int32_t tcpwinsz = 0;
++        socklen_t optsz = sizeof(tcpwinsz);
++	int ret = -1;
++
++	/* if we aren't on a socket return 128KB*/
++	if(!packet_connection_is_on_socket()) 
++	    return(128*1024);
++	ret = getsockopt(packet_get_connection_in(),
++			 SOL_SOCKET, SO_RCVBUF, &tcpwinsz, &optsz);
++	/* return no more than 64MB */
++	if ((ret == 0) && tcpwinsz > BUFFER_MAX_LEN_HPN)
++	    tcpwinsz = BUFFER_MAX_LEN_HPN;
++	debug2("tcpwinsz: %d for connection: %d", tcpwinsz, 
++	       packet_get_connection_in());
++	return(tcpwinsz);
++}
++
+ static void
+ channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
+ {
+ 	u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
+ 
++        /* check buffer limits */
++	if ((!c->tcpwinsz) || (c->dynamic_window > 0))
++    	    c->tcpwinsz = channel_tcpwinsz();
++	
++	limit = MIN(limit, 2 * c->tcpwinsz);
++	
+ 	if (c->istate == CHAN_INPUT_OPEN &&
+ 	    limit > 0 &&
+ 	    buffer_len(&c->input) < limit &&
+@@ -1661,14 +1686,21 @@ channel_check_window(Channel *c)
+ 	    c->local_maxpacket*3) ||
+ 	    c->local_window < c->local_window_max/2) &&
+ 	    c->local_consumed > 0) {
++		u_int addition = 0;
++		/* adjust max window size if we are in a dynamic environment */
++		if (c->dynamic_window && (c->tcpwinsz > c->local_window_max)) {
++			/* grow the window somewhat aggressively to maintain pressure */
++			addition = 1.5*(c->tcpwinsz - c->local_window_max);
++			c->local_window_max += addition;
++		}
+ 		packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
+ 		packet_put_int(c->remote_id);
+-		packet_put_int(c->local_consumed);
++		packet_put_int(c->local_consumed + addition);
+ 		packet_send();
+ 		debug2("channel %d: window %d sent adjust %d",
+ 		    c->self, c->local_window,
+ 		    c->local_consumed);
+-		c->local_window += c->local_consumed;
++		c->local_window += c->local_consumed + addition;
+ 		c->local_consumed = 0;
+ 	}
+ 	return 1;
+@@ -1871,11 +1903,12 @@ channel_after_select(fd_set *readset, fd
+ 
+ 
+ /* If there is data to send to the connection, enqueue some of it now. */
+-void
++int
+ channel_output_poll(void)
+ {
+ 	Channel *c;
+ 	u_int i, len;
++	int packet_length = 0;
+ 
+ 	for (i = 0; i < channels_alloc; i++) {
+ 		c = channels[i];
+@@ -1915,7 +1948,7 @@ channel_output_poll(void)
+ 					packet_start(SSH2_MSG_CHANNEL_DATA);
+ 					packet_put_int(c->remote_id);
+ 					packet_put_string(data, dlen);
+-					packet_send();
++					packet_length = packet_send();
+ 					c->remote_window -= dlen + 4;
+ 					xfree(data);
+ 				}
+@@ -1945,7 +1978,7 @@ channel_output_poll(void)
+ 				    SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
+ 				packet_put_int(c->remote_id);
+ 				packet_put_string(buffer_ptr(&c->input), len);
+-				packet_send();
++				packet_length = packet_send();
+ 				buffer_consume(&c->input, len);
+ 				c->remote_window -= len;
+ 			}
+@@ -1980,12 +2013,13 @@ channel_output_poll(void)
+ 			packet_put_int(c->remote_id);
+ 			packet_put_int(SSH2_EXTENDED_DATA_STDERR);
+ 			packet_put_string(buffer_ptr(&c->extended), len);
+-			packet_send();
++			packet_length = packet_send();
+ 			buffer_consume(&c->extended, len);
+ 			c->remote_window -= len;
+ 			debug2("channel %d: sent ext data %d", c->self, len);
+ 		}
+ 	}
++	return (packet_length);
+ }
+ 
+ 
+@@ -2342,7 +2376,8 @@ channel_set_af(int af)
+ 
+ static int
+ channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
+-    const char *host_to_connect, u_short port_to_connect, int gateway_ports)
++    const char *host_to_connect, u_short port_to_connect, int gateway_ports, 
++    int hpn_disabled, int hpn_buffer_size)
+ {
+ 	Channel *c;
+ 	int sock, r, success = 0, wildcard = 0, is_client;
+@@ -2456,9 +2491,15 @@ channel_setup_fwd_listener(int type, con
+ 			continue;
+ 		}
+ 		/* Allocate a channel number for the socket. */
++		/* explicitly test for hpn disabled option. if true use smaller window size */
++		if (hpn_disabled)
+ 		c = channel_new("port listener", type, sock, sock, -1,
+ 		    CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
+ 		    0, "port listener", 1);
++		else
++			c = channel_new("port listener", type, sock, sock, -1,
++		    	  hpn_buffer_size, CHAN_TCP_PACKET_DEFAULT,
++		    	  0, "port listener", 1); 
+ 		strlcpy(c->path, host, sizeof(c->path));
+ 		c->host_port = port_to_connect;
+ 		c->listening_port = listen_port;
+@@ -2495,20 +2536,22 @@ channel_cancel_rport_listener(const char
+ /* protocol local port fwd, used by ssh (and sshd in v1) */
+ int
+ channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
+-    const char *host_to_connect, u_short port_to_connect, int gateway_ports)
++    const char *host_to_connect, u_short port_to_connect, int gateway_ports, 
++    int hpn_disabled, int hpn_buffer_size)
+ {
+ 	return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
+ 	    listen_host, listen_port, host_to_connect, port_to_connect,
+-	    gateway_ports);
++	    gateway_ports, hpn_disabled, hpn_buffer_size);
+ }
+ 
+ /* protocol v2 remote port fwd, used by sshd */
+ int
+ channel_setup_remote_fwd_listener(const char *listen_address,
+-    u_short listen_port, int gateway_ports)
++    u_short listen_port, int gateway_ports, int hpn_disabled, int hpn_buffer_size)
+ {
+ 	return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
+-	    listen_address, listen_port, NULL, 0, gateway_ports);
++	    listen_address, listen_port, NULL, 0, gateway_ports, 
++	    hpn_disabled, hpn_buffer_size);
+ }
+ 
+ /*
+@@ -2623,7 +2666,8 @@ channel_request_rforward_cancel(const ch
+  * message if there was an error).
+  */
+ int
+-channel_input_port_forward_request(int is_root, int gateway_ports)
++channel_input_port_forward_request(int is_root, int gateway_ports,
++				   int hpn_disabled, int hpn_buffer_size)
+ {
+ 	u_short port, host_port;
+ 	int success = 0;
+@@ -2649,7 +2693,7 @@ channel_input_port_forward_request(int i
+ 
+ 	/* Initiate forwarding */
+ 	success = channel_setup_local_fwd_listener(NULL, port, hostname,
+-	    host_port, gateway_ports);
++	    host_port, gateway_ports, hpn_disabled, hpn_buffer_size);
+ 
+ 	/* Free the argument string. */
+ 	xfree(hostname);
+@@ -2853,7 +2897,8 @@ channel_send_window_changes(void)
+  */
+ int
+ x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
+-    int single_connection, u_int *display_numberp, int **chanids)
++    int single_connection, u_int *display_numberp, int **chanids, 
++    int hpn_disabled, int hpn_buffer_size)
+ {
+ 	Channel *nc = NULL;
+ 	int display_number, sock;
+@@ -2950,10 +2995,17 @@ x11_create_display_inet(int x11_display_
+ 	*chanids = xcalloc(num_socks + 1, sizeof(**chanids));
+ 	for (n = 0; n < num_socks; n++) {
+ 		sock = socks[n];
++		/* Is this really necassary? */
++		if (hpn_disabled) 
+ 		nc = channel_new("x11 listener",
+ 		    SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
+ 		    CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
+ 		    0, "X11 inet listener", 1);
++		else 
++			nc = channel_new("x11 listener",
++			    SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
++			    hpn_buffer_size, CHAN_X11_PACKET_DEFAULT,
++			    0, "X11 inet listener", 1);
+ 		nc->single_connection = single_connection;
+ 		(*chanids)[n] = nc->self;
+ 	}
+diff -NupwB openssh-4.9p1/channels.h openssh-4.9p1-hpn13/channels.h
+--- openssh-4.9p1/channels.h	2007-06-12 09:38:54.000000000 -0400
++++ openssh-4.9p1-hpn13/channels.h	2008-04-01 13:03:59.000000000 -0400
+@@ -98,8 +98,10 @@ struct Channel {
+ 	u_int	local_window_max;
+ 	u_int	local_consumed;
+ 	u_int	local_maxpacket;
++	int	dynamic_window;
+ 	int     extended_usage;
+ 	int	single_connection;
++	u_int 	tcpwinsz;	
+ 
+ 	char   *ctype;		/* type */
+ 
+@@ -122,9 +124,11 @@ struct Channel {
+ 
+ /* default window/packet sizes for tcp/x11-fwd-channel */
+ #define CHAN_SES_PACKET_DEFAULT	(32*1024)
+-#define CHAN_SES_WINDOW_DEFAULT	(64*CHAN_SES_PACKET_DEFAULT)
++#define CHAN_SES_WINDOW_DEFAULT	(4*CHAN_SES_PACKET_DEFAULT)
++
+ #define CHAN_TCP_PACKET_DEFAULT	(32*1024)
+-#define CHAN_TCP_WINDOW_DEFAULT	(64*CHAN_TCP_PACKET_DEFAULT)
++#define CHAN_TCP_WINDOW_DEFAULT	(4*CHAN_TCP_PACKET_DEFAULT)
++
+ #define CHAN_X11_PACKET_DEFAULT	(16*1024)
+ #define CHAN_X11_WINDOW_DEFAULT	(4*CHAN_X11_PACKET_DEFAULT)
+ 
+@@ -193,7 +197,7 @@ void	 channel_input_window_adjust(int, u
+ 
+ void	 channel_prepare_select(fd_set **, fd_set **, int *, u_int*, int);
+ void     channel_after_select(fd_set *, fd_set *);
+-void     channel_output_poll(void);
++int      channel_output_poll(void);
+ 
+ int      channel_not_very_much_buffered_data(void);
+ void     channel_close_all(void);
+@@ -208,21 +212,21 @@ void	 channel_add_permitted_opens(char *
+ int	 channel_add_adm_permitted_opens(char *, int);
+ void	 channel_clear_permitted_opens(void);
+ void	 channel_clear_adm_permitted_opens(void);
+-int      channel_input_port_forward_request(int, int);
++int      channel_input_port_forward_request(int, int, int, int);
+ int	 channel_connect_to(const char *, u_short);
+ int	 channel_connect_by_listen_address(u_short);
+ int	 channel_request_remote_forwarding(const char *, u_short,
+ 	     const char *, u_short);
+ int	 channel_setup_local_fwd_listener(const char *, u_short,
+-	     const char *, u_short, int);
++	     const char *, u_short, int, int, int);
+ void	 channel_request_rforward_cancel(const char *host, u_short port);
+-int	 channel_setup_remote_fwd_listener(const char *, u_short, int);
++int	 channel_setup_remote_fwd_listener(const char *, u_short, int, int, int);
+ int	 channel_cancel_rport_listener(const char *, u_short);
+ 
+ /* x11 forwarding */
+ 
+ int	 x11_connect_display(void);
+-int	 x11_create_display_inet(int, int, int, u_int *, int **);
++int	 x11_create_display_inet(int, int, int, u_int *, int **, int, int);
+ void     x11_input_open(int, u_int32_t, void *);
+ void	 x11_request_forwarding_with_spoofing(int, const char *, const char *,
+ 	    const char *);
+diff -NupwB openssh-4.9p1/cipher.c openssh-4.9p1-hpn13/cipher.c
+--- openssh-4.9p1/cipher.c	2006-08-04 22:39:39.000000000 -0400
++++ openssh-4.9p1-hpn13/cipher.c	2008-04-01 13:03:59.000000000 -0400
+@@ -55,6 +55,7 @@ extern const EVP_CIPHER *evp_ssh1_bf(voi
+ extern const EVP_CIPHER *evp_ssh1_3des(void);
+ extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
+ extern const EVP_CIPHER *evp_aes_128_ctr(void);
++extern const EVP_CIPHER *evp_aes_ctr_mt(void);
+ extern void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
+ 
+ struct Cipher {
+@@ -81,9 +82,9 @@ struct Cipher {
+ 	{ "aes256-cbc",		SSH_CIPHER_SSH2, 16, 32, 0, EVP_aes_256_cbc },
+ 	{ "rijndael-cbc at lysator.liu.se",
+ 				SSH_CIPHER_SSH2, 16, 32, 0, EVP_aes_256_cbc },
+-	{ "aes128-ctr",		SSH_CIPHER_SSH2, 16, 16, 0, evp_aes_128_ctr },
+-	{ "aes192-ctr",		SSH_CIPHER_SSH2, 16, 24, 0, evp_aes_128_ctr },
+-	{ "aes256-ctr",		SSH_CIPHER_SSH2, 16, 32, 0, evp_aes_128_ctr },
++	{ "aes128-ctr",		SSH_CIPHER_SSH2, 16, 16, 0, evp_aes_ctr_mt },
++	{ "aes192-ctr",		SSH_CIPHER_SSH2, 16, 24, 0, evp_aes_ctr_mt },
++	{ "aes256-ctr",		SSH_CIPHER_SSH2, 16, 32, 0, evp_aes_ctr_mt },
+ #ifdef USE_CIPHER_ACSS
+ 	{ "acss at openssh.org",	SSH_CIPHER_SSH2, 16, 5, 0, EVP_acss },
+ #endif
+@@ -156,7 +157,8 @@ ciphers_valid(const char *names)
+ 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
+ 	    (p = strsep(&cp, CIPHER_SEP))) {
+ 		c = cipher_by_name(p);
+-		if (c == NULL || c->number != SSH_CIPHER_SSH2) {
++		if (c == NULL || (c->number != SSH_CIPHER_SSH2 && 
++c->number != SSH_CIPHER_NONE)) {
+ 			debug("bad cipher %s [%s]", p, names);
+ 			xfree(cipher_list);
+ 			return 0;
+@@ -330,6 +332,7 @@ cipher_get_keyiv(CipherContext *cc, u_ch
+ 	int evplen;
+ 
+ 	switch (c->number) {
++	case SSH_CIPHER_NONE:
+ 	case SSH_CIPHER_SSH2:
+ 	case SSH_CIPHER_DES:
+ 	case SSH_CIPHER_BLOWFISH:
+@@ -364,6 +367,7 @@ cipher_set_keyiv(CipherContext *cc, u_ch
+ 	int evplen = 0;
+ 
+ 	switch (c->number) {
++	case SSH_CIPHER_NONE:
+ 	case SSH_CIPHER_SSH2:
+ 	case SSH_CIPHER_DES:
+ 	case SSH_CIPHER_BLOWFISH:
+diff -NupwB openssh-4.9p1/cipher-ctr-mt.c openssh-4.9p1-hpn13/cipher-ctr-mt.c
+--- openssh-4.9p1/cipher-ctr-mt.c	1969-12-31 19:00:00.000000000 -0500
++++ openssh-4.9p1-hpn13/cipher-ctr-mt.c	2008-04-01 13:03:59.000000000 -0400
+@@ -0,0 +1,473 @@
++/*
++ * OpenSSH Multi-threaded AES-CTR Cipher
++ *
++ * Author: Benjamin Bennett <ben at psc.edu>
++ * Copyright (c) 2008 Pittsburgh Supercomputing Center. All rights reserved.
++ *
++ * Based on original OpenSSH AES-CTR cipher. Small portions remain unchanged,
++ * Copyright (c) 2003 Markus Friedl <markus at openbsd.org>
++ *
++ * Permission to use, copy, modify, and distribute this software for any
++ * purpose with or without fee is hereby granted, provided that the above
++ * copyright notice and this permission notice appear in all copies.
++ *
++ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
++ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
++ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
++ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
++ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
++ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
++ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
++ */
++#include "includes.h"
++
++#include <sys/types.h>
++
++#include <stdarg.h>
++#include <string.h>
++
++#include <openssl/evp.h>
++
++#include "xmalloc.h"
++#include "log.h"
++
++/* compatibility with old or broken OpenSSL versions */
++#include "openbsd-compat/openssl-compat.h"
++
++#ifndef USE_BUILTIN_RIJNDAEL
++#include <openssl/aes.h>
++#endif
++
++#include <pthread.h>
++
++/*-------------------- TUNABLES --------------------*/
++/* Number of pregen threads to use */
++#define CIPHER_THREADS	2
++
++/* Number of keystream queues */
++#define NUMKQ		(CIPHER_THREADS + 2)
++
++/* Length of a keystream queue */
++#define KQLEN		4096
++
++/* Processor cacheline length */
++#define CACHELINE_LEN	64
++
++/* Collect thread stats and print at cancellation when in debug mode */
++// #define CIPHER_THREAD_STATS
++
++/* Use single-byte XOR instead of 8-byte XOR */
++// #define CIPHER_BYTE_XOR
++/*-------------------- END TUNABLES --------------------*/
++
++
++const EVP_CIPHER *evp_aes_ctr_mt(void);
++
++#ifdef CIPHER_THREAD_STATS
++/*
++ * Struct to collect thread stats
++ */
++struct thread_stats {
++	u_int	fills;
++	u_int	skips;
++	u_int	waits;
++	u_int	drains;
++};
++
++/*
++ * Debug print the thread stats
++ * Use with pthread_cleanup_push for displaying at thread cancellation
++ */
++static void
++thread_loop_stats(void *x)
++{
++	struct thread_stats *s = x;
++
++	debug("tid %lu - %u fills, %u skips, %u waits", pthread_self(),
++			s->fills, s->skips, s->waits);
++}
++
++ #define STATS_STRUCT(s)	struct thread_stats s
++ #define STATS_INIT(s)		{ memset(&s, 0, sizeof(s)); }
++ #define STATS_FILL(s)		{ s.fills++; }
++ #define STATS_SKIP(s)		{ s.skips++; }
++ #define STATS_WAIT(s)		{ s.waits++; }
++ #define STATS_DRAIN(s)		{ s.drains++; }
++#else
++ #define STATS_STRUCT(s)
++ #define STATS_INIT(s)
++ #define STATS_FILL(s)
++ #define STATS_SKIP(s)
++ #define STATS_WAIT(s)
++ #define STATS_DRAIN(s)
++#endif
++
++/* Keystream Queue state */
++enum {
++	KQINIT,
++	KQEMPTY,
++	KQFILLING,
++	KQFULL,
++	KQDRAINING
++};
++
++/* Keystream Queue struct */
++struct kq {
++	u_char		keys[KQLEN][AES_BLOCK_SIZE];
++	u_char		ctr[AES_BLOCK_SIZE];
++	u_char		pad0[CACHELINE_LEN];
++	volatile int	qstate;
++	pthread_mutex_t	lock;
++	pthread_cond_t	cond;
++	u_char		pad1[CACHELINE_LEN];
++};
++
++/* Context struct */
++struct ssh_aes_ctr_ctx
++{
++	struct kq	q[NUMKQ];
++	AES_KEY		aes_ctx;
++	STATS_STRUCT(stats);
++	u_char		aes_counter[AES_BLOCK_SIZE];
++	pthread_t	tid[CIPHER_THREADS];
++	int		state;
++	int		qidx;
++	int		ridx;
++};
++
++/* <friedl>
++ * increment counter 'ctr',
++ * the counter is of size 'len' bytes and stored in network-byte-order.
++ * (LSB at ctr[len-1], MSB at ctr[0])
++ */
++static void
++ssh_ctr_inc(u_char *ctr, u_int len)
++{
++	int i;
++
++	for (i = len - 1; i >= 0; i--)
++		if (++ctr[i])	/* continue on overflow */
++			return;
++}
++
++/*
++ * Add num to counter 'ctr'
++ */
++static void
++ssh_ctr_add(u_char *ctr, uint32_t num, u_int len)
++{
++	int i;
++	uint16_t n;
++
++	for (n = 0, i = len - 1; i >= 0 && (num || n); i--) {
++		n = ctr[i] + (num & 0xff) + n;
++		num >>= 8;
++		ctr[i] = n & 0xff;
++		n >>= 8;
++	}
++}
++
++/*
++ * Threads may be cancelled in a pthread_cond_wait, we must free the mutex
++ */
++static void
++thread_loop_cleanup(void *x)
++{
++	pthread_mutex_unlock((pthread_mutex_t *)x);
++}
++
++/*
++ * The life of a pregen thread:
++ *    Find empty keystream queues and fill them using their counter.
++ *    When done, update counter for the next fill.
++ */
++static void *
++thread_loop(void *x)
++{
++	AES_KEY key;
++	STATS_STRUCT(stats);
++	struct ssh_aes_ctr_ctx *c = x;
++	struct kq *q;
++	int i;
++	int qidx;
++
++	/* Threads stats on cancellation */
++	STATS_INIT(stats);
++#ifdef CIPHER_THREAD_STATS
<<Diff was trimmed, longer than 597 lines>>


More information about the pld-cvs-commit mailing list