From 173d8f740e5e1f1bc14a39dad852f07339cdbfdc Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Thu, 26 Apr 2018 15:22:44 +0200 Subject: [PATCH] esrf changes: slsReceiver: better checking of socket buffer pars. and warn on failures * not done. The 'setsockopt(SO_RECVBUF)' system call cannot set the socket buffer size lager than the specified in net.core.rmem_max. The requested value was 2 GB (commit 3b0e2e6), which is far too large for this application, so it was restored to the acceptable 100 MB value. * The syscall does not fail if the requested buffer size is larger than net.core.rmem_max. Use 'setsockopt(SO_RECVBUFFORCE)' to actually force a value larger than the system limit, which can be done if run in a privileged context (capability CAP_NET_ADMIN set). * The real value is read with 'getsockopt(SO_RECVBUF)'. If it corresponds to twice the requested value (see 'man 7 socket'), it is printed in green, otherwise it is signalled in red. * The 'setsockopt(SO_RECVBUFFORCE)' syscall removes the need to write to /proc/sys/net/core/rmem_max, so this was was suppressed in the 'UDPStandardImplementation' constructor. * The test on EIGER detectors before setting the system buffers was removed. Was there for 9m/2m eiger, but one can take care of memory requirements using a customizable max socket buffer size(only with permissions). to be implmented later. * The file /proc/sys/net/core/netdev_max_backlog is first read by the receiver to check is the current value is OK. If it is not, the receiver directly writes the good value into the file (instead of delegating to the system shell), printing a red error message if there is an access error (non-privileged user). --- .../slsDetector/slsDetector.cpp | 2 +- slsReceiverSoftware/include/genericSocket.h | 68 +++++++++++++------ .../src/UDPStandardImplementation.cpp | 37 +++++----- 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index e6a6875c0..85906539c 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -4392,7 +4392,7 @@ slsDetectorDefs::runStatus slsDetector::getRunStatus(){ int fnum=F_GET_RUN_STATUS; int ret=FAIL; char mess[MAX_STR_LENGTH]=""; - strcpy(mess,"aaaaa"); + strcpy(mess,"could not get run status"); runStatus retval=ERROR; #ifdef VERBOSE std::cout<< "Getting status "<< std::endl; diff --git a/slsReceiverSoftware/include/genericSocket.h b/slsReceiverSoftware/include/genericSocket.h index 8ca367989..b08e5e4e6 100644 --- a/slsReceiverSoftware/include/genericSocket.h +++ b/slsReceiverSoftware/include/genericSocket.h @@ -62,8 +62,7 @@ class sockaddr_in; using namespace std; #define DEFAULT_PACKET_SIZE 1286 -/*#define SOCKET_BUFFER_SIZE (100*1024*1024) //100MB*/ -#define SOCKET_BUFFER_SIZE (2000*1024*1024) //100MB +#define SOCKET_BUFFER_SIZE (2000*1024*1024) //2GB, previously 100MB #define DEFAULT_BACKLOG 5 @@ -81,7 +80,8 @@ enum communicationProtocol{ - genericSocket(const char* const host_ip_or_name, unsigned short int const port_number, communicationProtocol p, int ps = DEFAULT_PACKET_SIZE) : + genericSocket(const char* const host_ip_or_name, unsigned short int const port_number, + communicationProtocol p, int ps = DEFAULT_PACKET_SIZE) : portno(port_number), protocol(p), is_a_server(0), @@ -121,7 +121,7 @@ enum communicationProtocol{ return SOCK_DGRAM; default: - cerr << "unknown protocol " << p << endl; + cprintf(RED, "unknown protocol %d\n", p); return -1; } } @@ -140,7 +140,9 @@ enum communicationProtocol{ */ - genericSocket(unsigned short int const port_number, communicationProtocol p, int ps = DEFAULT_PACKET_SIZE, const char *eth=NULL, int hsize=0): + genericSocket(unsigned short int const port_number, communicationProtocol p, + int ps = DEFAULT_PACKET_SIZE, const char *eth=NULL, int hsize=0, + int buf_size=SOCKET_BUFFER_SIZE): portno(port_number), protocol(p), is_a_server(1), @@ -188,7 +190,7 @@ enum communicationProtocol{ socketDescriptor = socket(AF_INET, getProtocol(),0); //tcp if (socketDescriptor < 0) { - cerr << "Can not create socket "<0) { if ((file_des = accept(socketDescriptor,(struct sockaddr *) &clientAddress, &clientAddress_length)) < 0) { - cerr << "Error: with server accept, connection refused"< /proc/sys/net/core/rmem_max",RECEIVE_SOCKET_BUFFER_SIZE); - if (system(command)) { - FILE_LOG(logWARNING) << "No root privileges to change Socket Receiver Buffer size (net.core.rmem_max)"; - return; - } - FILE_LOG(logINFO) << "Socket Receiver Buffer size (/proc/sys/net/core/rmem_max) modified to " << RECEIVE_SOCKET_BUFFER_SIZE ; - - // to increase Max length of input packet queue - sprintf(command,"echo %d > /proc/sys/net/core/netdev_max_backlog",MAX_SOCKET_INPUT_PACKET_QUEUE); - if (system(command)) { - FILE_LOG(logWARNING) << "No root privileges to change Max length of input packet queue (net.core.rmem_max)"; - return; + int max_back_log; + const char *proc_file_name = "/proc/sys/net/core/netdev_max_backlog"; + { + ifstream proc_file(proc_file_name); + proc_file >> max_back_log; + } + + if (max_back_log < MAX_SOCKET_INPUT_PACKET_QUEUE) { + ofstream proc_file(proc_file_name); + if (proc_file.good()) { + proc_file << MAX_SOCKET_INPUT_PACKET_QUEUE << endl; + cprintf(GREEN, "Max length of input packet queue " + "(/proc/sys/net/core/netdev_max_backlog) modified to %d\n", + MAX_SOCKET_INPUT_PACKET_QUEUE); + } else { + const char *msg = "Could not change max length of" + "input packet queue (net.core.netdev_max_backlog): no root privileges?"; + cprintf(RED, "WARNING: %s\n", msg); + } } - FILE_LOG(logINFO) << "Max length of input packet queue (/proc/sys/net/core/netdev_max_backlog) modified to " << MAX_SOCKET_INPUT_PACKET_QUEUE ; }