1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* rpcb_stat.c
* Allows for gathering of statistics
*
* Copyright (c) 1990 by Sun Microsystems, Inc.
*/
/*
* Copyright 2014 Nexenta Systems, Inc. All rights reserved.
*/
#include <stdio.h>
#include <netconfig.h>
#include <rpc/rpc.h>
#include <rpc/rpcb_prot.h>
#include <sys/stat.h>
#ifdef PORTMAP
#include <rpc/pmap_prot.h>
#endif
#include <stdlib.h>
#include <atomic.h>
#include <assert.h>
#include <thread.h>
#include <synch.h>
#include <string.h>
#include "rpcbind.h"
static rpcb_stat_byvers inf;
static rwlock_t inf_lock = DEFAULTRWLOCK;
void
rpcbs_procinfo(int rtype, rpcproc_t proc)
{
assert(rtype >= 0 && rtype < RPCBVERS_STAT);
#ifdef PORTMAP
if ((rtype == RPCBVERS_2_STAT) && (proc > rpcb_highproc_2))
return;
#else
assert(rtype != RPCBVERS_2_STAT);
#endif
if ((rtype == RPCBVERS_3_STAT) && (proc > rpcb_highproc_3))
return;
if ((rtype == RPCBVERS_4_STAT) && (proc > rpcb_highproc_4))
return;
atomic_add_int((uint_t *)&inf[rtype].info[proc], 1);
}
void
rpcbs_set(int rtype, bool_t success)
{
assert(rtype >= 0 && rtype < RPCBVERS_STAT);
if (success == FALSE)
return;
atomic_add_int((uint_t *)&inf[rtype].setinfo, 1);
}
void
rpcbs_unset(int rtype, bool_t success)
{
assert(rtype >= 0 && rtype < RPCBVERS_STAT);
if (success == FALSE)
return;
atomic_add_int((uint_t *)&inf[rtype].unsetinfo, 1);
}
void
rpcbs_getaddr(int rtype, rpcprog_t prog, rpcvers_t vers, char *netid,
char *uaddr)
{
rpcbs_addrlist *al;
rpcbs_addrlist *s;
rpcbs_addrlist *wal;
struct netconfig *nconf;
assert(rtype >= 0 && rtype < RPCBVERS_STAT);
/*
* First try with read lock only.
*/
(void) rw_rdlock(&inf_lock);
for (s = al = inf[rtype].addrinfo; al; al = al->next) {
if ((al->prog == prog) && (al->vers == vers) &&
(strcmp(al->netid, netid) == 0)) {
(void) rw_unlock(&inf_lock);
if ((uaddr == NULL) || (uaddr[0] == '\0'))
atomic_add_int((uint_t *)&al->failure, 1);
else
atomic_add_int((uint_t *)&al->success, 1);
return;
}
}
(void) rw_unlock(&inf_lock);
/*
* If not found, we will likely need to add a new entry,
* so pre-allocate it, and then try to search again with write lock.
*/
nconf = rpcbind_get_conf(netid);
if (nconf == NULL) {
return;
}
al = (rpcbs_addrlist *) malloc(sizeof (rpcbs_addrlist));
if (al == NULL) {
return;
}
al->prog = prog;
al->vers = vers;
al->netid = nconf->nc_netid;
if ((uaddr == NULL) || (uaddr[0] == '\0')) {
al->failure = 1;
al->success = 0;
} else {
al->failure = 0;
al->success = 1;
}
(void) rw_wrlock(&inf_lock);
for (wal = inf[rtype].addrinfo; wal != s; wal = wal->next) {
if ((wal->prog == prog) && (wal->vers == vers) &&
(strcmp(wal->netid, netid) == 0)) {
(void) rw_unlock(&inf_lock);
free(al);
if ((uaddr == NULL) || (uaddr[0] == '\0'))
atomic_add_int((uint_t *)&wal->failure, 1);
else
atomic_add_int((uint_t *)&wal->success, 1);
return;
}
}
al->next = inf[rtype].addrinfo;
inf[rtype].addrinfo = al;
(void) rw_unlock(&inf_lock);
}
/*
* rpcbproc - rpcbind proc number on which this was called
*/
void
rpcbs_rmtcall(int rtype, rpcproc_t rpcbproc, rpcprog_t prog, rpcvers_t vers,
rpcproc_t proc, char *netid, rpcblist_ptr rbl)
{
rpcbs_rmtcalllist *rl;
rpcbs_rmtcalllist *s;
rpcbs_rmtcalllist *wrl;
struct netconfig *nconf;
assert(rtype >= 0 && rtype < RPCBVERS_STAT);
/*
* First try with read lock only.
*/
(void) rw_rdlock(&inf_lock);
for (s = rl = inf[rtype].rmtinfo; rl; rl = rl->next) {
if ((rl->prog == prog) && (rl->vers == vers) &&
(rl->proc == proc) && (strcmp(rl->netid, netid) == 0)) {
(void) rw_unlock(&inf_lock);
if ((rbl == NULL) || (rbl->rpcb_map.r_vers != vers))
atomic_add_int((uint_t *)&rl->failure, 1);
else
atomic_add_int((uint_t *)&rl->success, 1);
if (rpcbproc == RPCBPROC_INDIRECT)
atomic_add_int((uint_t *)&rl->indirect, 1);
return;
}
}
(void) rw_unlock(&inf_lock);
/*
* If not found, we will likely need to add a new entry,
* so pre-allocate it, and then try to search again with write lock.
*/
nconf = rpcbind_get_conf(netid);
if (nconf == NULL) {
return;
}
rl = (rpcbs_rmtcalllist *) malloc(sizeof (rpcbs_rmtcalllist));
if (rl == NULL) {
return;
}
rl->prog = prog;
rl->vers = vers;
rl->proc = proc;
rl->netid = nconf->nc_netid;
if ((rbl == NULL) || (rbl->rpcb_map.r_vers != vers)) {
rl->failure = 1;
rl->success = 0;
} else {
rl->failure = 0;
rl->success = 1;
}
rl->indirect = rpcbproc == RPCBPROC_INDIRECT ? 1 : 0;
(void) rw_wrlock(&inf_lock);
for (wrl = inf[rtype].rmtinfo; wrl != s; wrl = wrl->next) {
if ((wrl->prog == prog) && (wrl->vers == vers) &&
(wrl->proc == proc) && (strcmp(wrl->netid, netid) == 0)) {
(void) rw_unlock(&inf_lock);
free(rl);
if ((rbl == NULL) || (rbl->rpcb_map.r_vers != vers))
atomic_add_int((uint_t *)&wrl->failure, 1);
else
atomic_add_int((uint_t *)&wrl->success, 1);
if (rpcbproc == RPCBPROC_INDIRECT)
atomic_add_int((uint_t *)&wrl->indirect, 1);
return;
}
}
rl->next = inf[rtype].rmtinfo;
inf[rtype].rmtinfo = rl;
(void) rw_unlock(&inf_lock);
}
/* ARGSUSED */
bool_t
rpcbproc_getstat(void *argp, rpcb_stat_byvers **result)
{
/*
* inf_lock is unlocked in xdr_rpcb_stat_byvers_ptr()
*/
(void) rw_rdlock(&inf_lock);
*result = &inf;
return (TRUE);
}
bool_t
xdr_rpcb_stat_byvers_ptr(XDR *xdrs, rpcb_stat_byvers **objp)
{
if (xdrs->x_op == XDR_FREE) {
/*
* inf_lock is locked in rpcbproc_getstat()
*/
(void) rw_unlock(&inf_lock);
return (TRUE);
}
return (xdr_rpcb_stat_byvers(xdrs, (rpcb_stat *)*objp));
}
|