xref: /titanic_50/usr/src/uts/common/ipp/flowacct/flowacct.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/kmem.h>
31 #include <sys/conf.h>
32 #include <sys/atomic.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip6.h>
36 #include <sys/socket.h>
37 #include <sys/acct.h>
38 #include <sys/exacct.h>
39 #include <inet/common.h>
40 #include <inet/ip.h>
41 #include <inet/ip6.h>
42 #include <sys/ddi.h>
43 #include <sys/strsun.h>
44 #include <ipp/flowacct/flowacct_impl.h>
45 
46 /*
47  * flowacct - IPQoS accounting module. The module maintains an array
48  * of 256 hash buckets. When the action routine is invoked for a flow,
49  * if the flow (identified by the 5-tuple: saddr, daddr, sport, dport, proto)
50  * is already present in the flow table (indexed by the hash function FLOW_HASH)
51  * then a check is made to see if an item for this flow with the same
52  * dsfield, projid & user id is present. If it is, then the number of packets
53  * and the bytes are incremented for that item. If the item does
54  * not exist a new item is added for the flow. If the flow is not present
55  * an entry is made for the flow.
56  *
57  * A timer runs thru the table and writes all the flow items that have
58  * timed out to the accounting file (via exacct PSARC/1999/119), if present
59  * Configuration commands to change the timing interval is provided. The
60  * flow timeout value can also be configured. While the timeout is in nsec,
61  * the flow timer interval is in usec.
62  * Information for an active flow can be obtained by using kstats.
63  */
64 
65 /* Used in computing the hash index */
66 #define	FLOWACCT_ADDR_HASH(addr) 			\
67 	((addr).s6_addr8[8] ^ (addr).s6_addr8[9] ^ 	\
68 	(addr).s6_addr8[10] ^ (addr).s6_addr8[13] ^ 	\
69 	(addr).s6_addr8[14] ^ (addr).s6_addr8[15])
70 
71 #define	FLOWACCT_FLOW_HASH(f)				\
72 	(((FLOWACCT_ADDR_HASH(f->saddr)) + 		\
73 	(FLOWACCT_ADDR_HASH(f->daddr)) + 		\
74 	(f->proto) + (f->sport) + (f->dport)) 		\
75 	% FLOW_TBL_COUNT)
76 
77 /*
78  * Compute difference between a and b in nsec and store in delta.
79  * delta should be a hrtime_t. Taken from ip_mroute.c.
80  */
81 #define	FLOWACCT_DELTA(a, b, delta) { \
82 	int xxs; \
83  \
84 	delta = (a).tv_nsec - (b).tv_nsec; \
85 	if ((xxs = (a).tv_sec - (b).tv_sec) != 0) { \
86 		switch (xxs) { \
87 		case 2: \
88 		    delta += NANOSEC; \
89 		    /*FALLTHRU*/ \
90 		case 1: \
91 		    delta += NANOSEC; \
92 		    break; \
93 		default: \
94 		    delta += ((hrtime_t)NANOSEC * xxs); \
95 		} \
96 	} \
97 }
98 
99 /* Debug level */
100 int flowacct_debug = 0;
101 
102 /* Collect timed out flows to be written to the accounting file */
103 typedef struct flow_records_s {
104 	flow_usage_t *fl_use;
105 	struct flow_records_s *next;
106 }flow_records_t;
107 
108 /* Get port information from the packet. Ignore fragments. */
109 static void
110 flowacct_port_info(header_t *header, void *iph, int af, mblk_t *mp)
111 {
112 	uint16_t *up;
113 
114 	if (af == AF_INET) {
115 		ipha_t *ipha = (ipha_t *)iph;
116 		uint32_t u2, u1;
117 		uint_t iplen;
118 
119 		u2 = ntohs(ipha->ipha_fragment_offset_and_flags);
120 		u1 = u2 & (IPH_MF | IPH_OFFSET);
121 		if (u1 != 0) {
122 			return;
123 		}
124 		iplen = (ipha->ipha_version_and_hdr_length & 0xF) << 2;
125 		up = (uint16_t *)(mp->b_rptr + iplen);
126 		header->sport = (uint16_t)*up++;
127 		header->dport = (uint16_t)*up;
128 	} else {
129 		ip6_t *ip6h = (ip6_t *)iph;
130 		uint_t  length = IPV6_HDR_LEN;
131 		uint_t  ehdrlen;
132 		uint8_t *nexthdrp, *whereptr, *endptr;
133 		ip6_dest_t *desthdr;
134 		ip6_rthdr_t *rthdr;
135 		ip6_hbh_t *hbhhdr;
136 
137 		whereptr = ((uint8_t *)&ip6h[1]);
138 		endptr = mp->b_wptr;
139 		nexthdrp = &ip6h->ip6_nxt;
140 		while (whereptr < endptr) {
141 			switch (*nexthdrp) {
142 			case IPPROTO_HOPOPTS:
143 				hbhhdr = (ip6_hbh_t *)whereptr;
144 				ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
145 				if ((uchar_t *)hbhhdr +  ehdrlen > endptr)
146 					return;
147 				nexthdrp = &hbhhdr->ip6h_nxt;
148 				break;
149 			case IPPROTO_DSTOPTS:
150 				desthdr = (ip6_dest_t *)whereptr;
151 				ehdrlen = 8 * (desthdr->ip6d_len + 1);
152 				if ((uchar_t *)desthdr +  ehdrlen > endptr)
153 					return;
154 				nexthdrp = &desthdr->ip6d_nxt;
155 				break;
156 			case IPPROTO_ROUTING:
157 				rthdr = (ip6_rthdr_t *)whereptr;
158 				ehdrlen =  8 * (rthdr->ip6r_len + 1);
159 				if ((uchar_t *)rthdr +  ehdrlen > endptr)
160 					return;
161 				nexthdrp = &rthdr->ip6r_nxt;
162 				break;
163 			case IPPROTO_FRAGMENT:
164 				return;
165 			case IPPROTO_TCP:
166 			case IPPROTO_UDP:
167 			case IPPROTO_SCTP:
168 				/*
169 				 * Verify we have at least ICMP_MIN_TP_HDR_LEN
170 				 * bytes of the ULP's header to get the port
171 				 * info.
172 				 */
173 				if (((uchar_t *)ip6h + length +
174 				    ICMP_MIN_TP_HDR_LEN)  > endptr) {
175 					return;
176 				}
177 				/* Get the protocol & ports */
178 				header->proto = *nexthdrp;
179 				up = (uint16_t *)((uchar_t *)ip6h + length);
180 				header->sport = (uint16_t)*up++;
181 				header->dport = (uint16_t)*up;
182 				return;
183 			case IPPROTO_ICMPV6:
184 			case IPPROTO_ENCAP:
185 			case IPPROTO_IPV6:
186 			case IPPROTO_ESP:
187 			case IPPROTO_AH:
188 				header->proto = *nexthdrp;
189 				return;
190 			case IPPROTO_NONE:
191 			default:
192 				return;
193 			}
194 			length += ehdrlen;
195 			whereptr += ehdrlen;
196 		}
197 	}
198 }
199 
200 /*
201  * flowacct_find_ids(mp, header)
202  *
203  * attempt to discern the uid and projid of the originator of a packet by
204  * looking at the dblks making up the packet - yeuch!
205  *
206  * We do it by skipping any fragments with a credp of NULL (originated in
207  * kernel), taking the first value that isn't NULL to be the cred_t for the
208  * whole packet.
209  */
210 static void
211 flowacct_find_ids(mblk_t *mp, header_t *header)
212 {
213 	cred_t *cr;
214 
215 	while (DB_CRED(mp) == NULL && mp->b_cont != NULL)
216 		mp = mp->b_cont;
217 
218 	if ((cr = DB_CRED(mp)) != NULL) {
219 		header->uid = crgetuid(cr);
220 		header->projid = crgetprojid(cr);
221 	} else {
222 		header->uid = -1;
223 		header->projid = -1;
224 	}
225 }
226 
227 /*
228  * Extract header information in a header_t structure so that we don't have
229  * have to parse the packet everytime.
230  */
231 static int
232 flowacct_extract_header(mblk_t *mp, header_t *header)
233 {
234 	ipha_t *ipha;
235 	ip6_t *ip6h;
236 #define	rptr	((uchar_t *)ipha)
237 
238 	/* 0 means no port extracted. */
239 	header->sport = 0;
240 	header->dport = 0;
241 	flowacct_find_ids(mp, header);
242 
243 	V6_SET_ZERO(header->saddr);
244 	V6_SET_ZERO(header->daddr);
245 
246 	ipha = (ipha_t *)mp->b_rptr;
247 	header->isv4 = IPH_HDR_VERSION(ipha) == IPV4_VERSION;
248 	if (header->isv4) {
249 		ipha = (ipha_t *)mp->b_rptr;
250 		V4_PART_OF_V6(header->saddr) = (int32_t)ipha->ipha_src;
251 		V4_PART_OF_V6(header->daddr) = (int32_t)ipha->ipha_dst;
252 		header->dsfield = ipha->ipha_type_of_service;
253 		header->proto = ipha->ipha_protocol;
254 		header->pktlen = ntohs(ipha->ipha_length);
255 		if ((header->proto == IPPROTO_TCP) ||
256 		    (header->proto == IPPROTO_UDP) ||
257 		    (header->proto == IPPROTO_SCTP)) {
258 			flowacct_port_info(header, ipha, AF_INET, mp);
259 		}
260 	} else {
261 		/*
262 		 * Need to pullup everything.
263 		 */
264 		if (mp->b_cont != NULL) {
265 			if (!pullupmsg(mp, -1)) {
266 				flowacct0dbg(("flowacct_extract_header: "\
267 				    "pullup error"));
268 				return (-1);
269 			}
270 		}
271 		ip6h = (ip6_t *)mp->b_rptr;
272 		bcopy(ip6h->ip6_src.s6_addr32, header->saddr.s6_addr32,
273 		    sizeof (ip6h->ip6_src.s6_addr32));
274 		bcopy(ip6h->ip6_dst.s6_addr32, header->daddr.s6_addr32,
275 		    sizeof (ip6h->ip6_dst.s6_addr32));
276 		header->dsfield = __IPV6_TCLASS_FROM_FLOW(ip6h->ip6_vcf);
277 		header->proto = ip6h->ip6_nxt;
278 		header->pktlen = ntohs(ip6h->ip6_plen) +
279 		    ip_hdr_length_v6(mp, ip6h);
280 		flowacct_port_info(header, ip6h, AF_INET6, mp);
281 
282 	}
283 #undef	rptr
284 	return (0);
285 }
286 
287 /* Check if the flow (identified by the 5-tuple) exists in the hash table */
288 static flow_t *
289 flowacct_flow_present(header_t *header, int index,
290     flowacct_data_t *flowacct_data)
291 {
292 	list_hdr_t *hdr = flowacct_data->flows_tbl[index].head;
293 	flow_t *flow;
294 
295 	while (hdr != NULL) {
296 		flow = (flow_t *)hdr->objp;
297 		if ((flow != NULL) &&
298 		    (IN6_ARE_ADDR_EQUAL(&flow->saddr, &header->saddr)) &&
299 		    (IN6_ARE_ADDR_EQUAL(&flow->daddr, &header->daddr)) &&
300 		    (flow->proto == header->proto) &&
301 		    (flow->sport == header->sport) &&
302 		    (flow->dport == header->dport)) {
303 			return (flow);
304 		}
305 		hdr = hdr->next;
306 	}
307 	return ((flow_t *)NULL);
308 }
309 
310 /*
311  * Add an object to the list at insert_point. This could be a flow item or
312  * a flow itself.
313  */
314 static list_hdr_t *
315 flowacct_add_obj(list_head_t *tophdr, list_hdr_t *insert_point, void *obj)
316 {
317 	list_hdr_t *new_hdr;
318 
319 	if (tophdr == NULL) {
320 		return ((list_hdr_t *)NULL);
321 	}
322 
323 	new_hdr = (list_hdr_t *)kmem_zalloc(FLOWACCT_HDR_SZ, KM_NOSLEEP);
324 	if (new_hdr == NULL) {
325 		flowacct0dbg(("flowacct_add_obj: error allocating mem"));
326 		return ((list_hdr_t *)NULL);
327 	}
328 	gethrestime(&new_hdr->last_seen);
329 	new_hdr->objp = obj;
330 	tophdr->nbr_items++;
331 
332 	if (insert_point == NULL) {
333 		if (tophdr->head == NULL) {
334 			tophdr->head = new_hdr;
335 			tophdr->tail = new_hdr;
336 			return (new_hdr);
337 		}
338 
339 		new_hdr->next = tophdr->head;
340 		tophdr->head->prev = new_hdr;
341 		tophdr->head = new_hdr;
342 		return (new_hdr);
343 	}
344 
345 	if (insert_point == tophdr->tail) {
346 		tophdr->tail->next = new_hdr;
347 		new_hdr->prev = tophdr->tail;
348 		tophdr->tail = new_hdr;
349 		return (new_hdr);
350 	}
351 
352 	new_hdr->next = insert_point->next;
353 	new_hdr->prev = insert_point;
354 	insert_point->next->prev = new_hdr;
355 	insert_point->next = new_hdr;
356 	return (new_hdr);
357 }
358 
359 /* Delete an obj from the list. This could be a flow item or the flow itself */
360 static void
361 flowacct_del_obj(list_head_t *tophdr, list_hdr_t *hdr, uint_t mode)
362 {
363 	size_t	length;
364 	uint_t	type;
365 
366 	if ((tophdr == NULL) || (hdr == NULL)) {
367 		return;
368 	}
369 
370 	type = ((flow_t *)hdr->objp)->type;
371 
372 	tophdr->nbr_items--;
373 
374 	if (hdr->next != NULL) {
375 		hdr->next->prev = hdr->prev;
376 	}
377 	if (hdr->prev != NULL) {
378 		hdr->prev->next = hdr->next;
379 	}
380 	if (tophdr->head == hdr) {
381 		tophdr->head = hdr->next;
382 	}
383 	if (tophdr->tail == hdr) {
384 		tophdr->tail = hdr->prev;
385 	}
386 
387 	if (mode == FLOWACCT_DEL_OBJ) {
388 		switch (type) {
389 		case FLOWACCT_FLOW:
390 			length = FLOWACCT_FLOW_SZ;
391 			break;
392 		case FLOWACCT_ITEM:
393 			length = FLOWACCT_ITEM_SZ;
394 			break;
395 		}
396 		kmem_free(hdr->objp, length);
397 	}
398 
399 	kmem_free((void *)hdr, FLOWACCT_HDR_SZ);
400 }
401 
402 /*
403  * Checks if the given item (identified by dsfield, project id and uid)
404  * is already present for the flow.
405  */
406 static flow_item_t *
407 flowacct_item_present(flow_t *flow, uint8_t dsfield, pid_t proj_id, uint_t uid)
408 {
409 	list_hdr_t	*itemhdr;
410 	flow_item_t	*item;
411 
412 	itemhdr = flow->items.head;
413 
414 	while (itemhdr != NULL) {
415 		item = (flow_item_t *)itemhdr->objp;
416 
417 		if ((item->dsfield != dsfield) || (item->projid != proj_id) ||
418 		    (item->uid != uid)) {
419 			itemhdr = itemhdr->next;
420 			continue;
421 		}
422 		return (item);
423 	}
424 
425 	return ((flow_item_t *)NULL);
426 }
427 
428 /*
429  * Add the flow to the table, if not already present. If the flow is
430  * present in the table, add the item. Also, update the flow stats.
431  * Additionally, re-adjust the timout list as well.
432  */
433 static int
434 flowacct_update_flows_tbl(header_t *header, flowacct_data_t *flowacct_data)
435 {
436 	int index;
437 	list_head_t *fhead;
438 	list_head_t *thead;
439 	list_head_t *ihead;
440 	boolean_t added_flow = B_FALSE;
441 	timespec_t  now;
442 	flow_item_t *item;
443 	flow_t *flow;
444 
445 	index = FLOWACCT_FLOW_HASH(header);
446 	fhead = &flowacct_data->flows_tbl[index];
447 
448 	/* The timeout list */
449 	thead = &flowacct_data->flows_tbl[FLOW_TBL_COUNT];
450 
451 	mutex_enter(&fhead->lock);
452 	flow = flowacct_flow_present(header, index, flowacct_data);
453 	if (flow == NULL) {
454 		flow = (flow_t *)kmem_zalloc(FLOWACCT_FLOW_SZ, KM_NOSLEEP);
455 		if (flow == NULL) {
456 			flowacct0dbg(("flowacct_update_flows_tbl: mem alloc "\
457 			    "error"));
458 			mutex_exit(&fhead->lock);
459 			return (-1);
460 		}
461 		flow->hdr = flowacct_add_obj(fhead, fhead->tail, (void *)flow);
462 		if (flow->hdr == NULL) {
463 			flowacct0dbg(("flowacct_update_flows_tbl: mem alloc "\
464 			    "error"));
465 			kmem_free(flow, FLOWACCT_FLOW_SZ);
466 			mutex_exit(&fhead->lock);
467 			return (-1);
468 		}
469 
470 		flow->type = FLOWACCT_FLOW;
471 		flow->isv4 = header->isv4;
472 		bcopy(header->saddr.s6_addr32, flow->saddr.s6_addr32,
473 		    sizeof (header->saddr.s6_addr32));
474 		bcopy(header->daddr.s6_addr32, flow->daddr.s6_addr32,
475 		    sizeof (header->daddr.s6_addr32));
476 		flow->proto = header->proto;
477 		flow->sport = header->sport;
478 		flow->dport = header->dport;
479 		flow->back_ptr = fhead;
480 		added_flow = B_TRUE;
481 	}
482 
483 	ihead = &flow->items;
484 	item = flowacct_item_present(flow, header->dsfield, header->projid,
485 	    header->uid);
486 	if (item == NULL) {
487 		boolean_t just_once = B_TRUE;
488 		/*
489 		 * For all practical purposes, we limit the no. of entries in
490 		 * the flow table - i.e. the max_limt that a user specifies is
491 		 * the maximum no. of flow items in the table.
492 		 */
493 	try_again:
494 		atomic_add_32(&flowacct_data->nflows, 1);
495 		if (flowacct_data->nflows > flowacct_data->max_limit) {
496 			atomic_add_32(&flowacct_data->nflows, -1);
497 
498 			/* Try timing out once */
499 			if (just_once) {
500 				/*
501 				 * Need to release the lock, as this entry
502 				 * could contain a flow that can be timed
503 				 * out.
504 				 */
505 				mutex_exit(&fhead->lock);
506 				flowacct_timer(FLOWACCT_JUST_ONE,
507 				    flowacct_data);
508 				mutex_enter(&fhead->lock);
509 				/* Lets check again */
510 				just_once = B_FALSE;
511 				goto try_again;
512 			} else {
513 				mutex_exit(&fhead->lock);
514 				flowacct0dbg(("flowacct_update_flows_tbl: "\
515 				    "maximum active flows exceeded\n"));
516 				if (added_flow) {
517 					flowacct_del_obj(fhead, flow->hdr,
518 					    FLOWACCT_DEL_OBJ);
519 				}
520 				return (-1);
521 			}
522 		}
523 		item = (flow_item_t *)kmem_zalloc(FLOWACCT_ITEM_SZ, KM_NOSLEEP);
524 		if (item == NULL) {
525 			flowacct0dbg(("flowacct_update_flows_tbl: mem alloc "\
526 			    "error"));
527 			/* Need to remove the flow, if one was added */
528 			if (added_flow) {
529 				flowacct_del_obj(fhead, flow->hdr,
530 				    FLOWACCT_DEL_OBJ);
531 			}
532 			atomic_add_32(&flowacct_data->nflows, -1);
533 			mutex_exit(&fhead->lock);
534 			return (-1);
535 		}
536 		item->hdr = flowacct_add_obj(ihead, ihead->tail, (void *)item);
537 		if (item->hdr == NULL) {
538 			flowacct0dbg(("flowacct_update_flows_tbl: mem alloc "\
539 			    "error\n"));
540 			kmem_free(item, FLOWACCT_ITEM_SZ);
541 			/* Need to remove the flow, if one was added */
542 			if (added_flow) {
543 				flowacct_del_obj(fhead, flow->hdr,
544 				    FLOWACCT_DEL_OBJ);
545 			}
546 			atomic_add_32(&flowacct_data->nflows, -1);
547 			mutex_exit(&fhead->lock);
548 			return (-1);
549 		}
550 		/* If a flow was added, add it too */
551 		if (added_flow) {
552 			atomic_add_64(&flowacct_data->usedmem,
553 			    FLOWACCT_FLOW_RECORD_SZ);
554 		}
555 		atomic_add_64(&flowacct_data->usedmem, FLOWACCT_ITEM_RECORD_SZ);
556 
557 		item->type = FLOWACCT_ITEM;
558 		item->dsfield = header->dsfield;
559 		item->projid = header->projid;
560 		item->uid = header->uid;
561 		item->npackets = 1;
562 		item->nbytes = header->pktlen;
563 		item->creation_time = item->hdr->last_seen;
564 	} else {
565 		item->npackets++;
566 		item->nbytes += header->pktlen;
567 	}
568 	gethrestime(&now);
569 	flow->hdr->last_seen = item->hdr->last_seen = now;
570 	mutex_exit(&fhead->lock);
571 
572 	/* Re-adjust the timeout list */
573 	mutex_enter(&thead->lock);
574 	/* If the flow was added, append it to the tail of the timeout list */
575 	if (added_flow) {
576 		if (thead->head == NULL) {
577 			thead->head = flow->hdr;
578 			thead->tail = flow->hdr;
579 		} else {
580 			thead->tail->timeout_next = flow->hdr;
581 			flow->hdr->timeout_prev = thead->tail;
582 			thead->tail = flow->hdr;
583 		}
584 	/*
585 	 * Else, move this flow to the tail of the timeout list, if it is not
586 	 * already.
587 	 */
588 	} else if (flow->hdr != thead->tail) {
589 		if (flow->hdr == thead->head) {
590 			thead->head->timeout_next->timeout_prev = NULL;
591 			thead->head = thead->head->timeout_next;
592 			flow->hdr->timeout_next = NULL;
593 			thead->tail->timeout_next = flow->hdr;
594 			flow->hdr->timeout_prev = thead->tail;
595 			thead->tail = flow->hdr;
596 		} else {
597 			flow->hdr->timeout_prev->timeout_next =
598 			    flow->hdr->timeout_next;
599 			flow->hdr->timeout_next->timeout_prev =
600 			    flow->hdr->timeout_prev;
601 			flow->hdr->timeout_next = NULL;
602 			thead->tail->timeout_next = flow->hdr;
603 			flow->hdr->timeout_prev = thead->tail;
604 			thead->tail = flow->hdr;
605 		}
606 	}
607 	mutex_exit(&thead->lock);
608 	atomic_add_64(&flowacct_data->tbytes, header->pktlen);
609 
610 	return (0);
611 }
612 
613 /* Timer for timing out flows/items from the flow table */
614 void
615 flowacct_timeout_flows(void *args)
616 {
617 	flowacct_data_t *flowacct_data = (flowacct_data_t *)args;
618 	flowacct_timer(FLOWACCT_FLOW_TIMER, flowacct_data);
619 	flowacct_data->flow_tid = timeout(flowacct_timeout_flows, flowacct_data,
620 	    drv_usectohz(flowacct_data->timer));
621 }
622 
623 
624 /* Delete the item from the flow in the flow table */
625 static void
626 flowacct_timeout_item(flow_t **flow, list_hdr_t **item_hdr)
627 {
628 	list_hdr_t *next_it_hdr;
629 
630 	next_it_hdr = (*item_hdr)->next;
631 	flowacct_del_obj(&(*flow)->items, *item_hdr, FLOWACCT_DEL_OBJ);
632 	*item_hdr = next_it_hdr;
633 }
634 
635 /* Create a flow record for this timed out item */
636 static flow_records_t *
637 flowacct_create_record(flow_t *flow, list_hdr_t *ithdr)
638 {
639 	int count;
640 	flow_item_t *item = (flow_item_t *)ithdr->objp;
641 	flow_records_t *tmp_frec = NULL;
642 
643 	/* Record to be written into the accounting file */
644 	tmp_frec = kmem_zalloc(sizeof (flow_records_t), KM_NOSLEEP);
645 	if (tmp_frec == NULL) {
646 		flowacct0dbg(("flowacct_create_record: mem alloc error.\n"));
647 		return (NULL);
648 	}
649 	tmp_frec->fl_use = kmem_zalloc(sizeof (flow_usage_t), KM_NOSLEEP);
650 	if (tmp_frec->fl_use == NULL) {
651 		flowacct0dbg(("flowacct_create_record: mem alloc error\n"));
652 		kmem_free(tmp_frec, sizeof (flow_records_t));
653 		return (NULL);
654 	}
655 
656 	/* Copy the IP address */
657 	for (count = 0; count < 4; count++) {
658 		tmp_frec->fl_use->fu_saddr[count] =
659 		    htonl(flow->saddr.s6_addr32[count]);
660 		tmp_frec->fl_use->fu_daddr[count] =
661 		    htonl(flow->daddr.s6_addr32[count]);
662 	}
663 
664 	/*
665 	 * Ports, protocol, version, dsfield, project id, uid, nbytes, npackets
666 	 * creation time and last seen.
667 	 */
668 	tmp_frec->fl_use->fu_sport = htons(flow->sport);
669 	tmp_frec->fl_use->fu_dport = htons(flow->dport);
670 	tmp_frec->fl_use->fu_protocol = flow->proto;
671 	tmp_frec->fl_use->fu_isv4 = flow->isv4;
672 	tmp_frec->fl_use->fu_dsfield = item->dsfield;
673 	tmp_frec->fl_use->fu_projid = item->projid;
674 	tmp_frec->fl_use->fu_userid = item->uid;
675 	tmp_frec->fl_use->fu_nbytes = item->nbytes;
676 	tmp_frec->fl_use->fu_npackets = item->npackets;
677 	tmp_frec->fl_use->fu_lseen =
678 	    (uint64_t)(ulong_t)ithdr->last_seen.tv_sec;
679 	tmp_frec->fl_use->fu_ctime =
680 	    (uint64_t)(ulong_t)item->creation_time.tv_sec;
681 
682 	return (tmp_frec);
683 }
684 
685 /*
686  * Scan thru the timeout list and write the records to the accounting file, if
687  * possible. Basically step thru the timeout list maintained in the last
688  * hash bucket, FLOW_COUNT_TBL + 1, and timeout flows. This could be called
689  * from the timer, FLOWACCT_TIMER - delete only timed out flows or when this
690  * instance is deleted, FLOWACCT_PURGE_FLOW - delete all the flows from the
691  * table or as FLOWACCT_JUST_ONE - delete the first timed out flow. Since the
692  * flows are cronologically arranged in the timeout list,  when called as
693  * FLOWACCT_TIMER and FLOWACCT_JUST_ONE, we can stop when we come across
694  * the first flow that has not timed out (which means none of the following
695  * flows would have timed out).
696  */
697 void
698 flowacct_timer(int type, flowacct_data_t *flowacct_data)
699 {
700 	hrtime_t diff;
701 	timespec_t now;
702 	list_head_t *head, *thead;
703 	flow_t *flow;
704 	flow_item_t *item;
705 	list_hdr_t *fl_hdr, *next_fl_hdr;
706 	list_hdr_t *ithdr = (list_hdr_t *)NULL;
707 	flow_records_t *frec = NULL, *tmp_frec, *tail;
708 	uint64_t flow_size;
709 	uint64_t item_size;
710 
711 	ASSERT(flowacct_data != NULL);
712 
713 	/* 2s-complement for subtraction */
714 	flow_size = ~FLOWACCT_FLOW_RECORD_SZ + 1;
715 	item_size = ~FLOWACCT_ITEM_RECORD_SZ + 1;
716 
717 	/* Get the current time */
718 	gethrestime(&now);
719 
720 	/*
721 	 * For each flow in the table, scan thru all the items and delete
722 	 * those that have exceeded the timeout. If all the items in a
723 	 * flow have timed out, delete the flow entry as well. Finally,
724 	 * write all the delted items to the accounting file.
725 	 */
726 	thead = &flowacct_data->flows_tbl[FLOW_TBL_COUNT];
727 
728 	mutex_enter(&thead->lock);
729 	fl_hdr = thead->head;
730 	while (fl_hdr != NULL) {
731 		uint32_t items_deleted = 0;
732 		next_fl_hdr = fl_hdr->timeout_next;
733 		flow = (flow_t *)fl_hdr->objp;
734 		head = flow->back_ptr;
735 		mutex_enter(&head->lock);
736 
737 		/*LINTED*/
738 		FLOWACCT_DELTA(now, fl_hdr->last_seen, diff);
739 
740 		/*
741 		 * If type is FLOW_TIMER, then check if the item has timed out.
742 		 * If type is FLOW_PURGE delete the entry anyways.
743 		 */
744 		if ((type != FLOWACCT_PURGE_FLOW) &&
745 		    (diff < flowacct_data->timeout)) {
746 			mutex_exit(&head->lock);
747 			mutex_exit(&thead->lock);
748 			goto write_records;
749 		}
750 
751 		ithdr = flow->items.head;
752 		while (ithdr != NULL) {
753 			item = (flow_item_t *)ithdr->objp;
754 			/*
755 			 * Fill in the flow record to be
756 			 * written to the accounting file.
757 			 */
758 			tmp_frec = flowacct_create_record(flow, ithdr);
759 			/*
760 			 * If we don't have memory for records,
761 			 * we will come back in case this is
762 			 * called as FLOW_TIMER, else we will
763 			 * go ahead and delete the item from
764 			 * the table (when asked to PURGE the
765 			 * table), so there could be some
766 			 * entries not written to the file
767 			 * when this action instance is
768 			 * deleted.
769 			 */
770 			if (tmp_frec != NULL) {
771 				tmp_frec->fl_use->fu_aname =
772 				    flowacct_data->act_name;
773 				if (frec == NULL) {
774 					frec = tmp_frec;
775 					tail = frec;
776 				} else {
777 					tail->next = tmp_frec;
778 					tail = tmp_frec;
779 				}
780 			} else if (type != FLOWACCT_PURGE_FLOW) {
781 				mutex_exit(&head->lock);
782 				mutex_exit(&thead->lock);
783 				atomic_add_32(&flowacct_data->nflows,
784 				    (~items_deleted + 1));
785 				goto write_records;
786 			}
787 
788 			/* Update stats */
789 			atomic_add_64(&flowacct_data->tbytes, (~item->nbytes +
790 			    1));
791 
792 			/* Delete the item */
793 			flowacct_timeout_item(&flow, &ithdr);
794 			items_deleted++;
795 			atomic_add_64(&flowacct_data->usedmem, item_size);
796 		}
797 		ASSERT(flow->items.nbr_items == 0);
798 		atomic_add_32(&flowacct_data->nflows, (~items_deleted + 1));
799 
800 		if (fl_hdr == thead->tail) {
801 			thead->head = thead->tail = NULL;
802 		} else {
803 			thead->head = fl_hdr->timeout_next;
804 			thead->head->timeout_prev = NULL;
805 		}
806 		flowacct_del_obj(head, fl_hdr, FLOWACCT_DEL_OBJ);
807 		atomic_add_64(&flowacct_data->usedmem, flow_size);
808 		mutex_exit(&head->lock);
809 		if (type == FLOWACCT_JUST_ONE) {
810 			mutex_exit(&thead->lock);
811 			goto write_records;
812 		}
813 		fl_hdr = next_fl_hdr;
814 	}
815 	mutex_exit(&thead->lock);
816 write_records:
817 	/* Write all the timed out flows to the accounting file */
818 	while (frec != NULL) {
819 		tmp_frec = frec->next;
820 		exacct_commit_flow(frec->fl_use);
821 		kmem_free(frec->fl_use, sizeof (flow_usage_t));
822 		kmem_free(frec, sizeof (flow_records_t));
823 		frec = tmp_frec;
824 	}
825 }
826 
827 /*
828  * Get the IP header contents from the packet, update the flow table with
829  * this item and return.
830  */
831 int
832 flowacct_process(mblk_t **mpp, flowacct_data_t *flowacct_data)
833 {
834 	header_t *header;
835 	mblk_t *mp = *mpp;
836 
837 	ASSERT(mp != NULL);
838 
839 	/* If we don't find an M_DATA, return error */
840 	if (mp->b_datap->db_type != M_DATA) {
841 		if ((mp->b_cont != NULL) &&
842 		    (mp->b_cont->b_datap->db_type == M_DATA)) {
843 			mp = mp->b_cont;
844 		} else {
845 			flowacct0dbg(("flowacct_process: no data\n"));
846 			atomic_add_64(&flowacct_data->epackets, 1);
847 			return (EINVAL);
848 		}
849 	}
850 
851 	header = kmem_zalloc(FLOWACCT_HEADER_SZ, KM_NOSLEEP);
852 	if (header == NULL) {
853 		flowacct0dbg(("flowacct_process: error allocing mem"));
854 		atomic_add_64(&flowacct_data->epackets, 1);
855 		return (ENOMEM);
856 	}
857 
858 	/* Get all the required information into header. */
859 	if (flowacct_extract_header(mp, header) != 0) {
860 		kmem_free(header, FLOWACCT_HEADER_SZ);
861 		atomic_add_64(&flowacct_data->epackets, 1);
862 		return (EINVAL);
863 	}
864 
865 	/* Updated the flow table with this entry */
866 	if (flowacct_update_flows_tbl(header, flowacct_data) != 0) {
867 		kmem_free(header, FLOWACCT_HEADER_SZ);
868 		atomic_add_64(&flowacct_data->epackets, 1);
869 		return (ENOMEM);
870 	}
871 
872 	/* Update global stats */
873 	atomic_add_64(&flowacct_data->npackets, 1);
874 	atomic_add_64(&flowacct_data->nbytes, header->pktlen);
875 
876 	kmem_free(header, FLOWACCT_HEADER_SZ);
877 	if (flowacct_data->flow_tid == 0) {
878 		flowacct_data->flow_tid = timeout(flowacct_timeout_flows,
879 		    flowacct_data, drv_usectohz(flowacct_data->timer));
880 	}
881 	return (0);
882 }
883