summaryrefslogtreecommitdiffstats
path: root/src/vnet
diff options
context:
space:
mode:
authorFlorin Coras <fcoras@cisco.com>2019-01-14 23:33:46 -0800
committerDave Barach <openvpp@barachs.net>2019-01-17 20:31:54 +0000
commit58a93e8ef288b0bae75ec7186ba96bdcaf85d0d4 (patch)
tree2345aa718a507bd8bfc1857d705e9614e408be18 /src/vnet
parent72b04288d9a670829050a6ca5d931ae5b55b33ed (diff)
tls: preallocate app sessions on connect/accept
Avoid allocating session and possibly reallocating thread session pool on builtin session rx. Change-Id: I70e7c604678b44ce8d22603489e247a2c5faa439 Signed-off-by: Florin Coras <fcoras@cisco.com>
Diffstat (limited to 'src/vnet')
-rw-r--r--src/vnet/session/segment_manager.c3
-rw-r--r--src/vnet/tls/tls.c25
-rw-r--r--src/vnet/tls/tls.h7
3 files changed, 25 insertions, 10 deletions
diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c
index e48e951722d..a0d9a0959a5 100644
--- a/src/vnet/session/segment_manager.c
+++ b/src/vnet/session/segment_manager.c
@@ -599,6 +599,9 @@ segment_manager_dealloc_fifos (u32 segment_index, svm_fifo_t * rx_fifo,
svm_fifo_segment_private_t *fifo_segment;
segment_manager_t *sm;
+ if (!rx_fifo || !tx_fifo)
+ return;
+
/* It's possible to have no segment manager if the session was removed
* as result of a detach. */
if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
diff --git a/src/vnet/tls/tls.c b/src/vnet/tls/tls.c
index 34de539b295..d51d5dbaa38 100644
--- a/src/vnet/tls/tls.c
+++ b/src/vnet/tls/tls.c
@@ -206,7 +206,7 @@ tls_notify_app_accept (tls_ctx_t * ctx)
app = application_get (app_wrk->app_index);
lctx = tls_listener_ctx_get (ctx->listener_ctx_index);
- app_session = session_alloc (vlib_get_thread_index ());
+ app_session = session_get (ctx->c_s_index, ctx->c_thread_index);
app_session->app_wrk_index = ctx->parent_app_index;
app_session->connection_index = ctx->tls_ctx_handle;
@@ -221,7 +221,6 @@ tls_notify_app_accept (tls_ctx_t * ctx)
TLS_DBG (1, "failed to allocate fifos");
return rv;
}
- ctx->c_s_index = app_session->session_index;
ctx->app_session_handle = session_handle (app_session);
session_lookup_add_connection (&ctx->connection,
session_handle (app_session));
@@ -251,7 +250,7 @@ tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed)
goto failed;
sm = app_worker_get_connect_segment_manager (app_wrk);
- app_session = session_alloc (vlib_get_thread_index ());
+ app_session = session_get (ctx->c_s_index, ctx->c_thread_index);
app_session->app_wrk_index = ctx->parent_app_index;
app_session->connection_index = ctx->tls_ctx_handle;
app_session->session_type =
@@ -261,7 +260,6 @@ tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed)
if (session_alloc_fifos (sm, app_session))
goto failed;
- ctx->app_session_handle = session_handle (app_session);
app_session->session_state = SESSION_STATE_CONNECTING;
if (cb_fn (ctx->parent_app_index, ctx->parent_app_api_context,
app_session, 0 /* not failed */ ))
@@ -271,9 +269,7 @@ tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed)
return -1;
}
- /* parent_app_api_context should not be overwitten before used,
- * so defer setting c_s_index */
- ctx->c_s_index = app_session->session_index;
+ ctx->app_session_handle = session_handle (app_session);
app_session->session_state = SESSION_STATE_READY;
session_lookup_add_connection (&ctx->connection,
session_handle (app_session));
@@ -405,7 +401,7 @@ tls_session_disconnect_callback (stream_session_t * tls_session)
int
tls_session_accept_callback (stream_session_t * tls_session)
{
- stream_session_t *tls_listener;
+ stream_session_t *tls_listener, *app_session;
tls_ctx_t *lctx, *ctx;
u32 ctx_handle;
@@ -422,6 +418,12 @@ tls_session_accept_callback (stream_session_t * tls_session)
ctx->tls_session_handle = session_handle (tls_session);
ctx->listener_ctx_index = tls_listener->opaque;
+ /* Preallocate app session. Avoids allocating a session post handshake
+ * on tls_session rx and potentially invalidating the session pool */
+ app_session = session_alloc (ctx->c_thread_index);
+ app_session->session_state = SESSION_STATE_CLOSED;
+ ctx->c_s_index = app_session->session_index;
+
TLS_DBG (1, "Accept on listener %u new connection [%u]%x",
tls_listener->opaque, vlib_get_thread_index (), ctx_handle);
@@ -453,6 +455,7 @@ int
tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index,
stream_session_t * tls_session, u8 is_fail)
{
+ stream_session_t *app_session;
tls_ctx_t *ho_ctx, *ctx;
u32 ctx_handle;
@@ -496,6 +499,12 @@ tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index,
tls_session->opaque = ctx_handle;
tls_session->session_state = SESSION_STATE_READY;
+ /* Preallocate app session. Avoids allocating a session post handshake
+ * on tls_session rx and potentially invalidating the session pool */
+ app_session = session_alloc (ctx->c_thread_index);
+ app_session->session_state = SESSION_STATE_CLOSED;
+ ctx->c_s_index = app_session->session_index;
+
return tls_ctx_init_client (ctx);
}
diff --git a/src/vnet/tls/tls.h b/src/vnet/tls/tls.h
index 09f1bdc7b07..c4f04673f2e 100644
--- a/src/vnet/tls/tls.h
+++ b/src/vnet/tls/tls.h
@@ -39,7 +39,10 @@
typedef CLIB_PACKED (struct tls_cxt_id_
{
u32 parent_app_index;
- session_handle_t app_session_handle;
+ union {
+ session_handle_t app_session_handle;
+ u32 parent_app_api_ctx;
+ };
session_handle_t tls_session_handle;
u32 ssl_ctx;
u32 listener_ctx_index;
@@ -67,7 +70,7 @@ typedef struct tls_ctx_
#define tls_ctx_handle c_c_index
/* Temporary storage for session open opaque. Overwritten once
* underlying tcp connection is established */
-#define parent_app_api_context c_s_index
+#define parent_app_api_context c_tls_ctx_id.parent_app_api_ctx
u8 is_passive_close;
u8 resume;
ef='#n218'>218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
# Copyright (c) 2019 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generator of alerts:
- failed tests
- regressions
- progressions
"""


import smtplib
import logging
import re

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from os.path import isdir
from collections import OrderedDict

from pal_utils import get_last_completed_build_number
from pal_errors import PresentationError


class AlertingError(PresentationError):
    """Exception(s) raised by the alerting module.

    When raising this exception, put this information to the message in this
    order:
     - short description of the encountered problem (parameter msg),
     - relevant messages if there are any collected, e.g., from caught
       exception (optional parameter details),
     - relevant data if there are any collected (optional parameter details).
    """

    def __init__(self, msg, details=u'', level=u"CRITICAL"):
        """Sets the exception message and the level.

        :param msg: Short description of the encountered problem.
        :param details: Relevant messages if there are any collected, e.g.,
            from caught exception (optional parameter details), or relevant data
            if there are any collected (optional parameter details).
        :param level: Level of the error, possible choices are: "DEBUG", "INFO",
            "WARNING", "ERROR" and "CRITICAL".
        :type msg: str
        :type details: str
        :type level: str
        """

        super(AlertingError, self).__init__(f"Alerting: {msg}", details, level)

    def __repr__(self):
        return (
            f"AlertingError(msg={self._msg!r},details={self._details!r},"
            f"level={self._level!r})"
        )


class Alerting:
    """Class implementing the alerting mechanism.
    """

    def __init__(self, spec):
        """Initialization.

        :param spec: The CPTA specification.
        :type spec: Specification
        """

        # Implemented alerts:
        self._implemented_alerts = (u"failed-tests", )

        self._spec = spec

        try:
            self._spec_alert = spec.alerting
        except KeyError as err:
            raise AlertingError(u"Alerting is not configured, skipped.",
                                repr(err),
                                u"WARNING")

        self._path_failed_tests = spec.environment[u"paths"][u"DIR[STATIC,VPP]"]

        # Verify and validate input specification:
        self.configs = self._spec_alert.get(u"configurations", None)
        if not self.configs:
            raise AlertingError(u"No alert configuration is specified.")
        for config_type, config_data in self.configs.items():
            if config_type == u"email":
                if not config_data.get(u"server", None):
                    raise AlertingError(u"Parameter 'server' is missing.")
                if not config_data.get(u"address-to", None):
                    raise AlertingError(u"Parameter 'address-to' (recipient) "
                                        u"is missing.")
                if not config_data.get(u"address-from", None):
                    raise AlertingError(u"Parameter 'address-from' (sender) is "
                                        u"missing.")
            elif config_type == u"jenkins":
                if not isdir(config_data.get(u"output-dir", u"")):
                    raise AlertingError(u"Parameter 'output-dir' is "
                                        u"missing or it is not a directory.")
                if not config_data.get(u"output-file", None):
                    raise AlertingError(u"Parameter 'output-file' is missing.")
            else:
                raise AlertingError(
                    f"Alert of type {config_type} is not implemented."
                )

        self.alerts = self._spec_alert.get(u"alerts", None)
        if not self.alerts:
            raise AlertingError(u"No alert is specified.")
        for alert_data in self.alerts.values():
            if not alert_data.get(u"title", None):
                raise AlertingError(u"Parameter 'title' is missing.")
            if not alert_data.get(u"type", None) in self._implemented_alerts:
                raise AlertingError(u"Parameter 'failed-tests' is missing or "
                                    u"incorrect.")
            if not alert_data.get(u"way", None) in self.configs.keys():
                raise AlertingError(u"Parameter 'way' is missing or incorrect.")
            if not alert_data.get(u"include", None):
                raise AlertingError(u"Parameter 'include' is missing or the "
                                    u"list is empty.")

    def __str__(self):
        """Return string with human readable description of the alert.

        :returns: Readable description.
        :rtype: str
        """
        return f"configs={self.configs}, alerts={self.alerts}"

    def __repr__(self):
        """Return string executable as Python constructor call.

        :returns: Executable constructor call.
        :rtype: str
        """
        return f"Alerting(spec={self._spec})"

    def generate_alerts(self):
        """Generate alert(s) using specified way(s).
        """

        for alert_data in self.alerts.values():
            if alert_data[u"way"] == u"jenkins":
                self._generate_email_body(alert_data)
            else:
                raise AlertingError(
                    f"Alert with way {alert_data[u'way']} is not implemented."
                )

    @staticmethod
    def _send_email(server, addr_from, addr_to, subject, text=None, html=None):
        """Send an email using predefined configuration.

        :param server: SMTP server used to send email.
        :param addr_from: Sender address.
        :param addr_to: Recipient address(es).
        :param subject: Subject of the email.
        :param text: Message in the ASCII text format.
        :param html: Message in the HTML format.
        :type server: str
        :type addr_from: str
        :type addr_to: list
        :type subject: str
        :type text: str
        :type html: str
        """

        if not text and not html:
            raise AlertingError(u"No text/data to send.")

        msg = MIMEMultipart(u'alternative')
        msg[u'Subject'] = subject
        msg[u'From'] = addr_from
        msg[u'To'] = u", ".join(addr_to)

        if text:
            msg.attach(MIMEText(text, u'plain'))
        if html:
            msg.attach(MIMEText(html, u'html'))

        smtp_server = None
        try:
            logging.info(f"Trying to send alert {subject} ...")
            logging.debug(f"SMTP Server: {server}")
            logging.debug(f"From: {addr_from}")
            logging.debug(f"To: {u', '.join(addr_to)}")
            logging.debug(f"Message: {msg.as_string()}")
            smtp_server = smtplib.SMTP(server)
            smtp_server.sendmail(addr_from, addr_to, msg.as_string())
        except smtplib.SMTPException as err:
            raise AlertingError(u"Not possible to send the alert via email.",
                                str(err))
        finally:
            if smtp_server:
                smtp_server.quit()

    def _get_compressed_failed_tests(self, alert, test_set, sort=True):
        """Return the dictionary with compressed faild tests. The compression is
        done by grouping the tests from the same area but with different NICs,
        frame sizes and number of processor cores.

        For example, the failed tests:
          10ge2p1x520-64b-1c-ethip4udp-ip4scale4000-udpsrcscale15-nat44-mrr
          10ge2p1x520-64b-2c-ethip4udp-ip4scale4000-udpsrcscale15-nat44-mrr
          10ge2p1x520-64b-4c-ethip4udp-ip4scale4000-udpsrcscale15-nat44-mrr
          10ge2p1x520-imix-1c-ethip4udp-ip4scale4000-udpsrcscale15-nat44-mrr
          10ge2p1x520-imix-2c-ethip4udp-ip4scale4000-udpsrcscale15-nat44-mrr
          10ge2p1x520-imix-4c-ethip4udp-ip4scale4000-udpsrcscale15-nat44-mrr

        will be represented as:
          ethip4udp-ip4scale4000-udpsrcscale15-nat44 \
          (10ge2p1x520, 64b, imix, 1c, 2c, 4c)

        Structure of returned data:

        {
            "trimmed_TC_name_1": {
                "nics": [],
                "framesizes": [],
                "cores": []
            }
            ...
            "trimmed_TC_name_N": {
                "nics": [],
                "framesizes": [],
                "cores": []
            }
        }

        :param alert: Files are created for this alert.
        :param test_set: Specifies which set of tests will be included in the
            result. Its name is the same as the name of file with failed tests.
        :param sort: If True, the failed tests are sorted alphabetically.
        :type alert: dict
        :type test_set: str
        :type sort: bool
        :returns: CSIT build number, VPP version, Number of passed tests,
            Number of failed tests, Compressed failed tests.
        :rtype: tuple(str, str, int, int, OrderedDict)
        """

        directory = self.configs[alert[u"way"]][u"output-dir"]
        failed_tests = OrderedDict()
        file_path = f"{directory}/{test_set}.txt"
        version = u""
        try:
            with open(file_path, u'r') as f_txt:
                for idx, line in enumerate(f_txt):
                    if idx == 0:
                        build = line[:-1]
                        continue
                    if idx == 1:
                        version = line[:-1]
                        continue
                    if idx == 2:
                        passed = line[:-1]
                        continue
                    if idx == 3:
                        failed = line[:-1]
                        continue
                    try:
                        test = line[:-1].split(u'-')
                        name = u'-'.join(test[3:-1])
                    except IndexError:
                        continue
                    if failed_tests.get(name, None) is None:
                        failed_tests[name] = dict(nics=list(),
                                                  framesizes=list(),
                                                  cores=list())
                    if test[0] not in failed_tests[name][u"nics"]:
                        failed_tests[name][u"nics"].append(test[0])
                    if test[1] not in failed_tests[name][u"framesizes"]:
                        failed_tests[name][u"framesizes"].append(test[1])
                    if test[2] not in failed_tests[name][u"cores"]:
                        failed_tests[name][u"cores"].append(test[2])
        except IOError:
            logging.error(f"No such file or directory: {file_path}")
            return None, None, None, None, None
        if sort:
            sorted_failed_tests = OrderedDict()
            for key in sorted(failed_tests.keys()):
                sorted_failed_tests[key] = failed_tests[key]
            return build, version, passed, failed, sorted_failed_tests

        return build, version, passed, failed, failed_tests

    def _list_gressions(self, alert, idx, header, re_pro):
        """Create a file with regressions or progressions for the test set
        specified by idx.

        :param alert: Files are created for this alert.
        :param idx: Index of the test set as it is specified in the
            specification file.
        :param header: The header of the list of [re|pro]gressions.
        :param re_pro: 'regression' or 'progression'.
        :type alert: dict
        :type idx: int
        :type header: str
        :type re_pro: str
        """

        if re_pro not in (u"regressions", u"progressions"):
            return

        in_file = (
            f"{self.configs[alert[u'way']][u'output-dir']}/"
            f"{re_pro}-{alert[u'urls'][idx].split(u'/')[-1]}.txt"
        )
        out_file = (
            f"{self.configs[alert[u'way']][u'output-dir']}/"
            f"trending-{re_pro}.txt"
        )

        try:
            with open(in_file, u'r') as txt_file:
                file_content = txt_file.read()
                with open(out_file, u'a+') as reg_file:
                    reg_file.write(header)
                    if file_content:
                        reg_file.write(file_content)
                    else:
                        reg_file.write(f"No {re_pro}")
        except IOError as err:
            logging.warning(repr(err))

    def _generate_email_body(self, alert):
        """Create the file which is used in the generated alert.

        :param alert: Files are created for this alert.
        :type alert: dict
        """

        if alert[u"type"] != u"failed-tests":
            raise AlertingError(
                f"Alert of type {alert[u'type']} is not implemented."
            )

        text = u""
        for idx, test_set in enumerate(alert.get(u"include", list())):
            test_set_short = u""
            device = u""
            try:
                groups = re.search(
                    re.compile(r'((vpp|dpdk)-\dn-(skx|clx|hsw|tsh|dnv)-.*)'),
                    test_set
                )
                test_set_short = groups.group(1)
                device = groups.group(2)
            except (AttributeError, IndexError):
                logging.error(
                    f"The test set {test_set} does not include information "
                    f"about test bed. Using empty string instead."
                )
            build, version, passed, failed, failed_tests = \
                self._get_compressed_failed_tests(alert, test_set)
            if build is None:
                ret_code, build_nr, _ = get_last_completed_build_number(
                    self._spec.environment[u"urls"][u"URL[JENKINS,CSIT]"],
                    alert[u"urls"][idx].split(u'/')[-1])
                if ret_code != 0:
                    build_nr = u''
                text += (
                    f"\n\nNo input data available for {test_set_short}. "
                    f"See CSIT build {alert[u'urls'][idx]}/{build_nr} for more "
                    f"information.\n"
                )
                continue
            text += (
                f"\n\n{test_set_short}, {failed} tests failed, {passed} tests "
                f"passed, CSIT build: {alert[u'urls'][idx]}/{build}, "
                f"{device} version: {version}\n\n"
            )

            class MaxLens():
                """Class to store the max lengths of strings displayed in
                failed tests list.
                """
                def __init__(self, tst_name, nics, framesizes, cores):
                    """Initialisation.

                    :param tst_name: Name of the test.
                    :param nics: NICs used in the test.
                    :param framesizes: Frame sizes used in the tests
                    :param cores: Cores used in th test.
                    """
                    self.name = tst_name
                    self.nics = nics
                    self.frmsizes = framesizes
                    self.cores = cores

            max_len = MaxLens(0, 0, 0, 0)

            for name, params in failed_tests.items():
                failed_tests[name][u"nics"] = u",".join(sorted(params[u"nics"]))
                failed_tests[name][u"framesizes"] = \
                    u",".join(sorted(params[u"framesizes"]))
                failed_tests[name][u"cores"] = \
                    u",".join(sorted(params[u"cores"]))
                if len(name) > max_len.name:
                    max_len.name = len(name)
                if len(failed_tests[name][u"nics"]) > max_len.nics:
                    max_len.nics = len(failed_tests[name][u"nics"])
                if len(failed_tests[name][u"framesizes"]) > max_len.frmsizes:
                    max_len.frmsizes = len(failed_tests[name][u"framesizes"])
                if len(failed_tests[name][u"cores"]) > max_len.cores:
                    max_len.cores = len(failed_tests[name][u"cores"])

            for name, params in failed_tests.items():
                text += (
                    f"{name + u' ' * (max_len.name - len(name))}  "
                    f"{params[u'nics']}"
                    f"{u' ' * (max_len.nics - len(params[u'nics']))}  "
                    f"{params[u'framesizes']}"
                    f"{u' ' * (max_len.frmsizes-len(params[u'framesizes']))}  "
                    f"{params[u'cores']}"
                    f"{u' ' * (max_len.cores - len(params[u'cores']))}\n"
                )

            gression_hdr = (
                f"\n\n{test_set_short}, "
                f"CSIT build: {alert[u'urls'][idx]}/{build}, "
                f"{device} version: {version}\n\n"
            )
            # Add list of regressions:
            self._list_gressions(alert, idx, gression_hdr, u"regressions")

            # Add list of progressions:
            self._list_gressions(alert, idx, gression_hdr, u"progressions")

        text += f"\nFor detailed information visit: {alert[u'url-details']}\n"
        file_name = f"{self.configs[alert[u'way']][u'output-dir']}/" \
                    f"{self.configs[alert[u'way']][u'output-file']}"
        logging.info(f"Writing the file {file_name}.txt ...")

        try:
            with open(f"{file_name}.txt", u'w') as txt_file:
                txt_file.write(text)
        except IOError:
            logging.error(f"Not possible to write the file {file_name}.txt.")