summaryrefslogtreecommitdiffstats
path: root/oemrouter.cpp
blob: 273a67947f154c63dd8b26defdd7407b4819dd31 (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
#include <cstdio>
#include <cstring>
#include <map>
#include <utility>

#include "host-ipmid/oemrouter.hpp"

namespace oem
{

using Key = std::pair<Number, ipmi_cmd_t>;

// Private implementation of OemRouter Interface.
class RouterImpl : public Router
{
    public:
        RouterImpl() {}

        // Implement OemRouter Interface.
        void activate() override;
        void registerHandler(Number oen, ipmi_cmd_t cmd,
                             Handler handler) override;

        // Actual message routing function.
        ipmi_ret_t routeMsg(ipmi_cmd_t cmd, const uint8_t* reqBuf,
                            uint8_t* replyBuf, size_t* dataLen);

    private:
        std::map<Key, Handler> handlers;
};

// Static global instance for simplicity.
static RouterImpl* globalRouterImpl;

// TODO Refactor ipmid to avoid need for singleton here.
Router* mutableRouter()
{
    if (!globalRouterImpl)
    {
        globalRouterImpl = new RouterImpl;
    }
    return globalRouterImpl;
}

ipmi_ret_t RouterImpl::routeMsg(ipmi_cmd_t cmd, const uint8_t* reqBuf,
                                uint8_t* replyBuf, size_t* dataLen)
{
    // Not entirely clear we can route reply without complete OEM group.
    // TODO: consider adding a way to suppress malformed replies.
    if (*dataLen < groupMagicSize)
    {
        fprintf(stderr, "NetFn:[0x2E], OEM:[%zu bytes?], Cmd:[%#04X]\n",
                *dataLen, cmd);
        (*dataLen) = 0;
        return IPMI_CC_REQ_DATA_LEN_INVALID;
    }

    // Find registered handler or reject request.
    auto number = toOemNumber(reqBuf);
    auto cmdKey = std::make_pair(number, cmd);

    auto iter = handlers.find(cmdKey);
    if (iter == handlers.end())
    {
        auto wildKey = std::make_pair(number, IPMI_CMD_WILDCARD);
        iter = handlers.find(wildKey);
        if (iter == handlers.end())
        {
            fprintf(stderr, "No Registered handler for NetFn:[0x2E], "
                    "OEM:[%#08X], Cmd:[%#04X]\n", number, cmd);
            *dataLen = groupMagicSize;
            return IPMI_CC_INVALID;
        }
#ifdef __IPMI_DEBUG__
        fprintf(stderr, "Wildcard NetFn:[0x2E], OEM:[%#08X], Cmd:[%#04X]\n",
                number, cmd);
#endif
    }
    else
    {
#ifdef __IPMI_DEBUG__
        fprintf(stderr, "Match NetFn:[0x2E], OEM:[%#08X], Cmd:[%#04X]\n",
                number, cmd);
#endif
    }

    // Copy OEMGroup here, by analogy to IPMI CC code at netfn router;
    // OemHandler should deal only with optional following data bytes.
    std::memcpy(replyBuf, reqBuf, groupMagicSize);

    size_t oemDataLen = *dataLen - groupMagicSize;
    Handler& handler = iter->second;

    auto rc = handler(cmd, reqBuf + groupMagicSize,
                      replyBuf + groupMagicSize, &oemDataLen);

    // Add OEMGroup bytes to nominal reply.
    *dataLen = oemDataLen + groupMagicSize;
    return rc;
}

// Function suitable for use as ipmi_netfn_router() call-back.
// Translates call-back pointer args to more specific types.
ipmi_ret_t ipmi_oem_wildcard_handler(ipmi_netfn_t /* netfn */,
                                     ipmi_cmd_t cmd, ipmi_request_t request,
                                     ipmi_response_t response,
                                     ipmi_data_len_t dataLen,
                                     ipmi_context_t context)
{
    // View requests & responses as byte sequences.
    const uint8_t* reqBuf = static_cast<uint8_t*>(request);
    uint8_t* replyBuf = static_cast<uint8_t*>(response);

    // View context as router object, defaulting nullptr to global object.
    auto router = static_cast<RouterImpl*>(
            context ? context : mutableRouter());

    // Send message parameters to dispatcher.
    return router->routeMsg(cmd, reqBuf, replyBuf, dataLen);
}

// Enable message routing to begin.
void RouterImpl::activate()
{
    // Register netfn 0x2e OEM Group, any (wildcard) command.
    printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
           NETFUN_OEM_GROUP, IPMI_CMD_WILDCARD);
    ipmi_register_callback(NETFUN_OEM_GROUP, IPMI_CMD_WILDCARD, this,
                           ipmi_oem_wildcard_handler, PRIVILEGE_OEM);
}

void RouterImpl::registerHandler(Number oen, ipmi_cmd_t cmd,
                                 Handler handler)
{
    auto cmdKey = std::make_pair(oen, cmd);
    auto iter = handlers.find(cmdKey);
    if (iter == handlers.end())
    {
        // Add handler if key not already taken.
        handlers.emplace(cmdKey, handler);
    }
    else
    {
        fprintf(stderr, "ERROR : Duplicate registration for NetFn:[0x2E], "
                "OEM:[%#08X], Cmd:[%#04X]\n", oen, cmd);
    }
}

}  // namespace oem
OpenPOWER on IntegriCloud