22 #include <sys/resource.h>
26 #include <metaproxy/util.hpp>
28 #include <metaproxy/filter.hpp>
29 #include <metaproxy/package.hpp>
32 #include <yazpp/z-assoc.h>
33 #include <yazpp/pdu-assoc.h>
34 #include <yazpp/socket-manager.h>
35 #include <yazpp/limit-connect.h>
36 #include <yaz/timing.h>
38 #include <yaz/daemon.h>
39 #include <yaz/malloc_info.h>
54 size_t get(
const std::string &peer);
55 size_t add(
const std::string &peer);
56 size_t remove(
const std::string &peer);
96 yazpp_1::PDU_Assoc **
pdu;
121 ZAssocChild(yazpp_1::IPDU_Observable *the_PDU_Observable,
123 const mp::Package *package,
126 yazpp_1::LimitConnect &limit,
127 const char *peername);
132 yazpp_1::IPDU_Observable *the_PDU_Observable,
134 void recv_GDU(Z_GDU *apdu,
int len);
135 void report(Z_HTTP_Request *hreq);
152 yf::FrontendNet::ZAssocChild *ses,
156 void result(
const char *t_info);
174 yazpp_1::IPDU_Observable *the_PDU_Observable,
176 void recv_GDU(Z_GDU *apdu,
int len);
192 yf::FrontendNet::ThreadPoolPackage::ThreadPoolPackage(mp::Package *package,
195 m_assoc_child(ses), m_package(package), m_p(rep)
197 timer = yaz_timing_create();
200 yf::FrontendNet::ThreadPoolPackage::~ThreadPoolPackage()
202 yaz_timing_destroy(&timer);
206 bool yf::FrontendNet::ThreadPoolPackage::cleanup(
void *info)
208 mp::Session *ses = (mp::Session *) info;
209 if (*ses == m_package->session())
211 m_assoc_child->m_no_requests--;
217 void yf::FrontendNet::ThreadPoolPackage::result(
const char *t_info)
219 m_assoc_child->m_no_requests--;
221 yazpp_1::GDU *gdu = &m_package->response();
226 m_assoc_child->send_GDU(gdu->get(), &len);
228 yaz_timing_stop(timer);
229 double duration = yaz_timing_get_real(timer);
232 while (
m_p->m_duration_lim[ent] != 0.0 && duration >
m_p->m_duration_lim[ent])
234 m_p->m_duration_freq[ent]++;
236 m_p->m_duration_total += duration;
238 if (
m_p->m_duration_max < duration)
239 m_p->m_duration_max = duration;
241 if (
m_p->m_duration_min == 0.0 ||
m_p->m_duration_min > duration)
242 m_p->m_duration_min = duration;
244 if (
m_p->m_msg_config.length())
246 Z_GDU *z_gdu = gdu->get();
248 std::ostringstream os;
249 os <<
m_p->m_msg_config <<
" "
251 << std::fixed << std::setprecision (6) << duration <<
" ";
258 yaz_log(YLOG_LOG,
"%s %s", os.str().c_str(), t_info);
261 else if (!m_package->session().is_closed())
265 yazpp_1::GDU *gdu_req = &m_package->request();
266 Z_GDU *z_gdu = gdu_req->get();
267 if (z_gdu && z_gdu->which == Z_GDU_Z3950)
272 Z_APDU *apdu_response = odr.create_close(
273 z_gdu->u.z3950, Z_Close_systemProblem,
274 "unhandled Z39.50 request");
276 m_assoc_child->send_Z_PDU(apdu_response, &len);
278 else if (z_gdu && z_gdu->which == Z_GDU_HTTP_Request)
284 = odr.create_HTTP_Response(m_package->session(),
285 z_gdu->u.HTTP_Request, 500);
286 m_assoc_child->send_GDU(zgdu_res, &len);
288 m_package->session().close();
291 if (m_assoc_child->m_no_requests == 0 && m_package->session().is_closed())
293 m_assoc_child->close();
302 m_package->move(m_assoc_child->m_port->route);
306 yf::FrontendNet::ZAssocChild::ZAssocChild(
307 yazpp_1::IPDU_Observable *PDU_Observable,
309 const mp::Package *package,
311 yazpp_1::LimitConnect &limit_http_req,
312 const char *peername)
313 : Z_Assoc(PDU_Observable),
m_p(rep), m_limit_http_req(limit_http_req)
322 addr.append(peername);
324 addr.append(port->
port);
327 int session_timeout = 300;
331 if (mp::util::match_ip(it->pattern, peername))
334 yaz_log(YLOG_LOG,
"timeout pattern=%s ip=%s value=%d",
336 peername, it->value);
337 session_timeout = it->value;
341 timeout(session_timeout);
344 yazpp_1::IPDU_Observer *yf::FrontendNet::ZAssocChild::sessionNotify(
345 yazpp_1::IPDU_Observable *the_PDU_Observable,
int fd)
350 yf::FrontendNet::ZAssocChild::~ZAssocChild()
352 int d =
m_p->m_peerStat.remove(m_peer);
353 if (
m_p->m_msg_config.length())
355 std::ostringstream os;
356 os <<
m_p->m_msg_config <<
" "
357 << m_peer <<
" closing cnt=" << d;
358 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
362 void yf::FrontendNet::ZAssocChild::report(Z_HTTP_Request *hreq)
366 Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 200);
368 Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
372 int number_total = 0;
374 for (i = 0;
m_p->m_duration_lim[i] != 0.0; i++)
375 number_total +=
m_p->m_duration_freq[i];
376 number_total +=
m_p->m_duration_freq[i];
378 wrbuf_puts(w,
"<?xml version=\"1.0\"?>\n");
379 wrbuf_puts(w,
"<frontend_net>\n");
380 wrbuf_printf(w,
" <responses frequency=\"%d\">\n", number_total);
381 for (i = 0;
m_p->m_duration_lim[i] != 0.0; i++)
383 if (
m_p->m_duration_freq[i] > 0)
385 w,
" <response duration_start=\"%f\" "
386 "duration_end=\"%f\" frequency=\"%d\"/>\n",
387 i > 0 ?
m_p->m_duration_lim[i - 1] : 0.0,
388 m_p->m_duration_lim[i],
m_p->m_duration_freq[i]);
391 if (
m_p->m_duration_freq[i] > 0)
393 w,
" <response duration_start=\"%f\" frequency=\"%d\"/>\n",
394 m_p->m_duration_lim[i - 1],
m_p->m_duration_freq[i]);
396 if (
m_p->m_duration_max != 0.0)
398 w,
" <response duration_max=\"%f\"/>\n",
399 m_p->m_duration_max);
400 if (
m_p->m_duration_min != 0.0)
402 w,
" <response duration_min=\"%f\"/>\n",
403 m_p->m_duration_min);
404 if (
m_p->m_duration_total != 0.0)
406 w,
" <response duration_average=\"%f\"/>\n",
407 m_p->m_duration_total / number_total);
409 wrbuf_puts(w,
" </responses>\n");
413 m_thread_pool_observer->get_thread_info(thread_busy, thread_total);
415 wrbuf_printf(w,
" <thread_info busy=\"%d\" total=\"%d\"/>\n",
416 thread_busy, thread_total);
418 wrbuf_malloc_info(w);
422 if (nmem_get_status(buf,
sizeof(buf) - 1) == 0)
425 wrbuf_puts(w,
"</frontend_net>\n");
427 hres->content_len = w.len();
428 hres->content_buf = (
char *) w.buf();
431 send_GDU(gdu_res, &len);
434 void yf::FrontendNet::ZAssocChild::recv_GDU(Z_GDU *z_pdu,
int len)
438 mp::Package *p =
new mp::Package(m_session, m_origin);
440 if (z_pdu && z_pdu->which == Z_GDU_HTTP_Request)
442 Z_HTTP_Request *hreq = z_pdu->u.HTTP_Request;
444 const char *f = z_HTTP_header_lookup(hreq->headers,
"X-Forwarded-For");
446 p->origin().set_tcpip_address(std::string(f), m_session.id());
448 if (
m_p->m_stat_req.length()
449 && !strcmp(hreq->path,
m_p->m_stat_req.c_str()))
458 p->copy_route(*m_package);
459 p->request() = yazpp_1::GDU(z_pdu);
461 if (
m_p->m_msg_config.length())
465 std::ostringstream os;
466 os <<
m_p->m_msg_config <<
" "
470 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
473 if (z_pdu && z_pdu->which == Z_GDU_HTTP_Request)
475 Z_HTTP_Request *hreq = z_pdu->u.HTTP_Request;
476 std::string peername = p->origin().get_address();
478 m_limit_http_req.cleanup(
false);
479 int con_sz = m_limit_http_req.get_total(peername.c_str());
480 std::list<IP_Pattern>::const_iterator it =
m_p->http_req_max.begin();
481 for (; it !=
m_p->http_req_max.end(); it++)
483 if (mp::util::match_ip(it->pattern, peername))
485 if (it->verbose > 1 ||
486 (it->value && con_sz >= it->value && it->verbose > 0))
487 yaz_log(YLOG_LOG,
"http-req-max pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername.c_str(), con_sz, it->value);
488 if (con_sz < it->value)
491 Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 500);
493 send_GDU(gdu_res, &len);
499 m_limit_http_req.add_connect(peername.c_str());
502 m_thread_pool_observer->put(tp);
505 void yf::FrontendNet::ZAssocChild::failNotify()
508 if (m_session.is_closed())
510 if (m_no_requests == 0)
518 mp::Package *p =
new mp::Package(m_session, m_origin);
521 p->copy_route(*m_package);
522 m_thread_pool_observer->cleanup(tp, &m_session);
523 m_thread_pool_observer->put(tp);
526 void yf::FrontendNet::ZAssocChild::timeoutNotify()
531 void yf::FrontendNet::ZAssocChild::connectNotify()
536 yf::FrontendNet::ZAssocServer::ZAssocServer(
537 yazpp_1::IPDU_Observable *PDU_Observable,
541 Z_Assoc(PDU_Observable), m_port(port),
m_p(rep)
547 void yf::FrontendNet::ZAssocServer::set_package(
const mp::Package *package)
552 void yf::FrontendNet::ZAssocServer::set_thread_pool(
555 m_thread_pool_observer = observer;
558 yazpp_1::IPDU_Observer *yf::FrontendNet::ZAssocServer::sessionNotify(
559 yazpp_1::IPDU_Observable *the_PDU_Observable,
int fd)
562 const char *peername = the_PDU_Observable->getpeername();
564 peername =
"unknown";
567 const char *cp = strchr(peername,
':');
573 int total_sz =
m_p->m_peerStat.get(peername);
574 std::list<IP_Pattern>::const_iterator it =
m_p->connect_total.begin();
575 for (; it !=
m_p->connect_total.end(); it++)
577 if (mp::util::match_ip(it->pattern, peername))
579 if (it->verbose > 1 ||
580 (it->value && total_sz >= it->value && it->verbose > 0))
581 yaz_log(YLOG_LOG,
"connect-total pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername, total_sz, it->value);
582 if (total_sz < it->value)
587 limit_connect.cleanup(
false);
588 int con_sz = limit_connect.get_total(peername);
589 it =
m_p->connect_max.begin();
590 for (; it !=
m_p->connect_max.end(); it++)
592 if (mp::util::match_ip(it->pattern, peername))
594 if (it->verbose > 1 ||
595 (it->value && con_sz >= it->value && it->verbose > 0))
596 yaz_log(YLOG_LOG,
"connect-max pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername, con_sz, it->value);
597 if (con_sz < it->value)
602 limit_connect.add_connect(peername);
605 m_thread_pool_observer,
606 m_package, m_port,
m_p, limit_http_req,
611 yf::FrontendNet::ZAssocServer::~ZAssocServer()
615 void yf::FrontendNet::ZAssocServer::recv_GDU(Z_GDU *apdu,
int len)
619 void yf::FrontendNet::ZAssocServer::failNotify()
623 void yf::FrontendNet::ZAssocServer::timeoutNotify()
627 void yf::FrontendNet::ZAssocServer::connectNotify()
631 yf::FrontendNet::FrontendNet() :
m_p(new
Rep)
635 yf::FrontendNet::Rep::Rep()
637 m_max_threads = m_no_threads = 5;
639 m_listen_duration = 0;
642 for (i = 0; i < 22; i++)
643 m_duration_freq[i] = 0;
644 m_duration_lim[0] = 0.000001;
645 m_duration_lim[1] = 0.00001;
646 m_duration_lim[2] = 0.0001;
647 m_duration_lim[3] = 0.001;
648 m_duration_lim[4] = 0.01;
649 m_duration_lim[5] = 0.1;
650 m_duration_lim[6] = 0.2;
651 m_duration_lim[7] = 0.3;
652 m_duration_lim[8] = 0.5;
653 m_duration_lim[9] = 1.0;
654 m_duration_lim[10] = 1.5;
655 m_duration_lim[11] = 2.0;
656 m_duration_lim[12] = 3.0;
657 m_duration_lim[13] = 4.0;
658 m_duration_lim[14] = 5.0;
659 m_duration_lim[15] = 6.0;
660 m_duration_lim[16] = 8.0;
661 m_duration_lim[17] = 10.0;
662 m_duration_lim[18] = 15.0;
663 m_duration_lim[19] = 20.0;
664 m_duration_lim[20] = 30.0;
665 m_duration_lim[21] = 0.0;
666 m_duration_max = 0.0;
667 m_duration_min = 0.0;
668 m_duration_total = 0.0;
672 yf::FrontendNet::Rep::~Rep()
677 for (i = 0; i < m_ports.size(); i++)
685 yf::FrontendNet::~FrontendNet()
689 void yf::FrontendNet::stop(
int signo)
const
691 m_p->m_stop_signo = signo;
694 void yf::FrontendNet::start()
const
697 struct rlimit limit_data;
698 getrlimit(RLIMIT_NOFILE, &limit_data);
699 yaz_log(YLOG_LOG,
"getrlimit NOFILE cur=%ld max=%ld",
700 (
long) limit_data.rlim_cur, (
long) limit_data.rlim_max);
704 bool yf::FrontendNet::My_Timer_Thread::timeout()
709 yf::FrontendNet::My_Timer_Thread::My_Timer_Thread(
710 yazpp_1::ISocketObservable *obs,
712 m_obs(obs), m_pipe(9123), m_timeout(false)
715 obs->maskObserver(
this, yazpp_1::SOCKET_OBSERVE_READ);
716 obs->timeoutObserver(
this, duration);
719 void yf::FrontendNet::My_Timer_Thread::socketNotify(
int event)
722 m_obs->deleteObserver(
this);
725 void yf::FrontendNet::process(mp::Package &package)
const
732 if (
m_p->m_listen_duration)
734 m_p->m_listen_duration);
741 for (i = 0; i<
m_p->m_ports.size(); i++)
743 m_p->az[i]->set_package(&package);
744 m_p->az[i]->set_thread_pool(
747 while (
m_p->mySocketManager.processEvent() > 0)
749 if (
m_p->m_stop_signo == SIGTERM)
751 yaz_log(YLOG_LOG,
"metaproxy received SIGTERM");
755 for (i = 0; i <
m_p->m_ports.size(); i++)
757 m_p->pdu[i]->shutdown();
758 m_p->az[i]->server(
"");
765 if (
m_p->m_stop_signo == SIGUSR1)
767 yaz_log(YLOG_LOG,
"metaproxy received SIGUSR1");
768 m_p->m_stop_signo = 0;
772 for (i = 0; i <
m_p->m_ports.size(); i++)
773 m_p->az[i]->server(
"");
778 int no =
m_p->mySocketManager.getNumberOfObservers();
788 void yf::FrontendNet::configure(
const xmlNode * ptr,
bool test_only,
791 if (!ptr || !ptr->children)
793 throw yf::FilterException(
"No ports for Frontend");
795 std::vector<Port> ports;
796 for (ptr = ptr->children; ptr; ptr = ptr->next)
798 if (ptr->type != XML_ELEMENT_NODE)
800 if (!strcmp((
const char *) ptr->name,
"port"))
804 const char *names[5] = {
"route",
"max_recv_bytes",
"port",
806 std::string values[4];
808 mp::xml::parse_attr(ptr, names, values);
809 port.
route = values[0];
810 if (values[1].length() > 0)
814 if (values[2].length() > 0)
815 port.
port = values[2];
817 port.
port = mp::xml::get_text(ptr);
819 ports.push_back(port);
821 else if (!strcmp((
const char *) ptr->name,
"threads"))
823 std::string threads_str = mp::xml::get_text(ptr);
824 int threads = atoi(threads_str.c_str());
826 throw yf::FilterException(
"Bad value for threads: "
828 m_p->m_no_threads = threads;
830 else if (!strcmp((
const char *) ptr->name,
"max-threads"))
832 std::string threads_str = mp::xml::get_text(ptr);
833 int threads = atoi(threads_str.c_str());
835 throw yf::FilterException(
"Bad value for max-threads: "
837 m_p->m_max_threads = threads;
839 else if (!strcmp((
const char *) ptr->name,
"stack-size"))
841 std::string sz_str = mp::xml::get_text(ptr);
842 int sz = atoi(sz_str.c_str());
844 throw yf::FilterException(
"Bad value for stack-size: "
846 m_p->m_stack_size = sz * 1024;
848 else if (!strcmp((
const char *) ptr->name,
"timeout"))
850 const char *names[3] = {
"ip",
"verbose", 0};
851 std::string values[2];
853 mp::xml::parse_attr(ptr, names, values);
855 m.
value = mp::xml::get_int(ptr, 0);
857 m.
verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
858 m_p->session_timeout.push_back(m);
860 else if (!strcmp((
const char *) ptr->name,
"connect-max"))
862 const char *names[3] = {
"ip",
"verbose", 0};
863 std::string values[2];
865 mp::xml::parse_attr(ptr, names, values);
867 m.
value = mp::xml::get_int(ptr, INT_MAX);
869 m.
verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
870 m_p->connect_max.push_back(m);
872 else if (!strcmp((
const char *) ptr->name,
"connect-total"))
874 const char *names[3] = {
"ip",
"verbose", 0};
875 std::string values[2];
877 mp::xml::parse_attr(ptr, names, values);
879 m.
value = mp::xml::get_int(ptr, INT_MAX);
881 m.
verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
882 m_p->connect_total.push_back(m);
884 else if (!strcmp((
const char *) ptr->name,
"http-req-max"))
886 const char *names[3] = {
"ip",
"verbose", 0};
887 std::string values[2];
889 mp::xml::parse_attr(ptr, names, values);
891 m.
value = mp::xml::get_int(ptr, INT_MAX);
893 m.
verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
894 m_p->http_req_max.push_back(m);
896 else if (!strcmp((
const char *) ptr->name,
"message"))
898 m_p->m_msg_config = mp::xml::get_text(ptr);
900 else if (!strcmp((
const char *) ptr->name,
"stat-req"))
902 m_p->m_stat_req = mp::xml::get_text(ptr);
906 throw yf::FilterException(
"Bad element "
907 + std::string((
const char *)
911 if (
m_p->m_msg_config.length() > 0 &&
m_p->m_stat_req.length() == 0)
913 m_p->m_stat_req =
"/fn_stat";
920 void yf::FrontendNet::set_ports(std::vector<std::string> &ports)
922 std::vector<Port> nports;
925 for (i = 0; i < ports.size(); i++)
929 nport.
port = ports[i];
931 nports.push_back(nport);
937 void yf::FrontendNet::set_ports(std::vector<Port> &ports)
939 m_p->m_ports = ports;
941 m_p->az =
new yf::FrontendNet::ZAssocServer *[
m_p->m_ports.size()];
942 m_p->pdu =
new yazpp_1::PDU_Assoc *[
m_p->m_ports.size()];
946 for (i = 0; i <
m_p->m_ports.size(); i++)
948 for (i = 0; i <
m_p->m_ports.size(); i++)
951 yazpp_1::PDU_Assoc *as =
new yazpp_1::PDU_Assoc(&
m_p->mySocketManager);
953 if (
m_p->m_ports[i].cert_fname.length())
954 as->set_cert_fname(
m_p->m_ports[i].cert_fname.c_str());
957 m_p->az[i] =
new yf::FrontendNet::ZAssocServer(
958 as, &
m_p->m_ports[i],
m_p.get());
959 if (
m_p->az[i]->server(
m_p->m_ports[i].port.c_str()))
961 throw yf::FilterException(
"Unable to bind to address "
962 + std::string(
m_p->m_ports[i].port));
964 COMSTACK cs = as->get_comstack();
966 if (cs &&
m_p->m_ports[i].max_recv_bytes)
967 cs_set_max_recv_bytes(cs,
m_p->m_ports[i].max_recv_bytes);
972 void yf::FrontendNet::set_listen_duration(
int d)
974 m_p->m_listen_duration = d;
978 yf::FrontendNet::PeerStat::PeerStat()
983 yf::FrontendNet::PeerStat::~PeerStat()
994 size_t yf::FrontendNet::PeerStat::add(
const std::string &peer)
997 for (; n; n = n->
next)
1013 size_t yf::FrontendNet::PeerStat::get(
const std::string &peer)
1016 for (; n; n = n->
next)
1024 size_t yf::FrontendNet::PeerStat::remove(
const std::string &peer)
1027 for (; *np; np = &(*np)->
next)
1046 return new yf::FrontendNet;
static yf::Base * filter_creator()
struct metaproxy_1_filter_struct metaproxy_1_filter_frontend_net