summaryrefslogtreecommitdiff
path: root/kvmd/apps/ipmi/server.py
blob: 597e6dce60119367f55222970a5279b563afe567 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
# ========================================================================== #
#                                                                            #
#    KVMD - The main PiKVM daemon.                                           #
#                                                                            #
#    Copyright (C) 2018-2022  Maxim Devaev <[email protected]>               #
#                                                                            #
#    This program is free software: you can redistribute it and/or modify    #
#    it under the terms of the GNU General Public License as published by    #
#    the Free Software Foundation, either version 3 of the License, or       #
#    (at your option) any later version.                                     #
#                                                                            #
#    This program is distributed in the hope that it will be useful,         #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#    GNU General Public License for more details.                            #
#                                                                            #
#    You should have received a copy of the GNU General Public License       #
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.  #
#                                                                            #
# ========================================================================== #


import os
import select
import asyncio
import threading
import multiprocessing
import functools
import queue

from typing import Dict
from typing import Optional

import aiohttp
import serial

from pyghmi.ipmi.console import ServerConsole as IpmiConsole
from pyghmi.ipmi.private.session import Session as IpmiSession
from pyghmi.ipmi.private.serversession import IpmiServer as BaseIpmiServer
from pyghmi.ipmi.private.serversession import ServerSession as IpmiServerSession

from ...logging import get_logger

from ...clients.kvmd import KvmdClient

from ... import aiotools

from .auth import IpmiAuthManager


