From: wessels <> Date: Sat, 4 Apr 1998 12:17:38 +0000 (+0000) Subject: Cleaned up ICP sendto(). Now there is a single function X-Git-Tag: SQUID_3_0_PRE1~3629 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=17b6e7840a819b6dc91c794b1dc505c57d13e0e3;p=thirdparty%2Fsquid.git Cleaned up ICP sendto(). Now there is a single function for calling sendto() and messages may still be queued. Previously we forgot to call icpLogIcp() from the other place so now all logging and stats are in a single function which gets called after a successful send. --- diff --git a/src/client_side.cc b/src/client_side.cc index 046fba3072..f3d6fe40c5 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -1,6 +1,6 @@ /* - * $Id: client_side.cc,v 1.254 1998/04/04 00:23:13 wessels Exp $ + * $Id: client_side.cc,v 1.255 1998/04/04 05:17:38 wessels Exp $ * * DEBUG: section 33 Client-side Routines * AUTHOR: Duane Wessels @@ -525,27 +525,27 @@ clientUpdateCounters(clientHttpRequest * http) if (0 != i->stop.tv_sec && 0 != i->start.tv_sec) statHistCount(&Counter.icp.query_svc_time, tvSubUsec(i->start, i->stop)); #if CACHE_DIGEST - assert(http->request->hier.used_icp + http->request->hier.used_cd < 2); - if (http->request->hier.used_icp) { - statHistCount(&Counter.icp.client_svc_time, svc_time); - Counter.icp.times_used++; - } - if (http->request->hier.used_cd) { - statHistCount(&Counter.cd.client_svc_time, svc_time); - Counter.cd.times_used++; - } - t = httpHeaderGetLastStr(, HDR_X_CACHE); - if (NULL != t && 0 == strncmp(t, "HIT", 3)) { - if (http->request->hier.cd_hit) - Counter.cd.true_hits++; - else - Counter.cd.false_miss++; - } else { - if (http->request->hier.cd_hit) - Counter.cd.false_hit++; - else - Counter.cd.true_miss++; - } + assert(http->request->hier.used_icp + http->request->hier.used_cd < 2); + if (http->request->hier.used_icp) { + statHistCount(&Counter.icp.client_svc_time, svc_time); + Counter.icp.times_used++; + } + if (http->request->hier.used_cd) { + statHistCount(&Counter.cd.client_svc_time, svc_time); + Counter.cd.times_used++; + } + t = httpHeaderGetLastStr(, HDR_X_CACHE); + if (NULL != t && 0 == strncmp(t, "HIT", 3)) { + if (http->request->hier.cd_hit) + Counter.cd.true_hits++; + else + Counter.cd.false_miss++; + } else { + if (http->request->hier.cd_hit) + Counter.cd.false_hit++; + else + Counter.cd.true_miss++; + } #endif } @@ -1288,8 +1288,7 @@ clientProcessRequest2(clientHttpRequest * http) * so we can use std processing routines for IMS and such */ if (EBIT_TEST(r->flags, REQ_IMS) && e->lastmod <= r->ims) return LOG_TCP_IMS_HIT; - else - if (e->mem_status == IN_MEMORY) + else if (e->mem_status == IN_MEMORY) return LOG_TCP_MEM_HIT; else return LOG_TCP_HIT; @@ -1980,22 +1979,6 @@ httpAccept(int sock, void *notused) commSetDefer(fd, clientReadDefer, connState); } -void -AppendUdp(icpUdpData * item) -{ - item->next = NULL; - if (UdpQueueHead == NULL) { - UdpQueueHead = item; - UdpQueueTail = item; - } else if (UdpQueueTail == UdpQueueHead) { - UdpQueueTail = item; - UdpQueueHead->next = item; - } else { - UdpQueueTail->next = item; - UdpQueueTail = item; - } -} - /* return 1 if the request should be aborted */ static int CheckQuickAbort2(const clientHttpRequest * http) diff --git a/src/comm.cc b/src/comm.cc index 5cdd1a7f18..442901fa38 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -1,7 +1,7 @@ /* - * $Id: comm.cc,v 1.242 1998/03/31 05:37:38 wessels Exp $ + * $Id: comm.cc,v 1.243 1998/04/04 05:17:40 wessels Exp $ * * DEBUG: section 5 Socket Functions * AUTHOR: Harvest Derived @@ -662,7 +662,7 @@ int comm_udp_sendto(int fd, const struct sockaddr_in *to_addr, int addr_len, - const char *buf, + const void *buf, int len) { int x; diff --git a/src/globals.h b/src/globals.h index 95e5983dd6..e0d5a87bbf 100644 --- a/src/globals.h +++ b/src/globals.h @@ -1,6 +1,6 @@ /* - * $Id: globals.h,v 1.49 1998/04/02 17:11:23 rousskov Exp $ + * $Id: globals.h,v 1.50 1998/04/04 05:17:42 wessels Exp $ */ extern FILE *debug_log; /* NULL */ @@ -85,8 +85,6 @@ extern int reconfiguring; /* 0 */ extern int store_rebuilding; /* 1 */ extern int store_swap_size; /* 0 */ extern unsigned long store_mem_size; /* 0 */ -extern icpUdpData *UdpQueueHead; /* NULL */ -extern icpUdpData *UdpQueueTail; /* NULL */ extern time_t hit_only_mode_until; /* 0 */ extern StatCounters Counter; extern char *err_type_str[]; @@ -99,4 +97,4 @@ extern dlink_list store_list; extern const String StringNull; /* { 0, 0, NULL } */ extern int hot_obj_count; /* 0 */ extern int _db_level; -extern CacheDigest *store_digest; /* NULL */ +extern CacheDigest *store_digest; /* NULL */ diff --git a/src/icp_v2.cc b/src/icp_v2.cc index 6a0f72300f..866cef4668 100644 --- a/src/icp_v2.cc +++ b/src/icp_v2.cc @@ -1,67 +1,49 @@ #include "squid.h" -static void icpLogIcp(icpUdpData *); +static void icpLogIcp(struct in_addr, log_type, int, const char *, int); static void icpHandleIcpV2(int, struct sockaddr_in, char *, int); +static PF icpUdpSendQueue; + +static icpUdpData *UdpQueueHead = NULL; +static icpUdpData *UdpQueueTail = NULL; static void -icpLogIcp(icpUdpData * queue) +icpLogIcp(struct in_addr caddr, log_type logcode, int len, const char *url, int delay) { - icp_common_t *header = (icp_common_t *) (void *) queue->msg; - char *url = (char *) header + sizeof(icp_common_t); AccessLogEntry al; - clientdbUpdate(queue->address.sin_addr, queue->logcode, PROTO_ICP, queue->len); + clientdbUpdate(caddr, logcode, PROTO_ICP, len); + Counter.icp.pkts_sent++; + if (logcode == LOG_UDP_HIT) + Counter.icp.hits_sent++; + kb_incr(&Counter.icp.kbytes_sent, (size_t) len); + statHistCount(&Counter.icp.reply_svc_time, delay); if (!Config.onoff.log_udp) return; - memset(&al, '\0', sizeof(AccessLogEntry)); + memset(&al, '\0', sizeof(al)); al.icp.opcode = ICP_QUERY; al.url = url; - al.cache.caddr = queue->address.sin_addr; - al.cache.size = queue->len; - al.cache.code = queue->logcode; - al.cache.msec = tvSubMsec(queue->start, current_time); + al.cache.caddr = caddr; + al.cache.size = len; + al.cache.code = logcode; + al.cache.msec = delay; accessLogLog(&al); } -void -icpUdpReply(int fd, void *data) +static void +icpUdpSendQueue(int fd, void *unused) { - icpUdpData *queue = data; + icpUdpData *q; int x; - /* Disable handler, in case of errors. */ - commSetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0); - while ((queue = UdpQueueHead) != NULL) { - debug(12, 5) ("icpUdpReply: FD %d sending %d bytes to %s port %d\n", - fd, - queue->len, - inet_ntoa(queue->address.sin_addr), - ntohs(queue->address.sin_port)); - x = comm_udp_sendto(fd, - &queue->address, - sizeof(struct sockaddr_in), - queue->msg, - queue->len); - if (x < 0) { - if (ignoreErrno(errno)) - break; /* don't de-queue */ - } else { - Counter.icp.pkts_sent++; - if (queue->logcode == LOG_UDP_HIT) - Counter.icp.hits_sent++; - kb_incr(&Counter.icp.kbytes_sent, (size_t) x); - } - UdpQueueHead = queue->next; - if (queue->logcode) { - icpLogIcp(queue); - statHistCount(&Counter.icp.reply_svc_time, - tvSubUsec(queue->start, current_time)); - } - safe_free(queue->msg); - safe_free(queue); - } - /* Reinstate handler if needed */ - if (UdpQueueHead) { - commSetSelect(fd, COMM_SELECT_WRITE, icpUdpReply, UdpQueueHead, 0); + int delay; + while ((q = UdpQueueHead) != NULL) { + delay = tvSubUsec(q->queue_time, current_time); + /* increment delay to prevent looping */ + x = icpUdpSend(fd, &q->address, q->msg, q->logcode, ++delay); + UdpQueueHead = q->next; + safe_free(q); + if (x < 0) + break; } } @@ -96,47 +78,53 @@ icpCreateMessage( return buf; } -void +int icpUdpSend(int fd, const struct sockaddr_in *to, icp_common_t * msg, log_type logcode, - protocol_t proto) + int delay) { - icpUdpData *data = xcalloc(1, sizeof(icpUdpData)); + icpUdpData *queue; int x; - debug(12, 4) ("icpUdpSend: Queueing %s for %s\n", - icp_opcode_str[msg->opcode], - inet_ntoa(to->sin_addr)); - data->address = *to; - data->msg = msg; - data->len = (int) ntohs(msg->length); - data->start = current_time; - data->logcode = logcode; - data->proto = proto; - debug(12, 5) ("icpUdpSend: FD %d sending %d bytes to %s port %d\n", + int len; + len = (int) ntohs(msg->length); + debug(12, 5) ("icpUdpSend: FD %d sending %s, %d bytes to %s:%d\n", fd, - data->len, - inet_ntoa(data->address.sin_addr), - ntohs(data->address.sin_port)); - x = comm_udp_sendto(fd, - &data->address, - sizeof(struct sockaddr_in), - data->msg, - data->len); - if (x < 0) { - /* queue it */ - AppendUdp(data); - commSetSelect(fd, COMM_SELECT_WRITE, icpUdpReply, UdpQueueHead, 0); + icp_opcode_str[msg->opcode], + len, + inet_ntoa(to->sin_addr), + ntohs(to->sin_port)); + x = comm_udp_sendto(fd, to, sizeof(*to), msg, len); + if (0 == x) { + /* successfully written */ + icpLogIcp(to->sin_addr, logcode, len, (char *) (msg + 1), delay); + safe_free(msg); + } else if (0 == delay) { + /* send failed, but queue it */ + queue = xcalloc(1, sizeof(icpUdpData)); + queue->address = *to; + queue->msg = msg; + queue->len = (int) ntohs(msg->length); + queue->queue_time = current_time; + queue->logcode = logcode; + if (UdpQueueHead == NULL) { + UdpQueueHead = queue; + UdpQueueTail = queue; + } else if (UdpQueueTail == UdpQueueHead) { + UdpQueueTail = queue; + UdpQueueHead->next = queue; + } else { + UdpQueueTail->next = queue; + UdpQueueTail = queue; + } + commSetSelect(fd, COMM_SELECT_WRITE, icpUdpSendQueue, NULL, 0); Counter.icp.replies_queued++; } else { - Counter.icp.pkts_sent++; - if (data->logcode == LOG_UDP_HIT) - Counter.icp.hits_sent++; - kb_incr(&Counter.icp.kbytes_sent, (size_t) x); - safe_free(data->msg); - safe_free(data); + /* don't queue it */ + Counter.icp.replies_dropped++; } + return x; } int @@ -188,7 +176,7 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) url = buf + sizeof(icp_common_t) + sizeof(u_num32); if ((icp_request = urlParse(method, url)) == NULL) { reply = icpCreateMessage(ICP_ERR, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, PROTO_NONE); + icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); break; } checklist.src_addr = from.sin_addr; @@ -205,7 +193,7 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) clientdbUpdate(from.sin_addr, LOG_UDP_DENIED, Config.Port.icp, 0); } else { reply = icpCreateMessage(ICP_DENIED, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_DENIED, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_DENIED, 0); } break; } @@ -222,7 +210,7 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) debug(12, 5) ("icpHandleIcpV2: OPCODE %s\n", icp_opcode_str[header.opcode]); if (icpCheckUdpHit(entry, icp_request)) { reply = icpCreateMessage(ICP_HIT, flags, url, header.reqnum, src_rtt); - icpUdpSend(fd, &from, reply, LOG_UDP_HIT, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_HIT, 0); break; } if (Config.onoff.test_reachability && rtt == 0) { @@ -232,16 +220,16 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) /* if store is rebuilding, return a UDP_HIT, but not a MISS */ if (store_rebuilding && opt_reload_hit_only) { reply = icpCreateMessage(ICP_MISS_NOFETCH, flags, url, header.reqnum, src_rtt); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, 0); } else if (hit_only_mode_until > squid_curtime) { reply = icpCreateMessage(ICP_MISS_NOFETCH, flags, url, header.reqnum, src_rtt); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, 0); } else if (Config.onoff.test_reachability && rtt == 0) { reply = icpCreateMessage(ICP_MISS_NOFETCH, flags, url, header.reqnum, src_rtt); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, 0); } else { reply = icpCreateMessage(ICP_MISS, flags, url, header.reqnum, src_rtt); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS, 0); } break; diff --git a/src/icp_v3.cc b/src/icp_v3.cc index 905262d1c9..7c3b4ab2a3 100644 --- a/src/icp_v3.cc +++ b/src/icp_v3.cc @@ -32,7 +32,7 @@ icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len) url = buf + sizeof(icp_common_t) + sizeof(u_num32); if ((icp_request = urlParse(method, url)) == NULL) { reply = icpCreateMessage(ICP_ERR, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, PROTO_NONE); + icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); break; } checklist.src_addr = from.sin_addr; @@ -49,7 +49,7 @@ icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len) clientdbUpdate(from.sin_addr, LOG_UDP_DENIED, Config.Port.icp, 0); } else { reply = icpCreateMessage(ICP_DENIED, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_DENIED, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_DENIED, 0); } break; } @@ -60,19 +60,19 @@ icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len) icp_opcode_str[header.opcode]); if (icpCheckUdpHit(entry, icp_request)) { reply = icpCreateMessage(ICP_HIT, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_HIT, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_HIT, 0); break; } /* if store is rebuilding, return a UDP_HIT, but not a MISS */ if (opt_reload_hit_only && store_rebuilding) { reply = icpCreateMessage(ICP_MISS_NOFETCH, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, 0); } else if (hit_only_mode_until > squid_curtime) { reply = icpCreateMessage(ICP_MISS_NOFETCH, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, 0); } else { reply = icpCreateMessage(ICP_MISS, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS, icp_request->protocol); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS, 0); } break; diff --git a/src/protos.h b/src/protos.h index 46034bff05..b72f2024ef 100644 --- a/src/protos.h +++ b/src/protos.h @@ -108,7 +108,7 @@ extern int comm_select(time_t); extern void commSetSelect(int, unsigned int, PF *, void *, time_t); extern void comm_add_close_handler(int fd, PF *, void *); extern void comm_remove_close_handler(int fd, PF *, void *); -extern int comm_udp_sendto(int fd, const struct sockaddr_in *, int size, const char *buf, int len); +extern int comm_udp_sendto(int, const struct sockaddr_in *, int, const void *, int); extern void comm_write(int fd, char *buf, int size, @@ -378,11 +378,7 @@ extern void *icpCreateMessage(icp_opcode opcode, const char *url, int reqnum, int pad); -extern void icpUdpSend(int fd, - const struct sockaddr_in *, - icp_common_t * msg, - log_type, - protocol_t); +extern int icpUdpSend(int, const struct sockaddr_in *, icp_common_t *, log_type, int); extern PF icpHandleUdp; extern PF httpAccept; extern DEFER httpAcceptDefer; @@ -401,8 +397,6 @@ extern void snmpTokenize(char *, char **, int); extern int snmpCompare(oid * name1, int len1, oid * name2, int len2); #endif /* SQUID_SNMP */ -extern void AppendUdp(icpUdpData *); -extern PF icpUdpReply; extern void icpHandleIcpV3(int, struct sockaddr_in, char *, int); extern int icpCheckUdpHit(StoreEntry *, request_t * request); extern void icpConnectionsOpen(void); diff --git a/src/send-announce.cc b/src/send-announce.cc index 37ac82818b..c3c2955d5c 100644 --- a/src/send-announce.cc +++ b/src/send-announce.cc @@ -1,6 +1,6 @@ /* - * $Id: send-announce.cc,v 1.48 1998/03/27 18:41:14 wessels Exp $ + * $Id: send-announce.cc,v 1.49 1998/04/04 05:17:47 wessels Exp $ * * DEBUG: section 27 Cache Announcer * AUTHOR: Duane Wessels @@ -39,6 +39,8 @@ start_announce(void *datanotused) void *junk; if (0 == Config.onoff.announce) return; + if (theOutIcpConnection < 0) + return; cbdataAdd(junk = xmalloc(1), MEM_NONE); ipcache_nbgethostbyname(Config.Announce.host, send_announce, junk); eventAdd("send_announce", start_announce, NULL, Config.Announce.period); @@ -49,13 +51,14 @@ send_announce(const ipcache_addrs * ia, void *junk) { LOCAL_ARRAY(char, tbuf, 256); LOCAL_ARRAY(char, sndbuf, BUFSIZ); - icpUdpData *qdata = NULL; + struct sockaddr_in S; char *host = Config.Announce.host; char *file = NULL; u_short port = Config.Announce.port; int l; int n; int fd; + int x; cbdataFree(junk); if (ia == NULL) { debug(27, 1) ("send_announce: Unknown host '%s'\n", host); @@ -91,15 +94,15 @@ send_announce(const ipcache_addrs * ia, void *junk) debug(50, 1) ("send_announce: %s: %s\n", file, xstrerror()); } } - qdata = xcalloc(1, sizeof(icpUdpData)); - qdata->msg = xstrdup(sndbuf); - qdata->len = strlen(sndbuf) + 1; - qdata->address.sin_family = AF_INET; - qdata->address.sin_port = htons(port); - qdata->address.sin_addr = ia->in_addrs[0]; - AppendUdp(qdata); - commSetSelect(theOutIcpConnection, - COMM_SELECT_WRITE, - icpUdpReply, - qdata, 0); + memset(&S, '\0', sizeof(S)); + S.sin_family = AF_INET; + S.sin_port = htons(port); + S.sin_addr = ia->in_addrs[0]; + assert(theOutIcpConnection > 0); + x = comm_udp_sendto(theOutIcpConnection, + &S, sizeof(S), + sndbuf, strlen(sndbuf) + 1); + if (x < 0) + debug(27, 1) ("send_announce: FD %d: %s\n", theOutIcpConnection, + xstrerror()); } diff --git a/src/structs.h b/src/structs.h index e788091ced..74b1669d08 100644 --- a/src/structs.h +++ b/src/structs.h @@ -600,7 +600,7 @@ struct _icpUdpData { struct timeval start; #endif log_type logcode; - protocol_t proto; + struct timeval queue_time; }; struct _icp_ping_data { @@ -1134,6 +1134,7 @@ struct _StatCounters { int hits_sent; int hits_recv; int replies_queued; + int replies_dropped; kb_t kbytes_sent; kb_t kbytes_recv; StatHist query_svc_time;