1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 #include "ncsi_sockio.h" 19 #include "net_config.h" 20 #include "net_iface.h" 21 #include "platforms/nemora/portable/ncsi_client.h" 22 #include "platforms/nemora/portable/ncsi_fsm.h" 23 #include "platforms/nemora/portable/net_types.h" 24 25 #include <optional> 26 27 namespace ncsi 28 { 29 30 typedef ncsi_response_type_t (*ncsi_simple_poll_f)( 31 ncsi_state_t*, network_debug_t*, ncsi_buf_t*, mac_addr_t*, uint32_t, 32 uint16_t); 33 34 // This class encapsulates three state machines: 35 // * L2 -- performs basic NC-SI setup, reads NIC MAC addr 36 // * L3/4 -- once network is configured on the interface, 37 // sets up NC-SI filter in the NIC. 38 // * Test -- runs several basic NC-SI link tests, like 39 // ECHO Request/Reply, checks filter setup etc. 40 // Also, reads hostless/host-based flag from the NIC, see 41 // ncsi_fsm.c:is_nic_hostless() for details. 42 class StateMachine 43 { 44 public: 45 StateMachine(); 46 ~StateMachine(); 47 48 void set_sockio(net::SockIO* sock_io); 49 50 void set_net_config(net::ConfigBase* net_config); 51 52 // NC-SI State Machine's main function. 53 // max_rounds = 0 means run forever. 54 void run(int max_rounds = 0); 55 56 // How often Test FSM re-runs, in seconds. 57 void set_retest_delay(unsigned int delay); 58 59 private: 60 // Reset the state machine 61 void reset(); 62 63 // Poll L2 state machine. Each call advances it by one step. 64 // Its implementation is taken directly from EC. 65 size_t poll_l2_config(); 66 67 // This function is used to poll both L3/4 and Test state machine, 68 // depending on the function passed in as an argument. 69 size_t poll_simple(ncsi_simple_poll_f poll_func); 70 71 // Helper function for printing NC-SI error to stdout. 72 void report_ncsi_error(ncsi_response_type_t response_type); 73 74 int receive_ncsi(); 75 76 // Helper function for advancing the test FSM. 77 void run_test_fsm(size_t* tx_len); 78 79 // Clear the state and reset all state machines. 80 void clear_state(); 81 82 // In current implementation this is the same as clear state, 83 // except that it also increments the failure counter. 84 void fail(); 85 86 // Return true if the test state machine finished successfully. is_test_done()87 bool is_test_done() const 88 { 89 return ncsi_state_.test_state == NCSI_STATE_TEST_END; 90 } 91 92 // Max number of times a state machine is going to retry a command. 93 static constexpr auto MAX_TRIES = 5; 94 95 // How long (in seconds) to wait before re-running NC-SI test state 96 // machine. 97 unsigned int retest_delay_s_ = 1; 98 99 // The last known state of the link on the NIC 100 std::optional<bool> link_up_; 101 102 // The last known hostless mode of the NIC 103 std::optional<bool> hostless_; 104 105 // net_config_ is used to query and set network configuration. 106 // The StateMachine does not own the pointer and it is the 107 // responsibility of the client to make sure that it outlives the 108 // StateMachine. 109 net::ConfigBase* net_config_ = nullptr; 110 111 // sock_io_ is used to read and write NC-SI packets. 112 // The StateMachine does not own the pointer. It is the responsibility 113 // of the client to make sure that sock_io_ outlives the StateMachine. 114 net::SockIO* sock_io_ = nullptr; 115 116 // Both ncsi_state_ and network_debug_ parameters represent the state of 117 // the NC-SI state machine. The names and definitions are taken directly 118 // from EC. 119 ncsi_state_t ncsi_state_; 120 network_debug_t network_debug_; 121 122 // Depending on the state ncsi_buf_ represents either the NC-SI packet 123 // received from the NIC or NC-SI packet that was (or about to be) 124 // sent to the NIC. 125 ncsi_buf_t ncsi_buf_; 126 }; 127 128 } // namespace ncsi 129