# =====
class IpmiServer(BaseIpmiServer):  # pylint: disable=too-many-instance-attributes,abstract-method
    # https://www.intel.com/content/dam/www/public/us/en/documents/product-briefs/ipmi-second-gen-interface-spec-v2-rev1-1.pdf
    # https://www.thomas-krenn.com/en/wiki/IPMI_Basics

    def __init__(
        self,
        auth_manager: IpmiAuthManager,
        kvmd: KvmdClient,

        host: str,
        port: int,
        timeout: float,

        sol_device_path: str,
        sol_speed: int,
        sol_select_timeout: float,
        sol_proxy_port: int,
    ) -> None:

        super().__init__(authdata=auth_manager, address=host, port=port)

        self.__auth_manager = auth_manager
        self.__kvmd = kvmd

        self.__host = host
        self.__port = port
        self.__timeout = timeout

        self.__sol_device_path = sol_device_path
        self.__sol_speed = sol_speed
        self.__sol_select_timeout = sol_select_timeout
        self.__sol_proxy_port = (sol_proxy_port or port)

        self.__sol_lock = threading.Lock()
        self.__sol_console: Optional[IpmiConsole] = None
        self.__sol_thread: Optional[threading.Thread] = None
        self.__sol_stop = False

    def run(self) -> None:
        logger = get_logger(0)
        logger.info("Listening IPMI on UPD [%s]:%d ...", self.__host, self.__port)
        try:
            while True:
                IpmiSession.wait_for_rsp(self.__timeout)
        except (SystemExit, KeyboardInterrupt):
            pass
        self.__stop_sol_worker()
        logger.info("Bye-bye")

    # =====

    def handle_raw_request(self, request: Dict, session: IpmiServerSession) -> None:
        handler = {
            (6, 1): (lambda _, session: self.send_device_id(session)),  # Get device ID
            (6, 7): self.__get_power_state_handler,  # Power state
            (6, 4): self.__get_selftest_status_handler,  # Self-test
            (6, 0x38): (lambda _, session: session.send_ipmi_response()),  # Get channel auth types
            (0, 1): self.__get_chassis_status_handler,  # Get chassis status
            (0, 2): self.__chassis_control_handler,  # Chassis control
            (6, 0x48): self.__activate_sol_handler,  # Enable SOL
            (6, 0x49): self.__deactivate_sol_handler,  # Disable SOL
        }.get((request["netfn"], request["command"]))
        if handler is not None:
            try:
                handler(request, session)
            except (aiohttp.ClientError, asyncio.TimeoutError):
                session.send_ipmi_response(code=0xFF)
            except Exception:
                get_logger(0).exception("[%s]: Unexpected exception while handling IPMI request: netfn=%d; command=%d",
                                        session.sockaddr[0], request["netfn"], request["command"])
                session.send_ipmi_response(code=0xFF)
        else:
            session.send_ipmi_response(code=0xC1)

    # =====

    def __get_power_state_handler(self, _: Dict, session: IpmiServerSession) -> None:
        # https://github.com/arcress0/ipmiutil/blob/e2f6e95127d22e555f959f136d9bb9543c763896/util/ireset.c#L654
        result = self.__make_request(session, "atx.get_state() [power]", "atx.get_state")
        data = [(0 if result["leds"]["power"] else 5)]
        session.send_ipmi_response(data=data)

    def __get_selftest_status_handler(self, _: Dict, session: IpmiServerSession) -> None:
        # https://github.com/arcress0/ipmiutil/blob/e2f6e95127d22e555f959f136d9bb9543c763896/util/ihealth.c#L858
        data = [0x0055]
        try:
            self.__make_request(session, "atx.get_state() [health]", "atx.get_state")
        except Exception:
            data = [0]
        session.send_ipmi_response(data=data)

    def __get_chassis_status_handler(self, _: Dict, session: IpmiServerSession) -> None:
        result = self.__make_request(session, "atx.get_state() [chassis]", "atx.get_state")
        data = [int(result["leds"]["power"]), 0, 0]
        session.send_ipmi_response(data=data)

    def __chassis_control_handler(self, request: Dict, session: IpmiServerSession) -> None:
        action = {
            0: "off_hard",
            1: "on",
            3: "reset_hard",
            5: "off",
        }.get(request["data"][0], "")
        if action:
            if not self.__make_request(session, f"atx.switch_power({action})", "atx.switch_power", action=action):
                code = 0xC0  # Try again later
            else:
                code = 0
        else:
            code = 0xCC  # Invalid request
        session.send_ipmi_response(code=code)

    def __make_request(self, session: IpmiServerSession, name: str, func_path: str, **kwargs):  # type: ignore
        async def runner():  # type: ignore
            logger = get_logger(0)
            credentials = self.__auth_manager.get_credentials(session.username.decode())
            logger.info("[%s]: Performing request %s from user %r (IPMI) as %r (KVMD)",
                        session.sockaddr[0], name, credentials.ipmi_user, credentials.kvmd_user)
            try:
                async with self.__kvmd.make_session(credentials.kvmd_user, credentials.kvmd_passwd) as kvmd_session:
                    func = functools.reduce(getattr, func_path.split("."), kvmd_session)
                    return (await func(**kwargs))
            except (aiohttp.ClientError, asyncio.TimeoutError) as err:
                logger.error("[%s]: Can't perform request %s: %s", session.sockaddr[0], name, err)
                raise

        return aiotools.run_sync(runner())

    # =====

    def __activate_sol_handler(self, _: Dict, session: IpmiServerSession) -> None:
        with self.__sol_lock:
            if not self.__sol_device_path:
                session.send_ipmi_response(code=0x81)  # SOL disabled
            elif not os.access(self.__sol_device_path, os.R_OK | os.W_OK):
                get_logger(0).info("Can't activate SOL because %s is unavailable", self.__sol_device_path)
                session.send_ipmi_response(code=0x81)  # SOL disabled
            elif self.__is_sol_activated():
                session.send_ipmi_response(code=0x80)  # Already activated
            else:
                get_logger(0).info("Activating SOL ...")
                self.__stop_sol_worker()  # Join if dead
                session.send_ipmi_response(data=[
                    0, 0, 0, 0, 1, 0, 1, 0,
                    (self.__sol_proxy_port >> 8 & 0xFF), (self.__sol_proxy_port & 0xFF),
                    0xFF, 0xFF,
                ])
                self.__start_sol_worker(session)

    def __deactivate_sol_handler(self, _: Dict, session: IpmiServerSession) -> None:
        with self.__sol_lock:
            if not self.__sol_device_path:
                session.send_ipmi_response(code=0x81)
            elif not self.__is_sol_activated():
                session.send_ipmi_response(code=0x80)
            else:
                get_logger(0).info("Deactivating SOL ...")
                self.__stop_sol_worker()

    def __is_sol_activated(self) -> bool:
        return (self.__sol_thread is not None and self.__sol_thread.is_alive())

    def __start_sol_worker(self, session: IpmiServerSession) -> None:
        assert self.__sol_console is None
        assert self.__sol_thread is None
        user_queue: "multiprocessing.Queue[bytes]" = multiprocessing.Queue()  # Only for select()
        self.__sol_console = IpmiConsole(session, user_queue.put_nowait)
        self.__sol_thread = threading.Thread(target=self.__sol_worker, args=(user_queue,), daemon=True)
        self.__sol_thread.start()

    def __stop_sol_worker(self) -> None:
        if self.__sol_thread is not None:
            if self.__sol_thread.is_alive():
                self.__sol_stop = True
            self.__sol_thread.join()
            self.__sol_stop = False
            self.__sol_thread = None
        self.__close_sol_console()

    def __close_sol_console(self) -> None:
        if self.__sol_console is not None:
            self.__sol_console.close()
            self.__sol_console = None
            get_logger(0).info("SOL closed")

    def __sol_worker(self, user_queue: "multiprocessing.Queue[bytes]") -> None:
        logger = get_logger(0)
        logger.info("Starting SOL worker ...")
        try:
            assert self.__sol_console is not None
            with serial.Serial(self.__sol_device_path, self.__sol_speed) as tty:
                logger.info("Opened SOL port %s at speed=%d", self.__sol_device_path, self.__sol_speed)
                qr = user_queue._reader  # type: ignore  # pylint: disable=protected-access
                try:
                    while not self.__sol_stop:
                        ready = select.select([qr, tty], [], [], self.__sol_select_timeout)[0]
                        if qr in ready:
                            data = b""
                            for _ in range(user_queue.qsize()):  # Don't hold on this with [not empty()]
                                try:
                                    data += user_queue.get_nowait()
                                except queue.Empty:
                                    break
                            if data:
                                tty.write(data)
                        if tty in ready:
                            self.__sol_console.send_data(tty.read_all())
                finally:
                    logger.info("Closed SOL port %s", self.__sol_device_path)
        except Exception:
            logger.exception("SOL worker error")
            self.__close_sol_console()
        finally:
            logger.info("SOL worker finished")