nfs-ganesha 1.4
|
00001 00002 #include "ganesha_rpc.h" 00003 #include "config_parsing.h" 00004 #include "nfs_stat.h" 00005 #include "nfs_ip_stats.h" 00006 #include <stdlib.h> 00007 #include <string.h> 00008 #include <arpa/inet.h> 00009 #include "../MainNFSD/nfs_init.h" 00010 #include "nfs23.h" 00011 00012 #define MOUNT_PROGRAM 100005 00013 00014 hash_table_t * stats[1]; 00015 hash_table_t * ipstats; 00016 nfs_ip_stats_t * nfs_ip_stats; 00017 nfs_parameter_t nfs_param; 00018 pool_t *ip_stats_pool; 00019 00020 #define EQUALS(a, b, msg, args...) do { \ 00021 if (a != b) { \ 00022 printf(msg "\n", ## args); \ 00023 exit(1); \ 00024 } \ 00025 } while(0) 00026 00027 #define CMP(a, b, n, msg) do { \ 00028 if (strncmp(a, b, n) != 0) { \ 00029 printf(msg "\n"); \ 00030 exit(1); \ 00031 } \ 00032 } while(0) 00033 00034 sockaddr_t ipv4a; 00035 sockaddr_t ipv4b; 00036 sockaddr_t ipv4c; 00037 sockaddr_t ipv6a; 00038 sockaddr_t ipv6b; 00039 sockaddr_t ipv6c; 00040 00041 void *rpc_tcp_socket_manager_thread(void *Arg) 00042 { 00043 return NULL; 00044 } 00045 00046 void create_ipv4(char * ip, int port, struct sockaddr_in * addr) 00047 { 00048 memset(addr, 0, sizeof(struct sockaddr_in)); 00049 addr->sin_family = AF_INET; 00050 addr->sin_port = port; 00051 inet_pton(AF_INET, ip, &(addr->sin_addr)); 00052 } 00053 00054 void create_ipv6(char * ip, int port, struct sockaddr_in6 * addr) 00055 { 00056 memset(addr, 0, sizeof(struct sockaddr_in6)); 00057 addr->sin6_family = AF_INET6; 00058 addr->sin6_port = port; 00059 inet_pton(AF_INET6, ip, &(addr->sin6_addr.s6_addr)); 00060 } 00061 00062 void create_svc_req(struct svc_req *req, rpcvers_t ver, rpcprog_t prog, rpcproc_t proc) 00063 { 00064 memset(req, 0, sizeof(struct svc_req)); 00065 req->rq_prog = prog; 00066 req->rq_vers = ver; 00067 req->rq_proc = proc; 00068 } 00069 00070 void nfs_set_ip_stats_param_default() 00071 { 00072 00073 nfs_param.ip_stats_param.hash_param.index_size = PRIME_IP_STATS; 00074 nfs_param.ip_stats_param.hash_param.alphabet_length = 10; /* ipaddr is a numerical decimal value */ 00075 nfs_param.ip_stats_param.hash_param.nb_node_prealloc = NB_PREALLOC_HASH_IP_STATS; 00076 nfs_param.ip_stats_param.hash_param.hash_func_key = ip_stats_value_hash_func; 00077 nfs_param.ip_stats_param.hash_param.hash_func_rbt = ip_stats_rbt_hash_func; 00078 nfs_param.ip_stats_param.hash_param.compare_key = compare_ip_stats; 00079 nfs_param.ip_stats_param.hash_param.key_to_str = display_ip_stats_key; 00080 nfs_param.ip_stats_param.hash_param.val_to_str = display_ip_stats_val; 00081 nfs_param.ip_stats_param.hash_param.name = "IP Stats"; 00082 nfs_param.ip_stats_param.hash_param.flags = HASH_FLAG_NONE; 00083 nfs_param.core_param.dump_stats_per_client = 1; 00084 00085 } 00086 00087 void init() 00088 { 00089 nfs_set_ip_stats_param_default(); 00090 ipstats = nfs_Init_ip_stats(nfs_param.ip_stats_param); 00091 ip_stats_pool = NULL; 00092 stats[0] = ipstats; 00093 00094 ip_stats_pool = 00095 pool_init("IP Stats Cache Pool", sizeof(nfs_ip_stats_t), 00096 pool_basic_substrate, 00097 NULL, NULL, NULL); 00098 00099 create_ipv4("10.10.5.1", 2048, (struct sockaddr_in * ) &ipv4a); 00100 // create_ipv4("10.10.5.1", 2049, (struct sockaddr_in * ) &ipv4b); 00101 create_ipv4("10.10.5.2", 2048, (struct sockaddr_in * ) &ipv4c); 00102 00103 #ifdef _USE_TIRPC 00104 create_ipv6("2001::1", 2048, (struct sockaddr_in6 *) &ipv6a); 00105 // create_ipv6("2001::1", 2049, (struct sockaddr_in6 *) &ipv6b); 00106 create_ipv6("2001::f:1", 2048, (struct sockaddr_in6 *) &ipv6c); 00107 #endif 00108 00109 } 00110 00111 void test_not_found() 00112 { 00113 nfs_ip_stats_t * out; 00114 EQUALS(nfs_ip_stats_get(ipstats, &ipv4a, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv4a yet"); 00115 // EQUALS(nfs_ip_stats_get(ipstats, &ipv4b, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv4b yet"); 00116 EQUALS(nfs_ip_stats_get(ipstats, &ipv4c, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv4c yet"); 00117 } 00118 00119 void test_not_found_bc() 00120 { 00121 nfs_ip_stats_t * out; 00122 EQUALS(nfs_ip_stats_get(ipstats, &ipv4a, &out), IP_STATS_SUCCESS, "There should be an ipv4a"); 00123 // EQUALS(nfs_ip_stats_get(ipstats, &ipv4b, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv4b yet"); 00124 EQUALS(nfs_ip_stats_get(ipstats, &ipv4c, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv4c yet"); 00125 } 00126 00127 void test_not_found_c() 00128 { 00129 nfs_ip_stats_t * out; 00130 EQUALS(nfs_ip_stats_get(ipstats, &ipv4a, &out), IP_STATS_SUCCESS, "There should be an ipv4a"); 00131 // EQUALS(nfs_ip_stats_get(ipstats, &ipv4b, &out), IP_STATS_SUCCESS, "There should be an ipv4b"); 00132 EQUALS(nfs_ip_stats_get(ipstats, &ipv4c, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv4c yet"); 00133 } 00134 00135 void test_not_found_none() 00136 { 00137 nfs_ip_stats_t * out; 00138 EQUALS(nfs_ip_stats_get(ipstats, &ipv4a, &out), IP_STATS_SUCCESS, "There should be an ipv4a"); 00139 // EQUALS(nfs_ip_stats_get(ipstats, &ipv4b, &out), IP_STATS_SUCCESS, "There should be an ipv4b"); 00140 EQUALS(nfs_ip_stats_get(ipstats, &ipv4c, &out), IP_STATS_SUCCESS, "There should be an ipv4c"); 00141 } 00142 00143 00144 void test_add() 00145 { 00146 int rc = nfs_ip_stats_add(ipstats, &ipv4a, ip_stats_pool); 00147 EQUALS(rc, IP_STATS_SUCCESS, "Can't add ipv4a, rc = %d", rc); 00148 test_not_found_bc(); 00149 00150 /* rc = nfs_ip_stats_add(ipstats, &ipv4b, &ip_stats_pool); */ 00151 /* EQUALS(rc, IP_STATS_SUCCESS, "Can't add ipv4b"); */ 00152 /* test_not_found_c(); */ 00153 00154 rc = nfs_ip_stats_add(ipstats, &ipv4c, ip_stats_pool); 00155 EQUALS(rc, IP_STATS_SUCCESS, "Can't add ipv4c"); 00156 test_not_found_none(); 00157 } 00158 00159 void test_incr() 00160 { 00161 struct svc_req req; 00162 int i = 0; 00163 00164 create_svc_req(&req, NFS_V3, NFS_PROGRAM, NFSPROC3_GETATTR); 00165 00166 for (i = 0; i < 10; i++) 00167 { 00168 nfs_ip_stats_incr(ipstats, &ipv4a, NFS_PROGRAM, MOUNT_PROGRAM, &req); 00169 } 00170 00171 create_svc_req(&req, NFS_V3, NFS_PROGRAM, NFSPROC3_READ); 00172 for (i = 0; i < 5; i++) { 00173 nfs_ip_stats_incr(ipstats, &ipv4a, NFS_PROGRAM, MOUNT_PROGRAM, &req); 00174 } 00175 00176 create_svc_req(&req, NFS_V3, NFS_PROGRAM, NFSPROC3_READDIRPLUS); 00177 for (i = 0; i < 7; i++) { 00178 nfs_ip_stats_incr(ipstats, &ipv4a, NFS_PROGRAM, MOUNT_PROGRAM, &req); 00179 } 00180 00181 } 00182 00183 // check that counts look right, including a check on something we didn't set so that it's actually removed correctly 00184 void test_get() 00185 { 00186 nfs_ip_stats_t * pnfs_ip_stats; 00187 00188 nfs_ip_stats_get(ipstats, &ipv4a, &pnfs_ip_stats); 00189 EQUALS(pnfs_ip_stats->nb_call, 22, "Number of total calls should be 22"); 00190 00191 EQUALS(pnfs_ip_stats->req_nfs3[NFSPROC3_GETATTR], 10, "Number of total calls should be 10"); 00192 EQUALS(pnfs_ip_stats->req_nfs3[NFSPROC3_READ], 5, "Number of total calls should be 5"); 00193 EQUALS(pnfs_ip_stats->req_nfs3[NFSPROC3_READDIRPLUS], 7, "Number of total calls should be 7"); 00194 EQUALS(pnfs_ip_stats->req_nfs3[NFSPROC3_WRITE], 0, "Number of total calls should be 0"); 00195 00196 } 00197 00198 // remove then re-add ipv4c to test the removal path 00199 void test_remove() 00200 { 00201 int rc; 00202 rc = nfs_ip_stats_remove(ipstats, &ipv4c, ip_stats_pool); 00203 test_not_found_c(); 00204 EQUALS(rc, IP_STATS_SUCCESS, "Can't remove ipv4c"); 00205 00206 rc = nfs_ip_stats_remove(ipstats, &ipv4c, ip_stats_pool); 00207 test_not_found_c(); 00208 EQUALS(rc, IP_STATS_NOT_FOUND, "Can't remove ipv4c"); 00209 00210 rc = nfs_ip_stats_add(ipstats, &ipv4c, ip_stats_pool); 00211 EQUALS(rc, IP_STATS_SUCCESS, "Can't add ipv4c"); 00212 test_not_found_none(); 00213 } 00214 00215 // The IPv6 versions of all of the tests 00216 00217 void test_not_found_6() 00218 { 00219 nfs_ip_stats_t * out; 00220 EQUALS(nfs_ip_stats_get(ipstats, &ipv6a, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv6a yet"); 00221 // EQUALS(nfs_ip_stats_get(ipstats, &ipv6b, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv6b yet"); 00222 EQUALS(nfs_ip_stats_get(ipstats, &ipv6c, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv6c yet"); 00223 } 00224 00225 void test_not_found_bc_6() 00226 { 00227 nfs_ip_stats_t * out; 00228 EQUALS(nfs_ip_stats_get(ipstats, &ipv6a, &out), IP_STATS_SUCCESS, "There should be an ipv6a"); 00229 // EQUALS(nfs_ip_stats_get(ipstats, &ipv6b, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv6b yet"); 00230 EQUALS(nfs_ip_stats_get(ipstats, &ipv6c, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv6c yet"); 00231 } 00232 00233 void test_not_found_c_6() 00234 { 00235 nfs_ip_stats_t * out; 00236 EQUALS(nfs_ip_stats_get(ipstats, &ipv6a, &out), IP_STATS_SUCCESS, "There should be an ipv6a"); 00237 // EQUALS(nfs_ip_stats_get(ipstats, &ipv6b, &out), IP_STATS_SUCCESS, "There should be an ipv6b"); 00238 EQUALS(nfs_ip_stats_get(ipstats, &ipv6c, &out), IP_STATS_NOT_FOUND, "There shouldn't be an ipv6c yet"); 00239 } 00240 00241 void test_not_found_none_6() 00242 { 00243 nfs_ip_stats_t * out; 00244 EQUALS(nfs_ip_stats_get(ipstats, &ipv6a, &out), IP_STATS_SUCCESS, "There should be an ipv6a"); 00245 // EQUALS(nfs_ip_stats_get(ipstats, &ipv6b, &out), IP_STATS_SUCCESS, "There should be an ipv6b"); 00246 EQUALS(nfs_ip_stats_get(ipstats, &ipv6c, &out), IP_STATS_SUCCESS, "There should be an ipv6c"); 00247 } 00248 00249 00250 void test_add_6() 00251 { 00252 int rc = nfs_ip_stats_add(ipstats, &ipv6a, ip_stats_pool); 00253 EQUALS(rc, IP_STATS_SUCCESS, "Can't add ipv6a, rc = %d", rc); 00254 test_not_found_bc_6(); 00255 00256 /* rc = nfs_ip_stats_add(ipstats, &ipv6b, &ip_stats_pool); */ 00257 /* EQUALS(rc, IP_STATS_SUCCESS, "Can't add ipv6b"); */ 00258 /* test_not_found_c_6(); */ 00259 00260 rc = nfs_ip_stats_add(ipstats, &ipv6c, ip_stats_pool); 00261 EQUALS(rc, IP_STATS_SUCCESS, "Can't add ipv6c"); 00262 test_not_found_none_6(); 00263 } 00264 00265 void test_incr_6() 00266 { 00267 struct svc_req req; 00268 int i = 0; 00269 00270 create_svc_req(&req, NFS_V3, NFS_PROGRAM, NFSPROC3_GETATTR); 00271 00272 for (i = 0; i < 10; i++) 00273 { 00274 nfs_ip_stats_incr(ipstats, &ipv6a, NFS_PROGRAM, MOUNT_PROGRAM, &req); 00275 } 00276 00277 create_svc_req(&req, NFS_V3, NFS_PROGRAM, NFSPROC3_READ); 00278 for (i = 0; i < 5; i++) { 00279 nfs_ip_stats_incr(ipstats, &ipv6a, NFS_PROGRAM, MOUNT_PROGRAM, &req); 00280 } 00281 00282 create_svc_req(&req, NFS_V3, NFS_PROGRAM, NFSPROC3_READDIRPLUS); 00283 for (i = 0; i < 7; i++) { 00284 nfs_ip_stats_incr(ipstats, &ipv6a, NFS_PROGRAM, MOUNT_PROGRAM, &req); 00285 } 00286 00287 } 00288 00289 // check that counts look right, including a check on something we didn't set so that it's actually removed correctly 00290 void test_get_6() 00291 { 00292 nfs_ip_stats_t * pnfs_ip_stats; 00293 00294 nfs_ip_stats_get(ipstats, &ipv6a, &pnfs_ip_stats); 00295 EQUALS(pnfs_ip_stats->nb_call, 22, "Number of total calls should be 22"); 00296 00297 EQUALS(pnfs_ip_stats->req_nfs3[NFSPROC3_GETATTR], 10, "Number of total calls should be 10"); 00298 EQUALS(pnfs_ip_stats->req_nfs3[NFSPROC3_READ], 5, "Number of total calls should be 5"); 00299 EQUALS(pnfs_ip_stats->req_nfs3[NFSPROC3_READDIRPLUS], 7, "Number of total calls should be 7"); 00300 EQUALS(pnfs_ip_stats->req_nfs3[NFSPROC3_WRITE], 0, "Number of total calls should be 0"); 00301 00302 } 00303 00304 // remove then re-add ipv6c to test the removal path 00305 void test_remove_6() 00306 { 00307 int rc; 00308 rc = nfs_ip_stats_remove(ipstats, &ipv6c, ip_stats_pool); 00309 test_not_found_c_6(); 00310 EQUALS(rc, IP_STATS_SUCCESS, "Can't remove ipv6c"); 00311 00312 rc = nfs_ip_stats_remove(ipstats, &ipv6c, ip_stats_pool); 00313 test_not_found_c_6(); 00314 EQUALS(rc, IP_STATS_NOT_FOUND, "Can't remove ipv6c"); 00315 00316 rc = nfs_ip_stats_add(ipstats, &ipv6c, ip_stats_pool); 00317 EQUALS(rc, IP_STATS_SUCCESS, "Can't add ipv6c"); 00318 test_not_found_none_6(); 00319 } 00320 00321 // 00322 00323 int main() 00324 { 00325 int i; 00326 00327 init(); 00328 test_not_found(); 00329 test_add(); 00330 test_incr(); 00331 test_get(); 00332 for (i = 0; i < 5; i++) { 00333 test_remove(); 00334 } 00335 00336 #ifdef _USE_TIRPC 00337 test_not_found_6(); 00338 test_add_6(); 00339 test_incr_6(); 00340 test_get_6(); 00341 for (i = 0; i < 5; i++) { 00342 test_remove_6(); 00343 } 00344 #endif 00345 00346 return 0; 00347 }