aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/SysctlUtil.py
blob: 0db7e2c5e5f3791d81494b3d447018d0c5dab521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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.

"""Linux sysctl library."""

from resources.libraries.python.ssh import exec_cmd_no_error

__all__ = ["SysctlUtil"]


class SysctlUtil(object):
    """Class contains methods for getting or setting sysctl settings."""

    @staticmethod
    def get_sysctl_value(node, key):
        """Get sysctl key.

        :param node: Node in the topology.
        :param key: Key that will be set.
        :type node: dict
        :type key: str
        """
        command = 'sysctl {key}'.format(key=key)

        message = 'Node {host} failed to run: {command}'.\
            format(host=node['host'], command=command)

        exec_cmd_no_error(node, command, sudo=True, message=message)

    @staticmethod
    def set_sysctl_value(node, key, value):
        """Set sysctl key to specific value.

        :param node: Node in the topology.
        :param key: Key that will be set.
        :param value: Value to set.
        :type node: dict
        :type key: str
        :type value: str
        """
        command = 'sysctl -w {key}={value}'.format(key=key, value=value)

        message = 'Node {host} failed to run: {command}'.\
            format(host=node['host'], command=command)

        exec_cmd_no_error(node, command, sudo=True, message=message)
= lacp_input (vm, b0, bi0); b0->error = node->errors[error0]; /* If this pkt is traced, snapshoot the data */ if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) { int len; t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); len = (b0->current_length < sizeof (t0->pkt)) ? b0->current_length : sizeof (t0->pkt); t0->len = len; t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX]; clib_memcpy_fast (&t0->pkt, vlib_buffer_get_current (b0), len); } /* push this pkt to the next graph node, always error-drop */ vlib_set_next_frame_buffer (vm, node, next0, bi0); from += 1; n_left_from -= 1; } return frame->n_vectors; } /* * lacp input graph node declaration */ /* *INDENT-OFF* */ VLIB_REGISTER_NODE (lacp_input_node, static) = { .function = lacp_node_fn, .name = "lacp-input", .vector_size = sizeof (u32), .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = LACP_N_ERROR, .error_strings = lacp_error_strings, .format_trace = lacp_input_format_trace, .n_next_nodes = LACP_INPUT_N_NEXT, .next_nodes = { [LACP_INPUT_NEXT_NORMAL] = "error-drop", }, }; /* *INDENT-ON* */ static void lacp_elog_start_event (void) { lacp_main_t *lm = &lacp_main; /* *INDENT-OFF* */ ELOG_TYPE_DECLARE (e) = { .format = "Starting LACP process, interface count = %d", .format_args = "i4", }; /* *INDENT-ON* */ struct { u32 count; } *ed; ed = ELOG_DATA (&vlib_global_main.elog_main, e); ed->count = lm->lacp_int; } static void lacp_elog_stop_event (void) { lacp_main_t *lm = &lacp_main; /* *INDENT-OFF* */ ELOG_TYPE_DECLARE (e) = { .format = "Stopping LACP process, interface count = %d", .format_args = "i4", }; /* *INDENT-ON* */ struct { u32 count; } *ed; ed = ELOG_DATA (&vlib_global_main.elog_main, e); ed->count = lm->lacp_int; } /* * lacp periodic function */ static uword lacp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) { lacp_main_t *lm = &lacp_main; f64 poll_time_remaining; uword event_type, *event_data = 0; ethernet_register_input_type (vm, ETHERNET_TYPE_SLOW_PROTOCOLS /* LACP */ , lacp_input_node.index); poll_time_remaining = 0.2; while (1) { if (lm->lacp_int > 0) poll_time_remaining = vlib_process_wait_for_event_or_clock (vm, poll_time_remaining); else vlib_process_wait_for_event (vm); event_type = vlib_process_get_events (vm, &event_data); switch (event_type) { case ~0: /* no events => timeout */ break; case LACP_PROCESS_EVENT_START: poll_time_remaining = 0.2; lacp_elog_start_event (); break; case LACP_PROCESS_EVENT_STOP: if (lm->lacp_int == 0) { poll_time_remaining = SECS_IN_A_DAY; lacp_elog_stop_event (); } break; default: clib_warning ("BUG: event type 0x%wx", event_type); break; } vec_reset_length (event_data); if (vlib_process_suspend_time_is_zero (poll_time_remaining)) { lacp_periodic (vm); poll_time_remaining = 0.2; } } return 0; } void lacp_create_periodic_process (void) { lacp_main_t *lm = &lacp_main; /* Already created the process node? */ if (lm->lacp_process_node_index > 0) return; /* No, create it now and make a note of the node index */ lm->lacp_process_node_index = vlib_process_create (lm->vlib_main, "lacp-process", lacp_process, 16 /* log2_n_stack_bytes */ ); } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */