http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/iocore/net/Socks.cc ---------------------------------------------------------------------- diff --git a/iocore/net/Socks.cc b/iocore/net/Socks.cc index cfdd214..a0350f6 100644 --- a/iocore/net/Socks.cc +++ b/iocore/net/Socks.cc @@ -39,7 +39,7 @@ socks_conf_struct *g_socks_conf_stuff = 0; ClassAllocator<SocksEntry> socksAllocator("socksAllocator"); void -SocksEntry::init(ProxyMutex * m, SocksNetVC * vc, unsigned char socks_support, unsigned char ver) +SocksEntry::init(ProxyMutex *m, SocksNetVC *vc, unsigned char socks_support, unsigned char ver) { mutex = m; buf = new_MIOBuffer(); @@ -65,7 +65,7 @@ SocksEntry::init(ProxyMutex * m, SocksNetVC * vc, unsigned char socks_support, u assert(ats_is_ip4(&target_addr)); ats_ip_copy(&req_data.dest_ip, &target_addr); - //we dont have information about the source. set to destination's + // we dont have information about the source. set to destination's ats_ip_copy(&req_data.src_ip, &target_addr); server_params = SocksServerConfig::acquire(); @@ -88,10 +88,9 @@ SocksEntry::findServer() ink_assert(server_result.r == PARENT_UNDEFINED); server_params->findParent(&req_data, &server_result); } else { - socks_conf_struct *conf = netProcessor.socks_conf_stuff; if ((nattempts - 1) % conf->per_server_connection_attempts) - return; //attempt again + return; // attempt again server_params->markParentDown(&server_result); @@ -122,14 +121,13 @@ SocksEntry::findServer() #else if (nattempts > netProcessor.socks_conf_stuff->connection_attempts) memset(&server_addr, 0, sizeof(server_addr)); - else ats_ip_copy(&server_addr, &g_socks_conf_stuff->server_addr); + else + ats_ip_copy(&server_addr, &g_socks_conf_stuff->server_addr); #endif // SOCKS_WITH_TS char buff[INET6_ADDRSTRLEN]; - Debug("SocksParents", "findServer result: %s:%d", - ats_ip_ntop(&server_addr.sa, buff, sizeof(buff)), - ats_ip_port_host_order(&server_addr) - ); + Debug("SocksParents", "findServer result: %s:%d", ats_ip_ntop(&server_addr.sa, buff, sizeof(buff)), + ats_ip_port_host_order(&server_addr)); } void @@ -162,7 +160,7 @@ SocksEntry::free() } else { netVConnection->do_io_read(this, 0, 0); netVConnection->do_io_write(this, 0, 0); - netVConnection->action_ = action_; //assign the original continuation + netVConnection->action_ = action_; // assign the original continuation ats_ip_copy(&netVConnection->server_addr, &server_addr); Debug("Socks", "Sent success to HTTP"); NET_INCREMENT_DYN_STAT(socks_connections_successful_stat); @@ -183,12 +181,12 @@ int SocksEntry::startEvent(int event, void *data) { if (event == NET_EVENT_OPEN) { - netVConnection = (SocksNetVC *) data; + netVConnection = (SocksNetVC *)data; if (version == SOCKS5_VERSION) auth_handler = &socks5BasicAuthHandler; - SET_HANDLER((SocksEntryHandler) & SocksEntry::mainEvent); + SET_HANDLER((SocksEntryHandler)&SocksEntry::mainEvent); mainEvent(NET_EVENT_OPEN, data); } else { if (timeout) { @@ -238,38 +236,30 @@ SocksEntry::mainEvent(int event, void *data) unsigned char *p; switch (event) { - case NET_EVENT_OPEN: buf->reset(); unsigned short ts; - p = (unsigned char *) buf->start(); + p = (unsigned char *)buf->start(); ink_assert(netVConnection); if (auth_handler) { n_bytes = invokeSocksAuthHandler(auth_handler, SOCKS_AUTH_OPEN, p); } else { - - //Debug("Socks", " Got NET_EVENT_OPEN to SOCKS server\n"); + // Debug("Socks", " Got NET_EVENT_OPEN to SOCKS server\n"); p[n_bytes++] = version; p[n_bytes++] = (socks_cmd == NORMAL_SOCKS) ? SOCKS_CONNECT : socks_cmd; ts = ntohs(ats_ip_port_cast(&server_addr)); if (version == SOCKS5_VERSION) { - p[n_bytes++] = 0; //Reserved + p[n_bytes++] = 0; // Reserved if (ats_is_ip4(&server_addr)) { - p[n_bytes++] = 1; //IPv4 addr - memcpy(p + n_bytes, - &server_addr.sin.sin_addr, - 4 - ); + p[n_bytes++] = 1; // IPv4 addr + memcpy(p + n_bytes, &server_addr.sin.sin_addr, 4); n_bytes += 4; } else if (ats_is_ip6(&server_addr)) { - p[n_bytes++] = 4; //IPv6 addr - memcpy(p + n_bytes, - &server_addr.sin6.sin6_addr, - TS_IP6_SIZE - ); + p[n_bytes++] = 4; // IPv6 addr + memcpy(p + n_bytes, &server_addr.sin6.sin6_addr, TS_IP6_SIZE); n_bytes += TS_IP6_SIZE; } else { Debug("Socks", "SOCKS supports only IP addresses."); @@ -281,19 +271,15 @@ SocksEntry::mainEvent(int event, void *data) if (version == SOCKS4_VERSION) { if (ats_is_ip4(&server_addr)) { - //for socks4, ip addr is after the port - memcpy(p + n_bytes, - &server_addr.sin.sin_addr, - 4 - ); + // for socks4, ip addr is after the port + memcpy(p + n_bytes, &server_addr.sin.sin_addr, 4); n_bytes += 4; - p[n_bytes++] = 0; // NULL + p[n_bytes++] = 0; // NULL } else { Debug("Socks", "SOCKS v4 supports only IPv4 addresses."); } } - } buf->fill(n_bytes); @@ -304,7 +290,7 @@ SocksEntry::mainEvent(int event, void *data) } netVConnection->do_io_write(this, n_bytes, reader, 0); - //Debug("Socks", "Sent the request to the SOCKS server\n"); + // Debug("Socks", "Sent the request to the SOCKS server\n"); ret = EVENT_CONT; break; @@ -321,16 +307,15 @@ SocksEntry::mainEvent(int event, void *data) write_done = true; } - buf->reset(); // Use the same buffer for a read now + buf->reset(); // Use the same buffer for a read now if (auth_handler) n_bytes = invokeSocksAuthHandler(auth_handler, SOCKS_AUTH_WRITE_COMPLETE, NULL); else if (socks_cmd == NORMAL_SOCKS) - n_bytes = (version == SOCKS5_VERSION) - ? SOCKS5_REP_LEN : SOCKS4_REP_LEN; + n_bytes = (version == SOCKS5_VERSION) ? SOCKS5_REP_LEN : SOCKS4_REP_LEN; else { Debug("Socks", "Tunnelling the connection"); - //let the client handle the response + // let the client handle the response free(); break; } @@ -346,8 +331,8 @@ SocksEntry::mainEvent(int event, void *data) ret = EVENT_CONT; if (version == SOCKS5_VERSION && auth_handler == NULL) { - VIO *vio = (VIO *) data; - p = (unsigned char *) buf->start(); + VIO *vio = (VIO *)data; + p = (unsigned char *)buf->start(); if (vio->ndone >= 5) { int reply_len; @@ -365,7 +350,7 @@ SocksEntry::mainEvent(int event, void *data) break; default: reply_len = INT_MAX; - Debug("Socks", "Illegal address type(%d) in Socks server", (int) p[3]); + Debug("Socks", "Illegal address type(%d) in Socks server", (int)p[3]); } if (vio->ndone >= reply_len) { @@ -377,14 +362,14 @@ SocksEntry::mainEvent(int event, void *data) if (ret == EVENT_CONT) break; - // Fall Through + // Fall Through case VC_EVENT_READ_COMPLETE: if (timeout) { timeout->cancel(this); timeout = NULL; } - //Debug("Socks", "Successfully read the reply from the SOCKS server\n"); - p = (unsigned char *) buf->start(); + // Debug("Socks", "Successfully read the reply from the SOCKS server\n"); + p = (unsigned char *)buf->start(); if (auth_handler) { SocksAuthHandler temp = auth_handler; @@ -399,20 +384,19 @@ SocksEntry::mainEvent(int event, void *data) } } else { - bool success; if (version == SOCKS5_VERSION) { success = (p[0] == SOCKS5_VERSION && p[1] == SOCKS5_REQ_GRANTED); - Debug("Socks", "received reply of length %" PRId64" addr type %d", ((VIO *) data)->ndone, (int) p[3]); + Debug("Socks", "received reply of length %" PRId64 " addr type %d", ((VIO *)data)->ndone, (int)p[3]); } else success = (p[0] == 0 && p[1] == SOCKS4_REQ_GRANTED); - //ink_assert(*(p) == 0); - if (!success) { // SOCKS request failed - Debug("Socks", "Socks request denied %d", (int) *(p + 1)); + // ink_assert(*(p) == 0); + if (!success) { // SOCKS request failed + Debug("Socks", "Socks request denied %d", (int)*(p + 1)); lerrno = ESOCK_DENIED; } else { - Debug("Socks", "Socks request successful %d", (int) *(p + 1)); + Debug("Socks", "Socks request successful %d", (int)*(p + 1)); lerrno = 0; } free(); @@ -427,13 +411,13 @@ SocksEntry::mainEvent(int event, void *data) free(); break; } - /* else - This is server_connect_timeout. So we treat this as server being - down. - Should cancel any pending connect() action. Important on windows + /* else + This is server_connect_timeout. So we treat this as server being + down. + Should cancel any pending connect() action. Important on windows - fall through - */ + fall through + */ case VC_EVENT_ERROR: /*This is mostly ECONNREFUSED on Unix */ SET_HANDLER(&SocksEntry::startEvent); @@ -460,7 +444,7 @@ SocksEntry::mainEvent(int event, void *data) } void -loadSocksConfiguration(socks_conf_struct * socks_conf_stuff) +loadSocksConfiguration(socks_conf_struct *socks_conf_stuff) { int socks_config_fd = -1; ats_scoped_str config_pathname; @@ -468,7 +452,7 @@ loadSocksConfiguration(socks_conf_struct * socks_conf_stuff) char *tmp; #endif - socks_conf_stuff->accept_enabled = 0; //initialize it INKqa08593 + socks_conf_stuff->accept_enabled = 0; // initialize it INKqa08593 socks_conf_stuff->socks_needed = REC_ConfigReadInteger("proxy.config.socks.socks_needed"); if (!socks_conf_stuff->socks_needed) { Debug("Socks", "Socks Turned Off"); @@ -485,19 +469,18 @@ loadSocksConfiguration(socks_conf_struct * socks_conf_stuff) socks_conf_stuff->server_connect_timeout = REC_ConfigReadInteger("proxy.config.socks.server_connect_timeout"); socks_conf_stuff->socks_timeout = REC_ConfigReadInteger("proxy.config.socks.socks_timeout"); - Debug("Socks", "server connect timeout: %d socks respnonse timeout %d", - socks_conf_stuff->server_connect_timeout, socks_conf_stuff->socks_timeout); + Debug("Socks", "server connect timeout: %d socks respnonse timeout %d", socks_conf_stuff->server_connect_timeout, + socks_conf_stuff->socks_timeout); - socks_conf_stuff->per_server_connection_attempts = - REC_ConfigReadInteger("proxy.config.socks.per_server_connection_attempts"); + socks_conf_stuff->per_server_connection_attempts = REC_ConfigReadInteger("proxy.config.socks.per_server_connection_attempts"); socks_conf_stuff->connection_attempts = REC_ConfigReadInteger("proxy.config.socks.connection_attempts"); socks_conf_stuff->accept_enabled = REC_ConfigReadInteger("proxy.config.socks.accept_enabled"); socks_conf_stuff->accept_port = REC_ConfigReadInteger("proxy.config.socks.accept_port"); socks_conf_stuff->http_port = REC_ConfigReadInteger("proxy.config.socks.http_port"); Debug("SocksProxy", "Read SocksProxy info: accept_enabled = %d " - "accept_port = %d http_port = %d", socks_conf_stuff->accept_enabled, - socks_conf_stuff->accept_port, socks_conf_stuff->http_port); + "accept_port = %d http_port = %d", + socks_conf_stuff->accept_enabled, socks_conf_stuff->accept_port, socks_conf_stuff->http_port); #ifdef SOCKS_WITH_TS SocksServerConfig::startup(); @@ -511,19 +494,15 @@ loadSocksConfiguration(socks_conf_struct * socks_conf_stuff) goto error; } - socks_config_fd =::open(config_pathname, O_RDONLY); + socks_config_fd = ::open(config_pathname, O_RDONLY); if (socks_config_fd < 0) { Error("SOCKS Config: could not open config file '%s'. SOCKS Turned off", (const char *)config_pathname); goto error; } #ifdef SOCKS_WITH_TS - tmp = Load_IpMap_From_File( - &socks_conf_stuff->ip_map, - socks_config_fd, - "no_socks" - ); -// tmp = socks_conf_stuff->ip_range.read_table_from_file(socks_config_fd, "no_socks"); + tmp = Load_IpMap_From_File(&socks_conf_stuff->ip_map, socks_config_fd, "no_socks"); + // tmp = socks_conf_stuff->ip_range.read_table_from_file(socks_config_fd, "no_socks"); if (tmp) { Error("SOCKS Config: Error while reading ip_range: %s.", tmp); @@ -546,16 +525,15 @@ error: socks_conf_stuff->accept_enabled = 0; if (socks_config_fd >= 0) ::close(socks_config_fd); - } int -loadSocksAuthInfo(int fd, socks_conf_struct * socks_stuff) +loadSocksAuthInfo(int fd, socks_conf_struct *socks_stuff) { char c = '\0'; - char line[256] = { 0 }; // initialize all chars to nil - char user_name[256] = { 0 }; - char passwd[256] = { 0 }; + char line[256] = {0}; // initialize all chars to nil + char user_name[256] = {0}; + char passwd[256] = {0}; if (lseek(fd, 0, SEEK_SET) < 0) { Warning("Can not seek on Socks configuration file\n"); @@ -597,25 +575,24 @@ loadSocksAuthInfo(int fd, socks_conf_struct * socks_stuff) } int -socks5BasicAuthHandler(int event, unsigned char *p, void (**h_ptr) (void)) +socks5BasicAuthHandler(int event, unsigned char *p, void (**h_ptr)(void)) { - //for more info on Socks5 see RFC 1928 + // for more info on Socks5 see RFC 1928 int ret = 0; char *pass_phrase = netProcessor.socks_conf_stuff->user_name_n_passwd; switch (event) { - case SOCKS_AUTH_OPEN: - p[ret++] = SOCKS5_VERSION; //version - p[ret++] = (pass_phrase) ? 2 : 1; //#Methods - p[ret++] = 0; //no authentication + p[ret++] = SOCKS5_VERSION; // version + p[ret++] = (pass_phrase) ? 2 : 1; //#Methods + p[ret++] = 0; // no authentication if (pass_phrase) p[ret++] = 2; break; case SOCKS_AUTH_WRITE_COMPLETE: - //return number of bytes to read + // return number of bytes to read ret = 2; break; @@ -623,44 +600,45 @@ socks5BasicAuthHandler(int event, unsigned char *p, void (**h_ptr) (void)) if (p[0] == SOCKS5_VERSION) { switch (p[1]) { - - case 0: // no authentication required + case 0: // no authentication required Debug("Socks", "No authentication required for Socks server"); - //make sure this is ok for us. right now it is always ok for us. + // make sure this is ok for us. right now it is always ok for us. *h_ptr = NULL; break; case 2: Debug("Socks", "Socks server wants username/passwd"); if (!pass_phrase) { - Debug("Socks", "Buggy Socks server: asks for username/passwd " "when not supplied as an option"); + Debug("Socks", "Buggy Socks server: asks for username/passwd " + "when not supplied as an option"); ret = -1; *h_ptr = NULL; } else - *(SocksAuthHandler *) h_ptr = &socks5PasswdAuthHandler; + *(SocksAuthHandler *)h_ptr = &socks5PasswdAuthHandler; break; case 0xff: - Debug("Socks", "None of the Socks authentcations is acceptable " "to the server"); + Debug("Socks", "None of the Socks authentcations is acceptable " + "to the server"); *h_ptr = NULL; ret = -1; break; default: - Debug("Socks", "Unexpected Socks auth method (%d) from the server", (int) p[1]); + Debug("Socks", "Unexpected Socks auth method (%d) from the server", (int)p[1]); ret = -1; break; } } else { - Debug("Socks", "authEvent got wrong version %d from the Socks server", (int) p[0]); + Debug("Socks", "authEvent got wrong version %d from the Socks server", (int)p[0]); ret = -1; } break; default: - //This should be inpossible + // This should be inpossible ink_assert(!"bad case value"); ret = -1; break; @@ -669,48 +647,46 @@ socks5BasicAuthHandler(int event, unsigned char *p, void (**h_ptr) (void)) } int -socks5PasswdAuthHandler(int event, unsigned char *p, void (**h_ptr) (void)) +socks5PasswdAuthHandler(int event, unsigned char *p, void (**h_ptr)(void)) { - //for more info see RFC 1929 + // for more info see RFC 1929 int ret = 0; char *pass_phrase; int pass_len; switch (event) { - case SOCKS_AUTH_OPEN: pass_phrase = netProcessor.socks_conf_stuff->user_name_n_passwd; pass_len = netProcessor.socks_conf_stuff->user_name_n_passwd_len; ink_assert(pass_phrase); - p[0] = 1; //version + p[0] = 1; // version memcpy(&p[1], pass_phrase, pass_len); ret = 1 + pass_len; break; case SOCKS_AUTH_WRITE_COMPLETE: - //return number of bytes to read + // return number of bytes to read ret = 2; break; case SOCKS_AUTH_READ_COMPLETE: - //if (p[0] == 1) { // skip this. its not clear what this should be. + // if (p[0] == 1) { // skip this. its not clear what this should be. // NEC thinks it is 5 RFC seems to indicate 1. switch (p[1]) { - case 0: Debug("Socks", "Username/Passwd succeded"); *h_ptr = NULL; break; default: - Debug("Socks", "Username/Passwd authentication failed ret_code: %d", (int) p[1]); + Debug("Socks", "Username/Passwd authentication failed ret_code: %d", (int)p[1]); ret = -1; } //} - //else { + // else { // Debug("Socks", "authPassEvent got wrong version %d from " // "Socks server\n", (int)p[0]); // ret = -1;
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/iocore/net/UnixConnection.cc ---------------------------------------------------------------------- diff --git a/iocore/net/UnixConnection.cc b/iocore/net/UnixConnection.cc index 420add1..303175a 100644 --- a/iocore/net/UnixConnection.cc +++ b/iocore/net/UnixConnection.cc @@ -31,10 +31,10 @@ // set in the OS // #define RECV_BUF_SIZE (1024*64) // #define SEND_BUF_SIZE (1024*64) -#define FIRST_RANDOM_PORT 16000 -#define LAST_RANDOM_PORT 32000 +#define FIRST_RANDOM_PORT 16000 +#define LAST_RANDOM_PORT 32000 -#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) +#define ROUNDUP(x, y) ((((x) + ((y)-1)) / (y)) * (y)) #if TS_USE_TPROXY #if !defined(IP_TRANSPARENT) @@ -46,12 +46,10 @@ unsigned int const IP_TRANSPARENT = 19; // Functions // int -Connection::setup_mc_send( - sockaddr const* mc_addr, - sockaddr const* my_addr, - bool non_blocking, unsigned char mc_ttl, bool mc_loopback, Continuation * c -) { - (void) c; +Connection::setup_mc_send(sockaddr const *mc_addr, sockaddr const *my_addr, bool non_blocking, unsigned char mc_ttl, + bool mc_loopback, Continuation *c) +{ + (void)c; ink_assert(fd == NO_FD); int res = 0; int enable_reuseaddr = 1; @@ -62,7 +60,7 @@ Connection::setup_mc_send( fd = res; - if ((res = safe_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &enable_reuseaddr, sizeof(enable_reuseaddr)) < 0)) { + if ((res = safe_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&enable_reuseaddr, sizeof(enable_reuseaddr)) < 0)) { goto Lerror; } @@ -82,18 +80,18 @@ Connection::setup_mc_send( goto Lerror; // Set MultiCast TTL to specified value - if ((res = safe_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &mc_ttl, sizeof(mc_ttl)) < 0)) + if ((res = safe_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&mc_ttl, sizeof(mc_ttl)) < 0)) goto Lerror; // Set MultiCast Interface to specified value - if ((res = safe_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char *) &mc_if, sizeof(mc_if)) < 0)) + if ((res = safe_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&mc_if, sizeof(mc_if)) < 0)) goto Lerror; // Disable MultiCast loopback if requested if (!mc_loopback) { char loop = 0; - if ((res = safe_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof(loop)) < 0)) + if ((res = safe_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loop, sizeof(loop)) < 0)) goto Lerror; } return 0; @@ -106,14 +104,12 @@ Lerror: int -Connection::setup_mc_receive( - sockaddr const* mc_addr, - sockaddr const* my_addr, - bool non_blocking, Connection * sendChan, Continuation * c -) { +Connection::setup_mc_receive(sockaddr const *mc_addr, sockaddr const *my_addr, bool non_blocking, Connection *sendChan, + Continuation *c) +{ ink_assert(fd == NO_FD); - (void) sendChan; - (void) c; + (void)sendChan; + (void)c; int res = 0; int enable_reuseaddr = 1; IpAddr inaddr_any(INADDR_ANY); @@ -128,7 +124,7 @@ Connection::setup_mc_receive( goto Lerror; #endif - if ((res = safe_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &enable_reuseaddr, sizeof(enable_reuseaddr)) < 0)) + if ((res = safe_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&enable_reuseaddr, sizeof(enable_reuseaddr)) < 0)) goto Lerror; addr.assign(inaddr_any, ats_ip_port_cast(mc_addr)); @@ -146,7 +142,7 @@ Connection::setup_mc_receive( mc_request.imr_multiaddr.s_addr = ats_ip4_addr_cast(mc_addr); mc_request.imr_interface.s_addr = ats_ip4_addr_cast(my_addr); - if ((res = safe_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mc_request, sizeof(mc_request)) < 0)) + if ((res = safe_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mc_request, sizeof(mc_request)) < 0)) goto Lerror; } return 0; @@ -157,42 +153,51 @@ Lerror: return res; } -namespace { - /** Struct to make cleaning up resources easier. - - By default, the @a method is invoked on the @a object when - this object is destructed. This can be prevented by calling - the @c reset method. - - This is not overly useful in the allocate, check, return case - but very handy if there are - - multiple resources (each can have its own cleaner) - - multiple checks against the resource - In such cases, rather than trying to track all the resources - that might need cleaned up, you can set up a cleaner at allocation - and only have to deal with them on success, which is generally - singular. - - @code - self::some_method (...) { - /// allocate resource - cleaner<self> clean_up(this, &self::cleanup); - // modify or check the resource - if (fail) return FAILURE; // cleanup() is called - /// success! - clean_up.reset(); // cleanup() not called after this - return SUCCESS; - @endcode - */ - template <typename T> struct cleaner { - T* obj; ///< Object instance. - typedef void (T::*method)(); ///< Method signature. - method m; - - cleaner(T* _obj, method _method) : obj(_obj), m(_method) {} - ~cleaner() { if (obj) (obj->*m)(); } - void reset() { obj = 0; } - }; +namespace +{ +/** Struct to make cleaning up resources easier. + + By default, the @a method is invoked on the @a object when + this object is destructed. This can be prevented by calling + the @c reset method. + + This is not overly useful in the allocate, check, return case + but very handy if there are + - multiple resources (each can have its own cleaner) + - multiple checks against the resource + In such cases, rather than trying to track all the resources + that might need cleaned up, you can set up a cleaner at allocation + and only have to deal with them on success, which is generally + singular. + + @code + self::some_method (...) { + /// allocate resource + cleaner<self> clean_up(this, &self::cleanup); + // modify or check the resource + if (fail) return FAILURE; // cleanup() is called + /// success! + clean_up.reset(); // cleanup() not called after this + return SUCCESS; + @endcode + */ +template <typename T> struct cleaner { + T *obj; ///< Object instance. + typedef void (T::*method)(); ///< Method signature. + method m; + + cleaner(T *_obj, method _method) : obj(_obj), m(_method) {} + ~cleaner() + { + if (obj) + (obj->*m)(); + } + void + reset() + { + obj = 0; + } +}; } /** Default options. @@ -215,15 +220,13 @@ namespace { NetVCOptions const Connection::DEFAULT_OPTIONS; int -Connection::open(NetVCOptions const& opt) +Connection::open(NetVCOptions const &opt) { ink_assert(fd == NO_FD); int enable_reuseaddr = 1; // used for sockopt setting - int res = 0; // temp result + int res = 0; // temp result IpEndpoint local_addr; - sock_type = NetVCOptions::USE_UDP == opt.ip_proto - ? SOCK_DGRAM - : SOCK_STREAM; + sock_type = NetVCOptions::USE_UDP == opt.ip_proto ? SOCK_DGRAM : SOCK_STREAM; int family; // Need to do address calculations first, so we can determine the @@ -231,9 +234,7 @@ Connection::open(NetVCOptions const& opt) ink_zero(local_addr); bool is_any_address = false; - if (NetVCOptions::FOREIGN_ADDR == opt.addr_binding || - NetVCOptions::INTF_ADDR == opt.addr_binding - ) { + if (NetVCOptions::FOREIGN_ADDR == opt.addr_binding || NetVCOptions::INTF_ADDR == opt.addr_binding) { // Same for now, transparency for foreign addresses must be handled // *after* the socket is created, and we need to do this calculation // before the socket to get the IP family correct. @@ -249,7 +250,8 @@ Connection::open(NetVCOptions const& opt) } res = socketManager.socket(family, sock_type, 0); - if (-1 == res) return -errno; + if (-1 == res) + return -errno; fd = res; // mark fd for close until we succeed. @@ -257,20 +259,14 @@ Connection::open(NetVCOptions const& opt) // Try setting the various socket options, if requested. - if (-1 == safe_setsockopt(fd, - SOL_SOCKET, - SO_REUSEADDR, - reinterpret_cast<char *>(&enable_reuseaddr), - sizeof(enable_reuseaddr))) + if (-1 == safe_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&enable_reuseaddr), sizeof(enable_reuseaddr))) return -errno; if (NetVCOptions::FOREIGN_ADDR == opt.addr_binding) { - static char const * const DEBUG_TEXT = "::open setsockopt() IP_TRANSPARENT"; + static char const *const DEBUG_TEXT = "::open setsockopt() IP_TRANSPARENT"; #if TS_USE_TPROXY int value = 1; - if (-1 == safe_setsockopt(fd, SOL_IP, TS_IP_TRANSPARENT, - reinterpret_cast<char*>(&value), sizeof(value) - )) { + if (-1 == safe_setsockopt(fd, SOL_IP, TS_IP_TRANSPARENT, reinterpret_cast<char *>(&value), sizeof(value))) { Debug("socket", "%s - fail %d:%s", DEBUG_TEXT, errno, strerror(errno)); return -errno; } else { @@ -306,7 +302,7 @@ Connection::open(NetVCOptions const& opt) // apply dynamic options apply_options(opt); - if(local_addr.port() || !is_any_address) { + if (local_addr.port() || !is_any_address) { if (-1 == socketManager.ink_bind(fd, &local_addr.sa, ats_ip_size(&local_addr.sa))) return -errno; } @@ -317,7 +313,8 @@ Connection::open(NetVCOptions const& opt) } int -Connection::connect(sockaddr const* target, NetVCOptions const& opt) { +Connection::connect(sockaddr const *target, NetVCOptions const &opt) +{ ink_assert(fd != NO_FD); ink_assert(is_bound); ink_assert(!is_connected); @@ -335,14 +332,14 @@ Connection::connect(sockaddr const* target, NetVCOptions const& opt) { // (Is EWOULDBLOCK ok? Does that start the connect?) // We also want to handle the cases where the connect blocking // and IO blocking differ, by turning it on or off as needed. - if (-1 == res - && (opt.f_blocking_connect - || ! (EINPROGRESS == errno || EWOULDBLOCK == errno))) { + if (-1 == res && (opt.f_blocking_connect || !(EINPROGRESS == errno || EWOULDBLOCK == errno))) { return -errno; } else if (opt.f_blocking_connect && !opt.f_blocking) { - if (-1 == safe_nonblocking(fd)) return -errno; + if (-1 == safe_nonblocking(fd)) + return -errno; } else if (!opt.f_blocking_connect && opt.f_blocking) { - if (-1 == safe_blocking(fd)) return -errno; + if (-1 == safe_blocking(fd)) + return -errno; } cleanup.reset(); @@ -357,7 +354,7 @@ Connection::_cleanup() } void -Connection::apply_options(NetVCOptions const& opt) +Connection::apply_options(NetVCOptions const &opt) { // Set options which can be changed after a connection is established // ignore other changes @@ -372,7 +369,7 @@ Connection::apply_options(NetVCOptions const& opt) } if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_LINGER_ON) { struct linger l; - l.l_onoff = 1; + l.l_onoff = 1; l.l_linger = 0; safe_setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l)); Debug("socket", "::open:: setsockopt() turn on SO_LINGER on socket"); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/iocore/net/UnixNet.cc ---------------------------------------------------------------------- diff --git a/iocore/net/UnixNet.cc b/iocore/net/UnixNet.cc index a654483..03c3bec 100644 --- a/iocore/net/UnixNet.cc +++ b/iocore/net/UnixNet.cc @@ -40,7 +40,8 @@ int update_cop_config(const char *name, RecDataT data_type, RecData data, void * // INKqa10496 // One Inactivity cop runs on each thread once every second and // loops through the list of NetVCs and calls the timeouts -class InactivityCop : public Continuation { +class InactivityCop : public Continuation +{ public: InactivityCop(ProxyMutex *m) : Continuation(m), default_inactivity_timeout(0), total_connections_in(0), max_connections_in(0), connections_per_thread_in(0) @@ -54,13 +55,16 @@ public: RecRegisterConfigUpdateCb("proxy.config.net.default_inactivity_timeout", update_cop_config, (void *)this); } - int check_inactivity(int event, Event *e) { - (void) event; + int + check_inactivity(int event, Event *e) + { + (void)event; ink_hrtime now = ink_get_hrtime(); NetHandler &nh = *get_NetHandler(this_ethread()); total_connections_in = 0; // Copy the list and use pop() to catch any closes caused by callbacks. - forl_LL(UnixNetVConnection, vc, nh.open_list) { + forl_LL(UnixNetVConnection, vc, nh.open_list) + { if (vc->thread == this_ethread()) { if (vc->from_accept_thread == true) { ++total_connections_in; @@ -72,8 +76,8 @@ public: // If we cannot get the lock don't stop just keep cleaning MUTEX_TRY_LOCK(lock, vc->mutex, this_ethread()); if (!lock.is_locked()) { - NET_INCREMENT_DYN_STAT(inactivity_cop_lock_acquire_failure_stat); - continue; + NET_INCREMENT_DYN_STAT(inactivity_cop_lock_acquire_failure_stat); + continue; } if (vc->closed) { @@ -83,13 +87,12 @@ public: // set a default inactivity timeout if one is not set if (vc->next_inactivity_timeout_at == 0 && default_inactivity_timeout > 0) { - Debug("inactivity_cop", "vc: %p inactivity timeout not set, setting a default of %d", vc, - default_inactivity_timeout); + Debug("inactivity_cop", "vc: %p inactivity timeout not set, setting a default of %d", vc, default_inactivity_timeout); vc->set_inactivity_timeout(HRTIME_SECONDS(default_inactivity_timeout)); NET_INCREMENT_DYN_STAT(default_inactivity_timeout_stat); } else { - Debug("inactivity_cop_verbose", "vc: %p now: %" PRId64 " timeout at: %" PRId64 " timeout in: %" PRId64, vc, - now, ink_hrtime_to_sec(vc->next_inactivity_timeout_at), ink_hrtime_to_sec(vc->inactivity_timeout_in)); + Debug("inactivity_cop_verbose", "vc: %p now: %" PRId64 " timeout at: %" PRId64 " timeout in: %" PRId64, vc, now, + ink_hrtime_to_sec(vc->next_inactivity_timeout_at), ink_hrtime_to_sec(vc->inactivity_timeout_in)); } if (vc->next_inactivity_timeout_at && vc->next_inactivity_timeout_at < now) { @@ -99,8 +102,8 @@ public: NET_SUM_DYN_STAT(keep_alive_lru_timeout_total_stat, diff); NET_INCREMENT_DYN_STAT(keep_alive_lru_timeout_count_stat); } - Debug("inactivity_cop_verbose", "vc: %p now: %" PRId64 " timeout at: %" PRId64 " timeout in: %" PRId64, vc, - now, vc->next_inactivity_timeout_at, vc->inactivity_timeout_in); + Debug("inactivity_cop_verbose", "vc: %p now: %" PRId64 " timeout at: %" PRId64 " timeout in: %" PRId64, vc, now, + vc->next_inactivity_timeout_at, vc->inactivity_timeout_in); vc->handleEvent(EVENT_IMMEDIATE, e); } } @@ -111,13 +114,25 @@ public: return 0; } - void set_max_connections(const int32_t x) { max_connections_in = x; } - void set_connections_per_thread(const int32_t x) { connections_per_thread_in = x; } - void set_default_timeout(const int x) { default_inactivity_timeout = x; } + void + set_max_connections(const int32_t x) + { + max_connections_in = x; + } + void + set_connections_per_thread(const int32_t x) + { + connections_per_thread_in = x; + } + void + set_default_timeout(const int x) + { + default_inactivity_timeout = x; + } private: void keep_alive_lru(NetHandler &nh, ink_hrtime now, Event *e); - int default_inactivity_timeout; // only used when one is not set for some bad reason + int default_inactivity_timeout; // only used when one is not set for some bad reason int32_t total_connections_in; int32_t max_connections_in; int32_t connections_per_thread_in; @@ -126,7 +141,7 @@ private: int update_cop_config(const char *name, RecDataT data_type ATS_UNUSED, RecData data, void *cookie) { - InactivityCop * cop = static_cast<InactivityCop *>(cookie); + InactivityCop *cop = static_cast<InactivityCop *>(cookie); ink_assert(cop != NULL); if (cop != NULL) { @@ -140,13 +155,13 @@ update_cop_config(const char *name, RecDataT data_type ATS_UNUSED, RecData data, Debug("inactivity_cop_dynamic", "proxy.config.net.default_inactivity_timeout updated to %" PRId64, data.rec_int); cop->set_default_timeout(data.rec_int); } - } return REC_ERR_OKAY; } -void InactivityCop::keep_alive_lru(NetHandler &nh, const ink_hrtime now, Event *e) +void +InactivityCop::keep_alive_lru(NetHandler &nh, const ink_hrtime now, Event *e) { // maximum incoming connections is set to 0 then the feature is disabled if (max_connections_in == 0) { @@ -156,8 +171,7 @@ void InactivityCop::keep_alive_lru(NetHandler &nh, const ink_hrtime now, Event * if (connections_per_thread_in == 0) { // figure out the number of threads and calculate the number of connections per thread const int event_threads = eventProcessor.n_threads_for_type[ET_NET]; - const int ssl_threads = (ET_NET == SSLNetProcessor::ET_SSL) ? 0 : - eventProcessor.n_threads_for_type[SSLNetProcessor::ET_SSL]; + const int ssl_threads = (ET_NET == SSLNetProcessor::ET_SSL) ? 0 : eventProcessor.n_threads_for_type[SSLNetProcessor::ET_SSL]; connections_per_thread_in = max_connections_in / (event_threads + ssl_threads); } @@ -169,8 +183,9 @@ void InactivityCop::keep_alive_lru(NetHandler &nh, const ink_hrtime now, Event * to_process = min((int32_t)nh.keep_alive_lru_size, to_process); Debug("inactivity_cop_dynamic", "max cons: %d active: %d idle: %d process: %d" - " net type: %d ssl type: %d", connections_per_thread_in, total_connections_in - nh.keep_alive_lru_size, - nh.keep_alive_lru_size, to_process, ET_NET, SSLNetProcessor::ET_SSL); + " net type: %d ssl type: %d", + connections_per_thread_in, total_connections_in - nh.keep_alive_lru_size, nh.keep_alive_lru_size, to_process, ET_NET, + SSLNetProcessor::ET_SSL); // loop over the non-active connections and try to close them UnixNetVConnection *vc = nh.keep_alive_list.head; @@ -195,9 +210,9 @@ void InactivityCop::keep_alive_lru(NetHandler &nh, const ink_hrtime now, Event * NET_SUM_DYN_STAT(keep_alive_lru_timeout_total_stat, diff); NET_INCREMENT_DYN_STAT(keep_alive_lru_timeout_count_stat); } - Debug("inactivity_cop_dynamic", "closing connection NetVC=%p idle: %u now: %" PRId64 " at: %" PRId64 - " in: %" PRId64 " diff: %" PRId64, - vc, nh.keep_alive_lru_size, ink_hrtime_to_sec(now), ink_hrtime_to_sec(vc->next_inactivity_timeout_at), + Debug("inactivity_cop_dynamic", + "closing connection NetVC=%p idle: %u now: %" PRId64 " at: %" PRId64 " in: %" PRId64 " diff: %" PRId64, vc, + nh.keep_alive_lru_size, ink_hrtime_to_sec(now), ink_hrtime_to_sec(vc->next_inactivity_timeout_at), ink_hrtime_to_sec(vc->inactivity_timeout_in), diff); if (vc->closed) { close_UnixNetVConnection(vc, e->ethread); @@ -211,27 +226,30 @@ void InactivityCop::keep_alive_lru(NetHandler &nh, const ink_hrtime now, Event * if (total_idle_count > 0) { Debug("inactivity_cop_dynamic", "max cons: %d active: %d idle: %d already closed: %d, close event: %d" - " mean idle: %d\n", connections_per_thread_in, - total_connections_in - nh.keep_alive_lru_size - closed - handle_event, - nh.keep_alive_lru_size, closed, handle_event, total_idle_time / total_idle_count); + " mean idle: %d\n", + connections_per_thread_in, total_connections_in - nh.keep_alive_lru_size - closed - handle_event, nh.keep_alive_lru_size, + closed, handle_event, total_idle_time / total_idle_count); } } #endif -PollCont::PollCont(ProxyMutex *m, int pt):Continuation(m), net_handler(NULL), nextPollDescriptor(NULL), poll_timeout(pt) { +PollCont::PollCont(ProxyMutex *m, int pt) : Continuation(m), net_handler(NULL), nextPollDescriptor(NULL), poll_timeout(pt) +{ pollDescriptor = new PollDescriptor; pollDescriptor->init(); SET_HANDLER(&PollCont::pollEvent); } -PollCont::PollCont(ProxyMutex *m, NetHandler *nh, int pt):Continuation(m), net_handler(nh), nextPollDescriptor(NULL), poll_timeout(pt) +PollCont::PollCont(ProxyMutex *m, NetHandler *nh, int pt) + : Continuation(m), net_handler(nh), nextPollDescriptor(NULL), poll_timeout(pt) { pollDescriptor = new PollDescriptor; pollDescriptor->init(); SET_HANDLER(&PollCont::pollEvent); } -PollCont::~PollCont() { +PollCont::~PollCont() +{ delete pollDescriptor; if (nextPollDescriptor != NULL) { delete nextPollDescriptor; @@ -243,51 +261,47 @@ PollCont::~PollCont() { // and stores the resultant events in ePoll_Triggered_Events // int -PollCont::pollEvent(int event, Event *e) { - (void) event; - (void) e; +PollCont::pollEvent(int event, Event *e) +{ + (void)event; + (void)e; if (likely(net_handler)) { /* checking to see whether there are connections on the ready_queue (either read or write) that need processing [ebalsa] */ - if (likely - (!net_handler->read_ready_list.empty() || !net_handler->write_ready_list.empty() || - !net_handler->read_enable_list.empty() || !net_handler->write_enable_list.empty())) { - NetDebug("iocore_net_poll", "rrq: %d, wrq: %d, rel: %d, wel: %d", - net_handler->read_ready_list.empty(), + if (likely(!net_handler->read_ready_list.empty() || !net_handler->write_ready_list.empty() || + !net_handler->read_enable_list.empty() || !net_handler->write_enable_list.empty())) { + NetDebug("iocore_net_poll", "rrq: %d, wrq: %d, rel: %d, wel: %d", net_handler->read_ready_list.empty(), net_handler->write_ready_list.empty(), net_handler->read_enable_list.empty(), net_handler->write_enable_list.empty()); - poll_timeout = 0; //poll immediately returns -- we have triggered stuff to process right now + poll_timeout = 0; // poll immediately returns -- we have triggered stuff to process right now } else { poll_timeout = net_config_poll_timeout; } } - // wait for fd's to tigger, or don't wait if timeout is 0 +// wait for fd's to tigger, or don't wait if timeout is 0 #if TS_USE_EPOLL - pollDescriptor->result = epoll_wait(pollDescriptor->epoll_fd, - pollDescriptor->ePoll_Triggered_Events, POLL_DESCRIPTOR_SIZE, poll_timeout); - NetDebug("iocore_net_poll", "[PollCont::pollEvent] epoll_fd: %d, timeout: %d, results: %d", pollDescriptor->epoll_fd, poll_timeout, - pollDescriptor->result); + pollDescriptor->result = + epoll_wait(pollDescriptor->epoll_fd, pollDescriptor->ePoll_Triggered_Events, POLL_DESCRIPTOR_SIZE, poll_timeout); + NetDebug("iocore_net_poll", "[PollCont::pollEvent] epoll_fd: %d, timeout: %d, results: %d", pollDescriptor->epoll_fd, + poll_timeout, pollDescriptor->result); #elif TS_USE_KQUEUE struct timespec tv; tv.tv_sec = poll_timeout / 1000; tv.tv_nsec = 1000000 * (poll_timeout % 1000); - pollDescriptor->result = kevent(pollDescriptor->kqueue_fd, NULL, 0, - pollDescriptor->kq_Triggered_Events, - POLL_DESCRIPTOR_SIZE, - &tv); - NetDebug("iocore_net_poll", "[PollCont::pollEvent] kueue_fd: %d, timeout: %d, results: %d", pollDescriptor->kqueue_fd, poll_timeout, - pollDescriptor->result); + pollDescriptor->result = + kevent(pollDescriptor->kqueue_fd, NULL, 0, pollDescriptor->kq_Triggered_Events, POLL_DESCRIPTOR_SIZE, &tv); + NetDebug("iocore_net_poll", "[PollCont::pollEvent] kueue_fd: %d, timeout: %d, results: %d", pollDescriptor->kqueue_fd, + poll_timeout, pollDescriptor->result); #elif TS_USE_PORT int retval; timespec_t ptimeout; ptimeout.tv_sec = poll_timeout / 1000; ptimeout.tv_nsec = 1000000 * (poll_timeout % 1000); unsigned nget = 1; - if((retval = port_getn(pollDescriptor->port_fd, - pollDescriptor->Port_Triggered_Events, - POLL_DESCRIPTOR_SIZE, &nget, &ptimeout)) < 0) { + if ((retval = port_getn(pollDescriptor->port_fd, pollDescriptor->Port_Triggered_Events, POLL_DESCRIPTOR_SIZE, &nget, &ptimeout)) < + 0) { pollDescriptor->result = 0; - switch(errno) { + switch (errno) { case EINTR: case EAGAIN: case ETIME: @@ -302,9 +316,8 @@ PollCont::pollEvent(int event, Event *e) { } else { pollDescriptor->result = (int)nget; } - NetDebug("iocore_net_poll", "[PollCont::pollEvent] %d[%s]=port_getn(%d,%p,%d,%d,%d),results(%d)", - retval,retval < 0 ? strerror(errno) : "ok", - pollDescriptor->port_fd, pollDescriptor->Port_Triggered_Events, + NetDebug("iocore_net_poll", "[PollCont::pollEvent] %d[%s]=port_getn(%d,%p,%d,%d,%d),results(%d)", retval, + retval < 0 ? strerror(errno) : "ok", pollDescriptor->port_fd, pollDescriptor->Port_Triggered_Events, POLL_DESCRIPTOR_SIZE, nget, poll_timeout, pollDescriptor->result); #else #error port me @@ -313,12 +326,13 @@ PollCont::pollEvent(int event, Event *e) { } static void -net_signal_hook_callback(EThread *thread) { +net_signal_hook_callback(EThread *thread) +{ #if HAVE_EVENTFD uint64_t counter; ATS_UNUSED_RETURN(read(thread->evfd, &counter, sizeof(uint64_t))); #elif TS_USE_PORT - /* Nothing to drain or do */ +/* Nothing to drain or do */ #else char dummy[1024]; ATS_UNUSED_RETURN(read(thread->evpipe[0], &dummy[0], 1024)); @@ -326,7 +340,8 @@ net_signal_hook_callback(EThread *thread) { } static void -net_signal_hook_function(EThread *thread) { +net_signal_hook_function(EThread *thread) +{ #if HAVE_EVENTFD uint64_t counter = 1; ATS_UNUSED_RETURN(write(thread->evfd, &counter, sizeof(uint64_t))); @@ -342,8 +357,8 @@ net_signal_hook_function(EThread *thread) { void initialize_thread_for_net(EThread *thread) { - new((ink_dummy_for_new *) get_NetHandler(thread)) NetHandler(); - new((ink_dummy_for_new *) get_PollCont(thread)) PollCont(thread->mutex, get_NetHandler(thread)); + new ((ink_dummy_for_new *)get_NetHandler(thread)) NetHandler(); + new ((ink_dummy_for_new *)get_PollCont(thread)) PollCont(thread->mutex, get_NetHandler(thread)); get_NetHandler(thread)->mutex = new_ProxyMutex(); PollCont *pc = get_PollCont(thread); PollDescriptor *pd = pc->pollDescriptor; @@ -356,7 +371,7 @@ initialize_thread_for_net(EThread *thread) #endif thread->signal_hook = net_signal_hook_function; - thread->ep = (EventIO*)ats_malloc(sizeof(EventIO)); + thread->ep = (EventIO *)ats_malloc(sizeof(EventIO)); thread->ep->type = EVENTIO_ASYNC_SIGNAL; #if HAVE_EVENTFD thread->ep->start(pd, thread->evfd, 0, EVENTIO_READ); @@ -367,9 +382,9 @@ initialize_thread_for_net(EThread *thread) // NetHandler method definitions -NetHandler::NetHandler():Continuation(NULL), trigger_event(0), keep_alive_lru_size(0) +NetHandler::NetHandler() : Continuation(NULL), trigger_event(0), keep_alive_lru_size(0) { - SET_HANDLER((NetContHandler) & NetHandler::startNetEvent); + SET_HANDLER((NetContHandler)&NetHandler::startNetEvent); } // @@ -379,8 +394,8 @@ NetHandler::NetHandler():Continuation(NULL), trigger_event(0), keep_alive_lru_si int NetHandler::startNetEvent(int event, Event *e) { - (void) event; - SET_HANDLER((NetContHandler) & NetHandler::mainNetEvent); + (void)event; + SET_HANDLER((NetContHandler)&NetHandler::mainNetEvent); e->schedule_every(NET_PERIOD); trigger_event = e; return EVENT_CONT; @@ -423,8 +438,8 @@ int NetHandler::mainNetEvent(int event, Event *e) { ink_assert(trigger_event == e && (event == EVENT_INTERVAL || event == EVENT_POLL)); - (void) event; - (void) e; + (void)event; + (void)e; EventIO *epd = NULL; int poll_timeout; @@ -440,22 +455,23 @@ NetHandler::mainNetEvent(int event, Event *e) UnixNetVConnection *vc = NULL; #if TS_USE_EPOLL pd->result = epoll_wait(pd->epoll_fd, pd->ePoll_Triggered_Events, POLL_DESCRIPTOR_SIZE, poll_timeout); - NetDebug("iocore_net_main_poll", "[NetHandler::mainNetEvent] epoll_wait(%d,%d), result=%d", pd->epoll_fd,poll_timeout,pd->result); + NetDebug("iocore_net_main_poll", "[NetHandler::mainNetEvent] epoll_wait(%d,%d), result=%d", pd->epoll_fd, poll_timeout, + pd->result); #elif TS_USE_KQUEUE struct timespec tv; tv.tv_sec = poll_timeout / 1000; tv.tv_nsec = 1000000 * (poll_timeout % 1000); pd->result = kevent(pd->kqueue_fd, NULL, 0, pd->kq_Triggered_Events, POLL_DESCRIPTOR_SIZE, &tv); - NetDebug("iocore_net_main_poll", "[NetHandler::mainNetEvent] kevent(%d,%d), result=%d", pd->kqueue_fd,poll_timeout,pd->result); + NetDebug("iocore_net_main_poll", "[NetHandler::mainNetEvent] kevent(%d,%d), result=%d", pd->kqueue_fd, poll_timeout, pd->result); #elif TS_USE_PORT int retval; timespec_t ptimeout; ptimeout.tv_sec = poll_timeout / 1000; ptimeout.tv_nsec = 1000000 * (poll_timeout % 1000); unsigned nget = 1; - if((retval = port_getn(pd->port_fd, pd->Port_Triggered_Events, POLL_DESCRIPTOR_SIZE, &nget, &ptimeout)) < 0) { + if ((retval = port_getn(pd->port_fd, pd->Port_Triggered_Events, POLL_DESCRIPTOR_SIZE, &nget, &ptimeout)) < 0) { pd->result = 0; - switch(errno) { + switch (errno) { case EINTR: case EAGAIN: case ETIME: @@ -470,10 +486,9 @@ NetHandler::mainNetEvent(int event, Event *e) } else { pd->result = (int)nget; } - NetDebug("iocore_net_main_poll", "[NetHandler::mainNetEvent] %d[%s]=port_getn(%d,%p,%d,%d,%d),results(%d)", - retval,retval < 0 ? strerror(errno) : "ok", - pd->port_fd, pd->Port_Triggered_Events, - POLL_DESCRIPTOR_SIZE, nget, poll_timeout, pd->result); + NetDebug("iocore_net_main_poll", "[NetHandler::mainNetEvent] %d[%s]=port_getn(%d,%p,%d,%d,%d),results(%d)", retval, + retval < 0 ? strerror(errno) : "ok", pd->port_fd, pd->Port_Triggered_Events, POLL_DESCRIPTOR_SIZE, nget, poll_timeout, + pd->result); #else #error port me @@ -481,32 +496,31 @@ NetHandler::mainNetEvent(int event, Event *e) vc = NULL; for (int x = 0; x < pd->result; x++) { - epd = (EventIO*) get_ev_data(pd,x); + epd = (EventIO *)get_ev_data(pd, x); if (epd->type == EVENTIO_READWRITE_VC) { vc = epd->data.vc; - if (get_ev_events(pd,x) & (EVENTIO_READ|EVENTIO_ERROR)) { + if (get_ev_events(pd, x) & (EVENTIO_READ | EVENTIO_ERROR)) { vc->read.triggered = 1; if (!read_ready_list.in(vc)) read_ready_list.enqueue(vc); - else if (get_ev_events(pd,x) & EVENTIO_ERROR) { + else if (get_ev_events(pd, x) & EVENTIO_ERROR) { // check for unhandled epoll events that should be handled Debug("iocore_net_main", "Unhandled epoll event on read: 0x%04x read.enabled=%d closed=%d read.netready_queue=%d", - get_ev_events(pd,x), vc->read.enabled, vc->closed, read_ready_list.in(vc)); + get_ev_events(pd, x), vc->read.enabled, vc->closed, read_ready_list.in(vc)); } } vc = epd->data.vc; - if (get_ev_events(pd,x) & (EVENTIO_WRITE|EVENTIO_ERROR)) { + if (get_ev_events(pd, x) & (EVENTIO_WRITE | EVENTIO_ERROR)) { vc->write.triggered = 1; if (!write_ready_list.in(vc)) write_ready_list.enqueue(vc); - else if (get_ev_events(pd,x) & EVENTIO_ERROR) { + else if (get_ev_events(pd, x) & EVENTIO_ERROR) { // check for unhandled epoll events that should be handled - Debug("iocore_net_main", - "Unhandled epoll event on write: 0x%04x write.enabled=%d closed=%d write.netready_queue=%d", - get_ev_events(pd,x), vc->write.enabled, vc->closed, write_ready_list.in(vc)); + Debug("iocore_net_main", "Unhandled epoll event on write: 0x%04x write.enabled=%d closed=%d write.netready_queue=%d", + get_ev_events(pd, x), vc->write.enabled, vc->closed, write_ready_list.in(vc)); } - } else if (!(get_ev_events(pd,x) & EVENTIO_ERROR)) { - Debug("iocore_net_main", "Unhandled epoll event: 0x%04x", get_ev_events(pd,x)); + } else if (!(get_ev_events(pd, x) & EVENTIO_ERROR)) { + Debug("iocore_net_main", "Unhandled epoll event: 0x%04x", get_ev_events(pd, x)); } } else if (epd->type == EVENTIO_DNS_CONNECTION) { if (epd->data.dnscon != NULL) { @@ -518,13 +532,13 @@ NetHandler::mainNetEvent(int event, Event *e) } else if (epd->type == EVENTIO_ASYNC_SIGNAL) { net_signal_hook_callback(trigger_event->ethread); } - ev_next_event(pd,x); + ev_next_event(pd, x); } pd->result = 0; #if defined(USE_EDGE_TRIGGER) - // UnixNetVConnection * + // UnixNetVConnection * while ((vc = read_ready_list.dequeue())) { if (vc->closed) close_UnixNetVConnection(vc, trigger_event->ethread); @@ -557,7 +571,7 @@ NetHandler::mainNetEvent(int event, Event *e) #endif } } -#else /* !USE_EDGE_TRIGGER */ +#else /* !USE_EDGE_TRIGGER */ while ((vc = read_ready_list.dequeue())) { if (vc->closed) close_UnixNetVConnection(vc, trigger_event->ethread); @@ -578,4 +592,3 @@ NetHandler::mainNetEvent(int event, Event *e) return EVENT_CONT; } - http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/iocore/net/UnixNetAccept.cc ---------------------------------------------------------------------- diff --git a/iocore/net/UnixNetAccept.cc b/iocore/net/UnixNetAccept.cc index b396084..e735ec8 100644 --- a/iocore/net/UnixNetAccept.cc +++ b/iocore/net/UnixNetAccept.cc @@ -26,9 +26,9 @@ #ifdef ROUNDUP #undef ROUNDUP #endif -#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) +#define ROUNDUP(x, y) ((((x) + ((y)-1)) / (y)) * (y)) -typedef int (NetAccept::*NetAcceptHandler) (int, void *); +typedef int (NetAccept::*NetAcceptHandler)(int, void *); volatile int dummy_volatile = 0; int accept_till_done = 1; @@ -44,7 +44,7 @@ safe_delay(int msec) // delaying to let some of the current connections complete // static int -send_throttle_message(NetAccept * na) +send_throttle_message(NetAccept *na) { struct pollfd afd; Connection con[100]; @@ -54,8 +54,7 @@ send_throttle_message(NetAccept * na) afd.events = POLLIN; int n = 0; - while (check_net_throttle(ACCEPT, ink_get_hrtime()) && n < THROTTLE_AT_ONCE - 1 - && (socketManager.poll(&afd, 1, 0) > 0)) { + while (check_net_throttle(ACCEPT, ink_get_hrtime()) && n < THROTTLE_AT_ONCE - 1 && (socketManager.poll(&afd, 1, 0) > 0)) { int res = 0; if ((res = na->server.accept(&con[n])) < 0) return res; @@ -65,8 +64,7 @@ send_throttle_message(NetAccept * na) int i = 0; for (i = 0; i < n; i++) { socketManager.read(con[i].fd, dummy_read_request, 4096); - socketManager.write(con[i].fd, unix_netProcessor.throttle_error_message, - strlen(unix_netProcessor.throttle_error_message)); + socketManager.write(con[i].fd, unix_netProcessor.throttle_error_message, strlen(unix_netProcessor.throttle_error_message)); } safe_delay(NET_THROTTLE_DELAY / 2); for (i = 0; i < n; i++) @@ -79,9 +77,9 @@ send_throttle_message(NetAccept * na) // General case network connection accept code // int -net_accept(NetAccept * na, void *ep, bool blockable) +net_accept(NetAccept *na, void *ep, bool blockable) { - Event *e = (Event *) ep; + Event *e = (Event *)ep; int res = 0; int count = 0; int loop = accept_till_done; @@ -90,10 +88,10 @@ net_accept(NetAccept * na, void *ep, bool blockable) if (!blockable) if (!MUTEX_TAKE_TRY_LOCK_FOR(na->action_->mutex, e->ethread, na->action_->continuation)) return 0; - //do-while for accepting all the connections - //added by YTS Team, yamsat + // do-while for accepting all the connections + // added by YTS Team, yamsat do { - vc = (UnixNetVConnection *) na->alloc_cache; + vc = (UnixNetVConnection *)na->alloc_cache; if (!vc) { vc = (UnixNetVConnection *)na->getNetProcessor()->allocate_vc(e->ethread); NET_SUM_GLOBAL_DYN_STAT(net_connections_currently_open_stat, 1); @@ -122,8 +120,8 @@ net_accept(NetAccept * na, void *ep, bool blockable) vc->mutex = new_ProxyMutex(); vc->action_ = *na->action_; vc->set_is_transparent(na->server.f_inbound_transparent); - vc->closed = 0; - SET_CONTINUATION_HANDLER(vc, (NetVConnHandler) & UnixNetVConnection::acceptEvent); + vc->closed = 0; + SET_CONTINUATION_HANDLER(vc, (NetVConnHandler)&UnixNetVConnection::acceptEvent); if (e->ethread->is_event_type(na->etype)) vc->handleEvent(EVENT_NONE, e); @@ -160,7 +158,7 @@ NetAccept::init_accept_loop(const char *thr_name) // use it for high connection rates as well. // void -NetAccept::init_accept(EThread * t) +NetAccept::init_accept(EThread *t) { if (!t) t = eventProcessor.assign_thread(etype); @@ -171,7 +169,7 @@ NetAccept::init_accept(EThread * t) } if (do_listen(NON_BLOCKING)) return; - SET_HANDLER((NetAcceptHandler) & NetAccept::acceptEvent); + SET_HANDLER((NetAcceptHandler)&NetAccept::acceptEvent); period = ACCEPT_PERIOD; t->schedule_every(this, period, etype); } @@ -185,9 +183,9 @@ NetAccept::init_accept_per_thread() if (do_listen(NON_BLOCKING)) return; if (accept_fn == net_accept) - SET_HANDLER((NetAcceptHandler) & NetAccept::acceptFastEvent); + SET_HANDLER((NetAcceptHandler)&NetAccept::acceptFastEvent); else - SET_HANDLER((NetAcceptHandler) & NetAccept::acceptEvent); + SET_HANDLER((NetAcceptHandler)&NetAccept::acceptEvent); period = ACCEPT_PERIOD; NetAccept *a; @@ -213,7 +211,6 @@ NetAccept::do_listen(bool non_blocking, bool transparent) if (server.fd != NO_FD) { if ((res = server.setup_fd_for_listen(non_blocking, recv_bufsize, send_bufsize, transparent))) { - Warning("unable to listen on main accept port %d: errno = %d, %s", ntohs(server.accept_addr.port()), errno, strerror(errno)); goto Lretry; } @@ -233,15 +230,15 @@ NetAccept::do_listen(bool non_blocking, bool transparent) } int -NetAccept::do_blocking_accept(EThread * t) +NetAccept::do_blocking_accept(EThread *t) { int res = 0; int loop = accept_till_done; UnixNetVConnection *vc = NULL; Connection con; - //do-while for accepting all the connections - //added by YTS Team, yamsat + // do-while for accepting all the connections + // added by YTS Team, yamsat do { ink_hrtime now = ink_get_hrtime(); @@ -260,8 +257,8 @@ NetAccept::do_blocking_accept(EThread * t) if ((res = server.accept(&con)) < 0) { Lerror: int seriousness = accept_error_seriousness(res); - if (seriousness >= 0) { // not so bad - if (!seriousness) // bad enough to warn about + if (seriousness >= 0) { // not so bad + if (!seriousness) // bad enough to warn about check_transient_accept_error(res); safe_delay(NET_THROTTLE_DELAY); return 0; @@ -297,8 +294,8 @@ NetAccept::do_blocking_accept(EThread * t) vc->set_is_transparent(server.f_inbound_transparent); vc->mutex = new_ProxyMutex(); vc->action_ = *action_; - SET_CONTINUATION_HANDLER(vc, (NetVConnHandler) & UnixNetVConnection::acceptEvent); - //eventProcessor.schedule_imm(vc, getEtype()); + SET_CONTINUATION_HANDLER(vc, (NetVConnHandler)&UnixNetVConnection::acceptEvent); + // eventProcessor.schedule_imm(vc, getEtype()); eventProcessor.schedule_imm_signal(vc, getEtype()); } while (loop); @@ -309,9 +306,9 @@ NetAccept::do_blocking_accept(EThread * t) int NetAccept::acceptEvent(int event, void *ep) { - (void) event; - Event *e = (Event *) ep; - //PollDescriptor *pd = get_PollDescriptor(e->ethread); + (void)event; + Event *e = (Event *)ep; + // PollDescriptor *pd = get_PollDescriptor(e->ethread); ProxyMutex *m = 0; if (action_->mutex) @@ -327,24 +324,22 @@ NetAccept::acceptEvent(int event, void *ep) return EVENT_DONE; } - //ink_assert(ifd < 0 || event == EVENT_INTERVAL || (pd->nfds > ifd && pd->pfd[ifd].fd == server.fd)); - //if (ifd < 0 || event == EVENT_INTERVAL || (pd->pfd[ifd].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL))) { - //ink_assert(!"incomplete"); - int res; - if ((res = accept_fn(this, e, false)) < 0) { - NET_DECREMENT_DYN_STAT(net_accepts_currently_open_stat); - /* INKqa11179 */ - Warning("Accept on port %d failed with error no %d", - ats_ip_port_host_order(&server.addr), res - ); - Warning("Traffic Server may be unable to accept more network" "connections on %d", - ats_ip_port_host_order(&server.addr) - ); - e->cancel(); - delete this; - return EVENT_DONE; - } - //} + // ink_assert(ifd < 0 || event == EVENT_INTERVAL || (pd->nfds > ifd && pd->pfd[ifd].fd == server.fd)); + // if (ifd < 0 || event == EVENT_INTERVAL || (pd->pfd[ifd].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL))) { + // ink_assert(!"incomplete"); + int res; + if ((res = accept_fn(this, e, false)) < 0) { + NET_DECREMENT_DYN_STAT(net_accepts_currently_open_stat); + /* INKqa11179 */ + Warning("Accept on port %d failed with error no %d", ats_ip_port_host_order(&server.addr), res); + Warning("Traffic Server may be unable to accept more network" + "connections on %d", + ats_ip_port_host_order(&server.addr)); + e->cancel(); + delete this; + return EVENT_DONE; + } + //} } return EVENT_CONT; } @@ -353,9 +348,9 @@ NetAccept::acceptEvent(int event, void *ep) int NetAccept::acceptFastEvent(int event, void *ep) { - Event *e = (Event *) ep; - (void) event; - (void) e; + Event *e = (Event *)ep; + (void)event; + (void)e; int bufsz, res; Connection con; @@ -395,7 +390,7 @@ NetAccept::acceptFastEvent(int event, void *ep) } } } - if (sockopt_flags & 1) { // we have to disable Nagle + if (sockopt_flags & 1) { // we have to disable Nagle safe_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, SOCKOPT_ON, sizeof(int)); Debug("socket", "::acceptFastEvent: setsockopt() TCP_NODELAY on socket"); } @@ -427,7 +422,7 @@ NetAccept::acceptFastEvent(int event, void *ep) #if defined(linux) || res == -EPIPE #endif - ) { + ) { goto Ldone; } else if (accept_error_seriousness(res) >= 0) { check_transient_accept_error(res); @@ -449,9 +444,9 @@ NetAccept::acceptFastEvent(int event, void *ep) vc->nh = get_NetHandler(e->ethread); - SET_CONTINUATION_HANDLER(vc, (NetVConnHandler) & UnixNetVConnection::mainEvent); + SET_CONTINUATION_HANDLER(vc, (NetVConnHandler)&UnixNetVConnection::mainEvent); - if (vc->ep.start(pd, vc, EVENTIO_READ|EVENTIO_WRITE) < 0) { + if (vc->ep.start(pd, vc, EVENTIO_READ | EVENTIO_WRITE) < 0) { Warning("[NetAccept::acceptFastEvent]: Error in inserting fd[%d] in kevent\n", vc->con.fd); close_UnixNetVConnection(vc, e->ethread); return EVENT_DONE; @@ -487,14 +482,14 @@ Lerror: int -NetAccept::acceptLoopEvent(int event, Event * e) +NetAccept::acceptLoopEvent(int event, Event *e) { - (void) event; - (void) e; + (void)event; + (void)e; EThread *t = this_ethread(); while (do_blocking_accept(t) >= 0) - ; + ; // Don't think this ever happens ... NET_DECREMENT_DYN_STAT(net_accepts_currently_open_stat); @@ -509,19 +504,10 @@ NetAccept::acceptLoopEvent(int event, Event * e) // NetAccept::NetAccept() - : Continuation(NULL), - period(0), - alloc_cache(0), - ifd(-1), - callback_on_open(false), - backdoor(false), - recv_bufsize(0), - send_bufsize(0), - sockopt_flags(0), - packet_mark(0), - packet_tos(0), - etype(0) -{ } + : Continuation(NULL), period(0), alloc_cache(0), ifd(-1), callback_on_open(false), backdoor(false), recv_bufsize(0), + send_bufsize(0), sockopt_flags(0), packet_mark(0), packet_tos(0), etype(0) +{ +} // @@ -547,7 +533,8 @@ NetAccept::clone() const // Virtual function allows the correct // etype to be used in NetAccept functions (ET_SSL // or ET_NET). -EventType NetAccept::getEtype() const +EventType +NetAccept::getEtype() const { return etype; } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/iocore/net/UnixNetPages.cc ---------------------------------------------------------------------- diff --git a/iocore/net/UnixNetPages.cc b/iocore/net/UnixNetPages.cc index a734fa4..4b7ff39 100644 --- a/iocore/net/UnixNetPages.cc +++ b/iocore/net/UnixNetPages.cc @@ -27,13 +27,13 @@ #include "I_Tasks.h" struct ShowNet; -typedef int (ShowNet::*ShowNetEventHandler) (int event, Event * data); -struct ShowNet: public ShowCont -{ +typedef int (ShowNet::*ShowNetEventHandler)(int event, Event *data); +struct ShowNet : public ShowCont { int ithread; IpEndpoint addr; - int showMain(int event, Event * e) + int + showMain(int event, Event *e) { CHECK_SHOW(begin("Net")); CHECK_SHOW(show("<H3>Show <A HREF=\"./connections\">Connections</A></H3>\n" @@ -44,11 +44,13 @@ struct ShowNet: public ShowCont "</form>\n" "<form method = GET action = \"./ports\">\n" "Show Connections to/from Port (e.g. 80):<br>\n" - "<input type=text name=name size=64 maxlength=256>\n" "</form>\n")); + "<input type=text name=name size=64 maxlength=256>\n" + "</form>\n")); return complete(event, e); } - int showConnectionsOnThread(int event, Event * e) + int + showConnectionsOnThread(int event, Event *e) { EThread *ethread = e->ethread; NetHandler *nh = get_NetHandler(ethread); @@ -59,58 +61,46 @@ struct ShowNet: public ShowCont } ink_hrtime now = ink_get_hrtime(); - forl_LL(UnixNetVConnection, vc, nh->open_list) { -// uint16_t port = ats_ip_port_host_order(&addr.sa); + forl_LL(UnixNetVConnection, vc, nh->open_list) + { + // uint16_t port = ats_ip_port_host_order(&addr.sa); if (ats_is_ip(&addr) && addr != vc->server_addr) continue; -// if (port && port != ats_ip_port_host_order(&vc->server_addr.sa) && port != vc->accept_port) -// continue; + // if (port && port != ats_ip_port_host_order(&vc->server_addr.sa) && port != vc->accept_port) + // continue; char ipbuf[INET6_ADDRSTRLEN]; ats_ip_ntop(&vc->server_addr.sa, ipbuf, sizeof(ipbuf)); char opt_ipbuf[INET6_ADDRSTRLEN]; char interbuf[80]; - snprintf(interbuf, sizeof(interbuf), "[%s] %s:%d", - vc->options.toString(vc->options.addr_binding), - vc->options.local_ip.toString(opt_ipbuf, sizeof(opt_ipbuf)), - vc->options.local_port - ); + snprintf(interbuf, sizeof(interbuf), "[%s] %s:%d", vc->options.toString(vc->options.addr_binding), + vc->options.local_ip.toString(opt_ipbuf, sizeof(opt_ipbuf)), vc->options.local_port); CHECK_SHOW(show("<tr>" //"<td><a href=\"/connection/%d\">%d</a></td>" - "<td>%d</td>" // ID - "<td>%s</td>" // ipbuf - "<td>%d</td>" // port - "<td>%d</td>" // fd - "<td>%s</td>" // interbuf -// "<td>%d</td>" // accept port - "<td>%d secs ago</td>" // start time - "<td>%d</td>" // thread id - "<td>%d</td>" // read enabled - "<td>%" PRId64 "</td>" // read NBytes - "<td>%" PRId64 "</td>" // read NDone - "<td>%d</td>" // write enabled - "<td>%" PRId64 "</td>" // write nbytes - "<td>%" PRId64 "</td>" // write ndone - "<td>%d secs</td>" // Inactivity timeout at - "<td>%d secs</td>" // Activity timeout at - "<td>%d</td>" // shutdown - "<td>-%s</td>" // comments + "<td>%d</td>" // ID + "<td>%s</td>" // ipbuf + "<td>%d</td>" // port + "<td>%d</td>" // fd + "<td>%s</td>" // interbuf + // "<td>%d</td>" // accept port + "<td>%d secs ago</td>" // start time + "<td>%d</td>" // thread id + "<td>%d</td>" // read enabled + "<td>%" PRId64 "</td>" // read NBytes + "<td>%" PRId64 "</td>" // read NDone + "<td>%d</td>" // write enabled + "<td>%" PRId64 "</td>" // write nbytes + "<td>%" PRId64 "</td>" // write ndone + "<td>%d secs</td>" // Inactivity timeout at + "<td>%d secs</td>" // Activity timeout at + "<td>%d</td>" // shutdown + "<td>-%s</td>" // comments "</tr>\n", - vc->id, - ipbuf, - ats_ip_port_host_order(&vc->server_addr), - vc->con.fd, - interbuf, -// vc->accept_port, - (int) ((now - vc->submit_time) / HRTIME_SECOND), - ethread->id, - vc->read.enabled, - vc->read.vio.nbytes, - vc->read.vio.ndone, - vc->write.enabled, - vc->write.vio.nbytes, - vc->write.vio.ndone, - (int) (vc->inactivity_timeout_in / HRTIME_SECOND), - (int) (vc->active_timeout_in / HRTIME_SECOND), vc->f.shutdown, vc->closed ? "closed " : "")); + vc->id, ipbuf, ats_ip_port_host_order(&vc->server_addr), vc->con.fd, interbuf, + // vc->accept_port, + (int)((now - vc->submit_time) / HRTIME_SECOND), ethread->id, vc->read.enabled, vc->read.vio.nbytes, + vc->read.vio.ndone, vc->write.enabled, vc->write.vio.nbytes, vc->write.vio.ndone, + (int)(vc->inactivity_timeout_in / HRTIME_SECOND), (int)(vc->active_timeout_in / HRTIME_SECOND), + vc->f.shutdown, vc->closed ? "closed " : "")); } ithread++; if (ithread < eventProcessor.n_threads_for_type[ET_NET]) @@ -122,7 +112,8 @@ struct ShowNet: public ShowCont return EVENT_CONT; } - int showConnections(int event, Event * e) + int + showConnections(int event, Event *e) { CHECK_SHOW(begin("Net Connections")); CHECK_SHOW(show("<H3>Connections</H3>\n" @@ -151,7 +142,8 @@ struct ShowNet: public ShowCont return EVENT_CONT; } - int showSingleThread(int event, Event * e) + int + showSingleThread(int event, Event *e) { EThread *ethread = e->ethread; NetHandler *nh = get_NetHandler(ethread); @@ -165,15 +157,13 @@ struct ShowNet: public ShowCont CHECK_SHOW(show("<H3>Thread: %d</H3>\n", ithread)); CHECK_SHOW(show("<table border=1>\n")); int connections = 0; - forl_LL(UnixNetVConnection, vc, nh->open_list) - connections++; + forl_LL(UnixNetVConnection, vc, nh->open_list) connections++; CHECK_SHOW(show("<tr><td>%s</td><td>%d</td></tr>\n", "Connections", connections)); - //CHECK_SHOW(show("<tr><td>%s</td><td>%d</td></tr>\n", "Last Poll Size", pollDescriptor->nfds)); + // CHECK_SHOW(show("<tr><td>%s</td><td>%d</td></tr>\n", "Last Poll Size", pollDescriptor->nfds)); CHECK_SHOW(show("<tr><td>%s</td><td>%d</td></tr>\n", "Last Poll Ready", pollDescriptor->result)); CHECK_SHOW(show("</table>\n")); CHECK_SHOW(show("<table border=1>\n")); - CHECK_SHOW(show - ("<tr><th>#</th><th>Read Priority</th><th>Read Bucket</th><th>Write Priority</th><th>Write Bucket</th></tr>\n")); + CHECK_SHOW(show("<tr><th>#</th><th>Read Priority</th><th>Read Bucket</th><th>Write Priority</th><th>Write Bucket</th></tr>\n")); CHECK_SHOW(show("</table>\n")); ithread++; if (ithread < eventProcessor.n_threads_for_type[ET_NET]) @@ -183,35 +173,38 @@ struct ShowNet: public ShowCont return EVENT_CONT; } - int showThreads(int event, Event * e) + int + showThreads(int event, Event *e) { CHECK_SHOW(begin("Net Threads")); SET_HANDLER(&ShowNet::showSingleThread); eventProcessor.eventthread[ET_NET][0]->schedule_imm(this); // This can not use ET_TASK return EVENT_CONT; } - int showSingleConnection(int event, Event * e) + int + showSingleConnection(int event, Event *e) { CHECK_SHOW(begin("Net Connection")); return complete(event, e); } - int showHostnames(int event, Event * e) + int + showHostnames(int event, Event *e) { CHECK_SHOW(begin("Net Connections to/from Host")); return complete(event, e); } -ShowNet(Continuation * c, HTTPHdr * h): - ShowCont(c, h), ithread(0) { + ShowNet(Continuation *c, HTTPHdr *h) : ShowCont(c, h), ithread(0) + { memset(&addr, 0, sizeof(addr)); SET_HANDLER(&ShowNet::showMain); } }; #undef STREQ_PREFIX -#define STREQ_PREFIX(_x,_n,_s) (!ptr_len_ncasecmp(_x,_n,_s,sizeof(_s)-1)) +#define STREQ_PREFIX(_x, _n, _s) (!ptr_len_ncasecmp(_x, _n, _s, sizeof(_s) - 1)) Action * -register_ShowNet(Continuation * c, HTTPHdr * h) +register_ShowNet(Continuation *c, HTTPHdr *h) { ShowNet *s = new ShowNet(c, h); int path_len; @@ -240,10 +233,9 @@ register_ShowNet(Continuation * c, HTTPHdr * h) if (s->sarg) gn = (char *)memchr(s->sarg, '=', strlen(s->sarg)); if (gn) - ats_ip_port_cast(&s->addr.sa) = htons(atoi(gn+1)); + ats_ip_port_cast(&s->addr.sa) = htons(atoi(gn + 1)); SET_CONTINUATION_HANDLER(s, &ShowNet::showConnections); } eventProcessor.schedule_imm(s, ET_TASK); return &s->action; } - http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/iocore/net/UnixNetProcessor.cc ---------------------------------------------------------------------- diff --git a/iocore/net/UnixNetProcessor.cc b/iocore/net/UnixNetProcessor.cc index a07aed8..fdc58ab 100644 --- a/iocore/net/UnixNetProcessor.cc +++ b/iocore/net/UnixNetProcessor.cc @@ -28,7 +28,7 @@ NetProcessor::AcceptOptions const NetProcessor::DEFAULT_ACCEPT_OPTIONS; -NetProcessor::AcceptOptions& +NetProcessor::AcceptOptions & NetProcessor::AcceptOptions::reset() { local_port = 0; @@ -57,38 +57,37 @@ net_next_connection_number() { unsigned int res = 0; do { - res = (unsigned int) - ink_atomic_increment(&net_connection_number, 1); + res = (unsigned int)ink_atomic_increment(&net_connection_number, 1); } while (!res); return res; } Action * -NetProcessor::accept(Continuation* cont, AcceptOptions const& opt) +NetProcessor::accept(Continuation *cont, AcceptOptions const &opt) { - Debug("iocore_net_processor", "NetProcessor::accept - port %d,recv_bufsize %d, send_bufsize %d, sockopt 0x%0x", - opt.local_port, opt.recv_bufsize, opt.send_bufsize, opt.sockopt_flags); + Debug("iocore_net_processor", "NetProcessor::accept - port %d,recv_bufsize %d, send_bufsize %d, sockopt 0x%0x", opt.local_port, + opt.recv_bufsize, opt.send_bufsize, opt.sockopt_flags); - return ((UnixNetProcessor *) this)->accept_internal(cont, NO_FD, opt); + return ((UnixNetProcessor *)this)->accept_internal(cont, NO_FD, opt); } Action * -NetProcessor::main_accept(Continuation *cont, SOCKET fd, AcceptOptions const& opt) +NetProcessor::main_accept(Continuation *cont, SOCKET fd, AcceptOptions const &opt) { - UnixNetProcessor* this_unp = static_cast<UnixNetProcessor*>(this); + UnixNetProcessor *this_unp = static_cast<UnixNetProcessor *>(this); Debug("iocore_net_processor", "NetProcessor::main_accept - port %d,recv_bufsize %d, send_bufsize %d, sockopt 0x%0x", opt.local_port, opt.recv_bufsize, opt.send_bufsize, opt.sockopt_flags); return this_unp->accept_internal(cont, fd, opt); } Action * -UnixNetProcessor::accept_internal(Continuation *cont, int fd, AcceptOptions const& opt) +UnixNetProcessor::accept_internal(Continuation *cont, int fd, AcceptOptions const &opt) { EventType upgraded_etype = opt.etype; // setEtype requires non-const ref. EThread *thread = this_ethread(); ProxyMutex *mutex = thread->mutex; int accept_threads = opt.accept_threads; // might be changed. - IpEndpoint accept_ip; // local binding address. + IpEndpoint accept_ip; // local binding address. char thr_name[MAX_THREAD_NAME_LENGTH]; NetAccept *na = createNetAccept(); @@ -120,7 +119,7 @@ UnixNetProcessor::accept_internal(Continuation *cont, int fd, AcceptOptions cons ats_ip_copy(&na->server.accept_addr, &accept_ip); na->server.f_inbound_transparent = opt.f_inbound_transparent; if (opt.f_inbound_transparent) { - Debug( "http_tproxy", "Marking accept server %p on port %d as inbound transparent", na, opt.local_port); + Debug("http_tproxy", "Marking accept server %p on port %d as inbound transparent", na, opt.local_port); } int should_filter_int = 0; @@ -143,20 +142,19 @@ UnixNetProcessor::accept_internal(Continuation *cont, int fd, AcceptOptions cons if (na->callback_on_open) na->mutex = cont->mutex; if (opt.frequent_accept) { // true - if (accept_threads > 0) { + if (accept_threads > 0) { if (0 == na->do_listen(BLOCKING, opt.f_inbound_transparent)) { + for (int i = 1; i < accept_threads; ++i) { + NetAccept *a = na->clone(); - for (int i=1; i < accept_threads; ++i) { - NetAccept * a = na->clone(); - - snprintf(thr_name, MAX_THREAD_NAME_LENGTH, "[ACCEPT %d:%d]", i-1, ats_ip_port_host_order(&accept_ip)); + snprintf(thr_name, MAX_THREAD_NAME_LENGTH, "[ACCEPT %d:%d]", i - 1, ats_ip_port_host_order(&accept_ip)); a->init_accept_loop(thr_name); Debug("iocore_net_accept", "Created accept thread #%d for port %d", i, ats_ip_port_host_order(&accept_ip)); } // Start the "template" accept thread last. Debug("iocore_net_accept", "Created accept thread #%d for port %d", accept_threads, ats_ip_port_host_order(&accept_ip)); - snprintf(thr_name, MAX_THREAD_NAME_LENGTH, "[ACCEPT %d:%d]", accept_threads-1, ats_ip_port_host_order(&accept_ip)); + snprintf(thr_name, MAX_THREAD_NAME_LENGTH, "[ACCEPT %d:%d]", accept_threads - 1, ats_ip_port_host_order(&accept_ip)); na->init_accept_loop(thr_name); } } else { @@ -174,24 +172,21 @@ UnixNetProcessor::accept_internal(Continuation *cont, int fd, AcceptOptions cons } #endif #ifdef TCP_INIT_CWND - int tcp_init_cwnd = 0; - REC_ReadConfigInteger(tcp_init_cwnd, "proxy.config.http.server_tcp_init_cwnd"); - if(tcp_init_cwnd > 0) { + int tcp_init_cwnd = 0; + REC_ReadConfigInteger(tcp_init_cwnd, "proxy.config.http.server_tcp_init_cwnd"); + if (tcp_init_cwnd > 0) { Debug("net", "Setting initial congestion window to %d", tcp_init_cwnd); - if(setsockopt(na->server.fd, IPPROTO_TCP, TCP_INIT_CWND, &tcp_init_cwnd, sizeof(int)) != 0) { + if (setsockopt(na->server.fd, IPPROTO_TCP, TCP_INIT_CWND, &tcp_init_cwnd, sizeof(int)) != 0) { Error("Cannot set initial congestion window to %d", tcp_init_cwnd); } - } + } #endif return na->action_; } Action * -UnixNetProcessor::connect_re_internal( - Continuation * cont, - sockaddr const* target, - NetVCOptions * opt -) { +UnixNetProcessor::connect_re_internal(Continuation *cont, sockaddr const *target, NetVCOptions *opt) +{ ProxyMutex *mutex = cont->mutex; EThread *t = mutex->thread_holding; UnixNetVConnection *vc = (UnixNetVConnection *)this->allocate_vc(t); @@ -213,7 +208,7 @@ UnixNetProcessor::connect_re_internal( */ !socks_conf_stuff->ip_map.contains(target)) #endif - ); + ); SocksEntry *socksEntry = NULL; NET_SUM_GLOBAL_DYN_STAT(net_connections_currently_open_stat, 1); @@ -228,7 +223,7 @@ UnixNetProcessor::connect_re_internal( char buff[INET6_ADDRPORTSTRLEN]; Debug("Socks", "Using Socks ip: %s\n", ats_ip_nptop(target, buff, sizeof(buff))); socksEntry = socksAllocator.alloc(); - socksEntry->init(cont->mutex, vc, opt->socks_support, opt->socks_version); /*XXXX remove last two args */ + socksEntry->init(cont->mutex, vc, opt->socks_support, opt->socks_version); /*XXXX remove last two args */ socksEntry->action_ = cont; cont = socksEntry; if (!ats_is_ip(&socksEntry->server_addr)) { @@ -266,14 +261,12 @@ UnixNetProcessor::connect_re_internal( } Action * -UnixNetProcessor::connect(Continuation * cont, UnixNetVConnection ** /* avc */, sockaddr const* target, - NetVCOptions * opt) +UnixNetProcessor::connect(Continuation *cont, UnixNetVConnection ** /* avc */, sockaddr const *target, NetVCOptions *opt) { return connect_re(cont, target, opt); } -struct CheckConnect:public Continuation -{ +struct CheckConnect : public Continuation { UnixNetVConnection *vc; Action action_; MIOBuffer *buf; @@ -282,12 +275,13 @@ struct CheckConnect:public Continuation int recursion; ink_hrtime timeout; - int handle_connect(int event, Event * e) + int + handle_connect(int event, Event *e) { connect_status = event; switch (event) { case NET_EVENT_OPEN: - vc = (UnixNetVConnection *) e; + vc = (UnixNetVConnection *)e; Debug("iocore_net_connect", "connect Net open"); vc->do_io_write(this, 10, /* some non-zero number just to get the poll going */ reader); @@ -296,25 +290,24 @@ struct CheckConnect:public Continuation return EVENT_CONT; break; - case NET_EVENT_OPEN_FAILED: - Debug("iocore_net_connect", "connect Net open failed"); + case NET_EVENT_OPEN_FAILED: + Debug("iocore_net_connect", "connect Net open failed"); if (!action_.cancelled) - action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void *) e); + action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void *)e); break; - case VC_EVENT_WRITE_READY:int sl, ret; + case VC_EVENT_WRITE_READY: + int sl, ret; socklen_t sz; - if (!action_.cancelled) - { + if (!action_.cancelled) { sz = sizeof(int); - ret = getsockopt(vc->con.fd, SOL_SOCKET, SO_ERROR, (char *) &sl, &sz); - if (!ret && sl == 0) - { + ret = getsockopt(vc->con.fd, SOL_SOCKET, SO_ERROR, (char *)&sl, &sz); + if (!ret && sl == 0) { Debug("iocore_net_connect", "connection established"); /* disable write on vc */ vc->write.enabled = 0; vc->cancel_inactivity_timeout(); - //write_disable(get_NetHandler(this_ethread()), vc); + // write_disable(get_NetHandler(this_ethread()), vc); /* clean up vc fields */ vc->write.vio.nbytes = 0; vc->write.vio.op = VIO::NONE; @@ -323,32 +316,31 @@ struct CheckConnect:public Continuation action_.continuation->handleEvent(NET_EVENT_OPEN, vc); delete this; - return EVENT_DONE; + return EVENT_DONE; } } vc->do_io_close(); if (!action_.cancelled) - action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void *) -ENET_CONNECT_FAILED); + action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void *)-ENET_CONNECT_FAILED); break; case VC_EVENT_INACTIVITY_TIMEOUT: Debug("iocore_net_connect", "connect timed out"); vc->do_io_close(); if (!action_.cancelled) - action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void *) -ENET_CONNECT_TIMEOUT); + action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void *)-ENET_CONNECT_TIMEOUT); break; default: ink_assert(!"unknown connect event"); if (!action_.cancelled) - action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void *) -ENET_CONNECT_FAILED); - + action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void *)-ENET_CONNECT_FAILED); } if (!recursion) delete this; return EVENT_DONE; } - Action *connect_s(Continuation * cont, sockaddr const* target, - int _timeout, NetVCOptions * opt) + Action * + connect_s(Continuation *cont, sockaddr const *target, int _timeout, NetVCOptions *opt) { action_ = cont; timeout = HRTIME_SECONDS(_timeout); @@ -363,13 +355,15 @@ struct CheckConnect:public Continuation } } - CheckConnect(ProxyMutex * m = NULL):Continuation(m), connect_status(-1), recursion(0), timeout(0) { + CheckConnect(ProxyMutex *m = NULL) : Continuation(m), connect_status(-1), recursion(0), timeout(0) + { SET_HANDLER(&CheckConnect::handle_connect); buf = new_empty_MIOBuffer(1); reader = buf->alloc_reader(); } - ~CheckConnect() { + ~CheckConnect() + { buf->dealloc_all_readers(); buf->clear(); free_MIOBuffer(buf); @@ -377,8 +371,7 @@ struct CheckConnect:public Continuation }; Action * -NetProcessor::connect_s(Continuation * cont, sockaddr const* target, - int timeout, NetVCOptions * opt) +NetProcessor::connect_s(Continuation *cont, sockaddr const *target, int timeout, NetVCOptions *opt) { Debug("iocore_net_connect", "NetProcessor::connect_s called"); CheckConnect *c = new CheckConnect(cont->mutex); @@ -386,7 +379,6 @@ NetProcessor::connect_s(Continuation * cont, sockaddr const* target, } - struct PollCont; // This is a little odd, in that the actual threads are created before calling the processor. @@ -406,7 +398,7 @@ UnixNetProcessor::start(int, size_t) netthreads = eventProcessor.eventthread[etype]; for (int i = 0; i < n_netthreads; ++i) { initialize_thread_for_net(netthreads[i]); - extern void initialize_thread_for_http_sessions(EThread *thread, int thread_index); + extern void initialize_thread_for_http_sessions(EThread * thread, int thread_index); initialize_thread_for_http_sessions(netthreads[i], i); } @@ -419,7 +411,8 @@ UnixNetProcessor::start(int, size_t) socks_conf_stuff = new socks_conf_struct; loadSocksConfiguration(socks_conf_stuff); if (!socks_conf_stuff->socks_needed && socks_conf_stuff->accept_enabled) { - Warning("We can not have accept_enabled and socks_needed turned off" " disabling Socks accept\n"); + Warning("We can not have accept_enabled and socks_needed turned off" + " disabling Socks accept\n"); socks_conf_stuff->accept_enabled = 0; } else { // this is sslNetprocessor @@ -436,9 +429,9 @@ UnixNetProcessor::start(int, size_t) } */ -/* - * Stat pages - */ + /* + * Stat pages + */ extern Action *register_ShowNet(Continuation * c, HTTPHdr * h); if (etype == ET_NET) statPagesManager.register_http("net", register_ShowNet); @@ -469,9 +462,8 @@ UnixNetProcessor::allocate_vc(EThread *t) return vc; } -struct socks_conf_struct * -NetProcessor::socks_conf_stuff = NULL; +struct socks_conf_struct *NetProcessor::socks_conf_stuff = NULL; int NetProcessor::accept_mss = 0; UnixNetProcessor unix_netProcessor; -NetProcessor & netProcessor = unix_netProcessor; +NetProcessor &netProcessor = unix_netProcessor;
