Added cipher acceleration to SILC Accelerator. Added cipher softacc.
[crypto.git] / lib / silcacc / softacc.c
1 /*
2
3   softacc.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 -  2008 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silccrypto.h"
21 #include "softacc.h"
22 #include "softacc_i.h"
23
24 /* Software accelerator is a thread-pool system where computationally
25    expensive operations are executed in multiple threads for the purpose of
26    off-loading and balancing the computations across multiple processors. */
27
28 /************************** Types and definitions ***************************/
29
30 /* Software accelerator operations */
31 const SilcAcceleratorStruct softacc =
32 {
33   "softacc", silc_softacc_init, silc_softacc_uninit,
34 #ifdef SILC_DIST_SOFTACC_PKCS
35   softacc_pkcs,
36 #else /* !SILC_DIST_SOFTACC_PKCS */
37   NULL,
38 #endif /* SILC_DIST_SOFTACC_PKCS */
39 #ifdef SILC_DIST_SOFTACC_CIPHER
40   softacc_cipher,
41 #else /* !SILC_DIST_SOFTACC_CIPHER */
42   NULL,
43 #endif /* SILC_DIST_SOFTACC_CIPHER */
44 };
45
46 /***************************** Accelerator API ******************************/
47
48 /* Initialize software accelerator */
49
50 SilcBool silc_softacc_init(SilcSchedule schedule, va_list va)
51 {
52   SilcSoftacc sa;
53   char *opt;
54
55   /* If already initialized, uninitialize first. */
56   sa = silc_global_get_var("softacc", FALSE);
57   if (sa)
58     silc_softacc_uninit();
59
60   sa = silc_global_set_var("softacc", sizeof(*sa), NULL, FALSE);
61   if (!sa)
62     return FALSE;
63
64   sa->schedule = schedule;
65   sa->min_threads = SILC_SOFTACC_MIN_THREADS;
66   sa->max_threads = SILC_SOFTACC_MAX_THREADS;
67   sa->cipher_threads = SILC_SOFTACC_CIPHER_THREADS;
68   sa->cipher_blocks = SILC_SOFTACC_CIPHER_BLOCKS;
69   sa->cipher_streams = SILC_SOFTACC_CIPHER_STREAMS;
70
71   /* Get options */
72   while ((opt = va_arg(va, char *))) {
73     if (!strcmp(opt, "min_threads"))
74       sa->min_threads = va_arg(va, SilcUInt32);
75     else if (!strcmp(opt, "max_threads"))
76       sa->max_threads = va_arg(va, SilcUInt32);
77     else if (!strcmp(opt, "cipher_threads"))
78       sa->cipher_threads = va_arg(va, SilcUInt32);
79     else if (!strcmp(opt, "cipher_blocks"))
80       sa->cipher_blocks = va_arg(va, SilcUInt32);
81     else if (!strcmp(opt, "cipher_streams"))
82       sa->cipher_streams = va_arg(va, SilcUInt32);
83   }
84
85   if (!sa->cipher_streams || !sa->cipher_blocks || !sa->cipher_threads)
86     return FALSE;
87
88   SILC_LOG_DEBUG(("Initialize software accelerator, min_threads %d, "
89                   "max_threads %d", sa->min_threads, sa->max_threads));
90
91   /* Start the thread pool */
92   sa->tp = silc_thread_pool_alloc(NULL, sa->min_threads,
93                                   sa->max_threads, TRUE);
94   if (!sa->tp) {
95     silc_global_del_var("softacc", FALSE);
96     return FALSE;
97   }
98
99   return TRUE;
100 }
101
102 /* Uninitialize */
103
104 SilcBool silc_softacc_uninit(void)
105 {
106   SilcSoftacc sa;
107
108   sa = silc_global_get_var("softacc", FALSE);
109   if (!sa)
110     return FALSE;
111
112   SILC_LOG_DEBUG(("Uninitialize software accelerator"));
113
114   silc_thread_pool_free(sa->tp, TRUE);
115   silc_global_del_var("softacc", FALSE);
116
117   return TRUE;
118 }