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)(ncsi_state_t*,
31                                                    network_debug_t*,
32                                                    ncsi_buf_t*, mac_addr_t*,
33                                                    uint32_t, uint16_t);
34 
35 // This class encapsulates three state machines:
36 //  * L2 -- performs basic NC-SI setup, reads NIC MAC addr
37 //  * L3/4 -- once network is configured on the interface,
38 //      sets up NC-SI filter in the NIC.
39 //  * Test -- runs several basic NC-SI link tests, like
40 //      ECHO Request/Reply, checks filter setup etc.
41 //      Also, reads hostless/host-based flag from the NIC, see
42 //      ncsi_fsm.c:is_nic_hostless() for details.
43 class StateMachine
44 {
45   public:
46     StateMachine();
47     ~StateMachine();
48 
49     void set_sockio(net::SockIO* sock_io);
50 
51     void set_net_config(net::ConfigBase* net_config);
52 
53     // NC-SI State Machine's main function.
54     // max_rounds = 0 means run forever.
55     void run(int max_rounds = 0);
56 
57     // How often Test FSM re-runs, in seconds.
58     void set_retest_delay(unsigned int delay);
59 
60   private:
61     // Reset the state machine
62     void reset();
63 
64     // Poll L2 state machine. Each call advances it by one step.
65     // Its implementation is taken directly from EC.
66     size_t poll_l2_config();
67 
68     // This function is used to poll both L3/4 and Test state machine,
69     // depending on the function passed in as an argument.
70     size_t poll_simple(ncsi_simple_poll_f poll_func);
71 
72     // Helper function for printing NC-SI error to stdout.
73     void report_ncsi_error(ncsi_response_type_t response_type);
74 
75     int receive_ncsi();
76 
77     // Helper function for advancing the test FSM.
78     void run_test_fsm(size_t* tx_len);
79 
80     // Clear the state and reset all state machines.
81     void clear_state();
82 
83     // In current implementation this is the same as clear state,
84     // except that it also increments the failure counter.
85     void fail();
86 
87     // Return true if the test state machine finished successfully.
88     bool is_test_done() const
89     {
90         return ncsi_state_.test_state == NCSI_STATE_TEST_END;
91     }
92 
93     // Max number of times a state machine is going to retry a command.
94     static constexpr auto MAX_TRIES = 5;
95 
96     // How long (in seconds) to wait before re-running NC-SI test state
97     // machine.
98     unsigned int retest_delay_s_ = 1;
99 
100     // The last known state of the link on the NIC
101     std::optional<bool> link_up_;
102 
103     // The last known hostless mode of the NIC
104     std::optional<bool> hostless_;
105 
106     // net_config_ is used to query and set network configuration.
107     // The StateMachine does not own the pointer and it is the
108     // responsibility of the client to make sure that it outlives the
109     // StateMachine.
110     net::ConfigBase* net_config_ = nullptr;
111 
112     // sock_io_ is used to read and write NC-SI packets.
113     // The StateMachine does not own the pointer. It is the responsibility
114     // of the client to make sure that sock_io_ outlives the StateMachine.
115     net::SockIO* sock_io_ = nullptr;
116 
117     // Both ncsi_state_ and network_debug_ parameters represent the state of
118     // the NC-SI state machine. The names and definitions are taken directly
119     // from EC.
120     ncsi_state_t ncsi_state_;
121     network_debug_t network_debug_;
122 
123     // Depending on the state ncsi_buf_ represents either the NC-SI packet
124     // received from the NIC or NC-SI packet that was (or about to be)
125     // sent to the NIC.
126     ncsi_buf_t ncsi_buf_;
127 };
128 
129 } // namespace ncsi
130