-
Notifications
You must be signed in to change notification settings - Fork 75
/
dcmihandler.cpp
1245 lines (1101 loc) · 39.7 KB
/
dcmihandler.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.h"
#include "dcmihandler.hpp"
#include "user_channel/channel_layer.hpp"
#include <ipmid/api.hpp>
#include <ipmid/utils.hpp>
#include <nlohmann/json.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
#include <bitset>
#include <cmath>
#include <fstream>
#include <variant>
using namespace phosphor::logging;
using sdbusplus::server::xyz::openbmc_project::network::EthernetInterface;
using InternalFailure =
sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
void register_netfn_dcmi_functions() __attribute__((constructor));
constexpr auto pcapPath = "/xyz/openbmc_project/control/host0/power_cap";
constexpr auto pcapInterface = "xyz.openbmc_project.Control.Power.Cap";
constexpr auto powerCapProp = "PowerCap";
constexpr auto powerCapEnableProp = "PowerCapEnable";
using namespace phosphor::logging;
namespace dcmi
{
constexpr auto assetTagMaxOffset = 62;
constexpr auto assetTagMaxSize = 63;
constexpr auto maxBytes = 16;
constexpr size_t maxCtrlIdStrLen = 63;
constexpr uint8_t parameterRevision = 2;
constexpr uint8_t specMajorVersion = 1;
constexpr uint8_t specMinorVersion = 5;
constexpr auto sensorValueIntf = "xyz.openbmc_project.Sensor.Value";
constexpr auto sensorValueProp = "Value";
constexpr uint8_t configParameterRevision = 1;
constexpr auto option12Mask = 0x01;
constexpr auto activateDhcpReply = 0x00;
constexpr uint8_t dhcpTiming1 = 0x04; // 4 sec
constexpr uint16_t dhcpTiming2 = 0x78; // 120 sec
constexpr uint16_t dhcpTiming3 = 0x40; // 60 sec
// When DHCP Option 12 is enabled the string "SendHostName=true" will be
// added into n/w configuration file and the parameter
// SendHostNameEnabled will set to true.
constexpr auto dhcpOpt12Enabled = "SendHostNameEnabled";
enum class DCMIConfigParameters : uint8_t
{
ActivateDHCP = 1,
DiscoveryConfig,
DHCPTiming1,
DHCPTiming2,
DHCPTiming3,
};
// Refer Table 6-14, DCMI Entity ID Extension, DCMI v1.5 spec
static const std::map<uint8_t, std::string> entityIdToName{
{0x40, "inlet"}, {0x37, "inlet"}, {0x41, "cpu"},
{0x03, "cpu"}, {0x42, "baseboard"}, {0x07, "baseboard"}};
nlohmann::json parseJSONConfig(const std::string& configFile)
{
std::ifstream jsonFile(configFile);
if (!jsonFile.is_open())
{
lg2::error("Temperature readings JSON file not found");
elog<InternalFailure>();
}
auto data = nlohmann::json::parse(jsonFile, nullptr, false);
if (data.is_discarded())
{
lg2::error("Temperature readings JSON parser failure");
elog<InternalFailure>();
}
return data;
}
bool isDCMIPowerMgmtSupported()
{
static bool parsed = false;
static bool supported = false;
if (!parsed)
{
auto data = parseJSONConfig(gDCMICapabilitiesConfig);
supported = (gDCMIPowerMgmtSupported ==
data.value(gDCMIPowerMgmtCapability, 0));
}
return supported;
}
std::optional<uint32_t> getPcap(ipmi::Context::ptr& ctx)
{
std::string service{};
boost::system::error_code ec =
ipmi::getService(ctx, pcapInterface, pcapPath, service);
if (ec.value())
{
return std::nullopt;
}
uint32_t pcap{};
ec = ipmi::getDbusProperty(ctx, service, pcapPath, pcapInterface,
powerCapProp, pcap);
if (ec.value())
{
lg2::error("Error in getPcap prop: {ERROR}", "ERROR", ec.message());
elog<InternalFailure>();
return std::nullopt;
}
return pcap;
}
std::optional<bool> getPcapEnabled(ipmi::Context::ptr& ctx)
{
std::string service{};
boost::system::error_code ec =
ipmi::getService(ctx, pcapInterface, pcapPath, service);
if (ec.value())
{
return std::nullopt;
}
bool pcapEnabled{};
ec = ipmi::getDbusProperty(ctx, service, pcapPath, pcapInterface,
powerCapEnableProp, pcapEnabled);
if (ec.value())
{
lg2::error("Error in getPcap prop");
elog<InternalFailure>();
return std::nullopt;
}
return pcapEnabled;
}
bool setPcap(ipmi::Context::ptr& ctx, const uint32_t powerCap)
{
std::string service{};
boost::system::error_code ec =
ipmi::getService(ctx, pcapInterface, pcapPath, service);
if (ec.value())
{
return false;
}
ec = ipmi::setDbusProperty(ctx, service, pcapPath, pcapInterface,
powerCapProp, powerCap);
if (ec.value())
{
lg2::error("Error in setPcap property: {ERROR}", "ERROR", ec.message());
elog<InternalFailure>();
return false;
}
return true;
}
bool setPcapEnable(ipmi::Context::ptr& ctx, bool enabled)
{
std::string service{};
boost::system::error_code ec =
ipmi::getService(ctx, pcapInterface, pcapPath, service);
if (ec.value())
{
return false;
}
ec = ipmi::setDbusProperty(ctx, service, pcapPath, pcapInterface,
powerCapEnableProp, enabled);
if (ec.value())
{
lg2::error("Error in setPcapEnabled property: {ERROR}", "ERROR",
ec.message());
elog<InternalFailure>();
return false;
}
return true;
}
std::optional<std::string> readAssetTag(ipmi::Context::ptr& ctx)
{
// Read the object tree with the inventory root to figure out the object
// that has implemented the Asset tag interface.
ipmi::DbusObjectInfo objectInfo;
boost::system::error_code ec = getDbusObject(
ctx, dcmi::assetTagIntf, ipmi::sensor::inventoryRoot, "", objectInfo);
if (ec.value())
{
return std::nullopt;
}
std::string assetTag{};
ec =
ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first,
dcmi::assetTagIntf, dcmi::assetTagProp, assetTag);
if (ec.value())
{
lg2::error("Error in reading asset tag: {ERROR}", "ERROR",
ec.message());
elog<InternalFailure>();
return std::nullopt;
}
return assetTag;
}
bool writeAssetTag(ipmi::Context::ptr& ctx, const std::string& assetTag)
{
// Read the object tree with the inventory root to figure out the object
// that has implemented the Asset tag interface.
ipmi::DbusObjectInfo objectInfo;
boost::system::error_code ec = getDbusObject(
ctx, dcmi::assetTagIntf, ipmi::sensor::inventoryRoot, "", objectInfo);
if (ec.value())
{
return false;
}
ec =
ipmi::setDbusProperty(ctx, objectInfo.second, objectInfo.first,
dcmi::assetTagIntf, dcmi::assetTagProp, assetTag);
if (ec.value())
{
lg2::error("Error in writing asset tag: {ERROR}", "ERROR",
ec.message());
elog<InternalFailure>();
return false;
}
return true;
}
std::optional<std::string> getHostName(ipmi::Context::ptr& ctx)
{
std::string service{};
boost::system::error_code ec =
ipmi::getService(ctx, networkConfigIntf, networkConfigObj, service);
if (ec.value())
{
return std::nullopt;
}
std::string hostname{};
ec = ipmi::getDbusProperty(ctx, service, networkConfigObj,
networkConfigIntf, hostNameProp, hostname);
if (ec.value())
{
lg2::error("Error fetching hostname");
elog<InternalFailure>();
return std::nullopt;
}
return hostname;
}
std::optional<EthernetInterface::DHCPConf>
getDHCPEnabled(ipmi::Context::ptr& ctx)
{
auto ethdevice = ipmi::getChannelName(ethernetDefaultChannelNum);
ipmi::DbusObjectInfo ethernetObj{};
boost::system::error_code ec = ipmi::getDbusObject(
ctx, ethernetIntf, networkRoot, ethdevice, ethernetObj);
if (ec.value())
{
return std::nullopt;
}
std::string service{};
ec = ipmi::getService(ctx, ethernetIntf, ethernetObj.first, service);
if (ec.value())
{
return std::nullopt;
}
std::string dhcpVal{};
ec = ipmi::getDbusProperty(ctx, service, ethernetObj.first, ethernetIntf,
"DHCPEnabled", dhcpVal);
if (ec.value())
{
return std::nullopt;
}
return EthernetInterface::convertDHCPConfFromString(dhcpVal);
}
std::optional<bool> getDHCPOption(ipmi::Context::ptr& ctx,
const std::string& prop)
{
ipmi::ObjectTree objectTree;
if (ipmi::getAllDbusObjects(ctx, networkRoot, dhcpIntf, objectTree))
{
return std::nullopt;
}
for (const auto& [path, serviceMap] : objectTree)
{
for (const auto& [service, object] : serviceMap)
{
bool value{};
if (ipmi::getDbusProperty(ctx, service, path, dhcpIntf, prop,
value))
{
return std::nullopt;
}
if (value)
{
return true;
}
}
}
return false;
}
bool setDHCPOption(ipmi::Context::ptr& ctx, std::string prop, bool value)
{
ipmi::ObjectTree objectTree;
if (ipmi::getAllDbusObjects(ctx, networkRoot, dhcpIntf, objectTree))
{
return false;
}
for (const auto& [path, serviceMap] : objectTree)
{
for (const auto& [service, object] : serviceMap)
{
if (ipmi::setDbusProperty(ctx, service, path, dhcpIntf, prop,
value))
{
return false;
}
}
}
return true;
}
} // namespace dcmi
constexpr uint8_t exceptionPowerOff = 0x01;
ipmi::RspType<uint16_t, // reserved
uint8_t, // exception actions
uint16_t, // power limit requested in watts
uint32_t, // correction time in milliseconds
uint16_t, // reserved
uint16_t // statistics sampling period in seconds
>
getPowerLimit(ipmi::Context::ptr ctx, uint16_t reserved)
{
if (!dcmi::isDCMIPowerMgmtSupported())
{
return ipmi::responseInvalidCommand();
}
if (reserved)
{
return ipmi::responseInvalidFieldRequest();
}
std::optional<uint16_t> pcapValue = dcmi::getPcap(ctx);
std::optional<bool> pcapEnable = dcmi::getPcapEnabled(ctx);
if (!pcapValue || !pcapEnable)
{
return ipmi::responseUnspecifiedError();
}
constexpr uint16_t reserved1{};
constexpr uint16_t reserved2{};
/*
* Exception action if power limit is exceeded and cannot be controlled
* with the correction time limit is hardcoded to Hard Power Off system
* and log event to SEL.
*/
constexpr uint8_t exception = exceptionPowerOff;
/*
* Correction time limit and Statistics sampling period is currently not
* populated.
*/
constexpr uint32_t correctionTime{};
constexpr uint16_t statsPeriod{};
if (*pcapEnable == false)
{
constexpr ipmi::Cc responseNoPowerLimitSet = 0x80;
return ipmi::response(responseNoPowerLimitSet, reserved1, exception,
*pcapValue, correctionTime, reserved2,
statsPeriod);
}
return ipmi::responseSuccess(reserved1, exception, *pcapValue,
correctionTime, reserved2, statsPeriod);
}
ipmi::RspType<> setPowerLimit(ipmi::Context::ptr& ctx, uint16_t reserved1,
uint8_t reserved2, uint8_t exceptionAction,
uint16_t powerLimit, uint32_t correctionTime,
uint16_t reserved3, uint16_t statsPeriod)
{
if (!dcmi::isDCMIPowerMgmtSupported())
{
lg2::error("DCMI Power management is unsupported!");
return ipmi::responseInvalidCommand();
}
// Only process the power limit requested in watts. Return errors
// for other fields that are set
if (reserved1 || reserved2 || reserved3 || correctionTime || statsPeriod ||
exceptionAction != exceptionPowerOff)
{
return ipmi::responseInvalidFieldRequest();
}
if (!dcmi::setPcap(ctx, powerLimit))
{
return ipmi::responseUnspecifiedError();
}
lg2::info("Set Power Cap: {POWERCAP}", "POWERCAP", powerLimit);
return ipmi::responseSuccess();
}
ipmi::RspType<> applyPowerLimit(ipmi::Context::ptr& ctx, bool enabled,
uint7_t reserved1, uint16_t reserved2)
{
if (!dcmi::isDCMIPowerMgmtSupported())
{
lg2::error("DCMI Power management is unsupported!");
return ipmi::responseInvalidCommand();
}
if (reserved1 || reserved2)
{
return ipmi::responseInvalidFieldRequest();
}
if (!dcmi::setPcapEnable(ctx, enabled))
{
return ipmi::responseUnspecifiedError();
}
lg2::info("Set Power Cap Enable: {POWERCAPENABLE}", "POWERCAPENABLE",
enabled);
return ipmi::responseSuccess();
}
ipmi::RspType<uint8_t, // total tag length
std::vector<char> // tag data
>
getAssetTag(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count)
{
// Verify offset to read and number of bytes to read are not exceeding
// the range.
if ((offset > dcmi::assetTagMaxOffset) || (count > dcmi::maxBytes) ||
((offset + count) > dcmi::assetTagMaxSize))
{
return ipmi::responseParmOutOfRange();
}
std::optional<std::string> assetTagResp = dcmi::readAssetTag(ctx);
if (!assetTagResp)
{
return ipmi::responseUnspecifiedError();
}
std::string& assetTag = assetTagResp.value();
// If the asset tag is longer than 63 bytes, restrict it to 63 bytes to
// suit Get Asset Tag command.
if (assetTag.size() > dcmi::assetTagMaxSize)
{
assetTag.resize(dcmi::assetTagMaxSize);
}
if (offset >= assetTag.size())
{
return ipmi::responseParmOutOfRange();
}
// silently truncate reads beyond the end of assetTag
if ((offset + count) >= assetTag.size())
{
count = assetTag.size() - offset;
}
auto totalTagSize = static_cast<uint8_t>(assetTag.size());
std::vector<char> data{assetTag.begin() + offset,
assetTag.begin() + offset + count};
return ipmi::responseSuccess(totalTagSize, data);
}
ipmi::RspType<uint8_t // new asset tag length
>
setAssetTag(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count,
const std::vector<char>& data)
{
// Verify offset to read and number of bytes to read are not exceeding
// the range.
if ((offset > dcmi::assetTagMaxOffset) || (count > dcmi::maxBytes) ||
((offset + count) > dcmi::assetTagMaxSize))
{
return ipmi::responseParmOutOfRange();
}
if (data.size() != count)
{
return ipmi::responseReqDataLenInvalid();
}
std::optional<std::string> assetTagResp = dcmi::readAssetTag(ctx);
if (!assetTagResp)
{
return ipmi::responseUnspecifiedError();
}
std::string& assetTag = assetTagResp.value();
if (offset > assetTag.size())
{
return ipmi::responseParmOutOfRange();
}
// operation is to truncate at offset and append new data
assetTag.resize(offset);
assetTag.append(data.begin(), data.end());
if (!dcmi::writeAssetTag(ctx, assetTag))
{
return ipmi::responseUnspecifiedError();
}
auto totalTagSize = static_cast<uint8_t>(assetTag.size());
return ipmi::responseSuccess(totalTagSize);
}
ipmi::RspType<uint8_t, // length
std::vector<char> // data
>
getMgmntCtrlIdStr(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count)
{
if (count > dcmi::maxBytes || offset + count > dcmi::maxCtrlIdStrLen)
{
return ipmi::responseParmOutOfRange();
}
std::optional<std::string> hostnameResp = dcmi::getHostName(ctx);
if (!hostnameResp)
{
return ipmi::responseUnspecifiedError();
}
std::string& hostname = hostnameResp.value();
// If the id string is longer than 63 bytes, restrict it to 63 bytes to
// suit set management ctrl str command.
if (hostname.size() > dcmi::maxCtrlIdStrLen)
{
hostname.resize(dcmi::maxCtrlIdStrLen);
}
if (offset >= hostname.size())
{
return ipmi::responseParmOutOfRange();
}
// silently truncate reads beyond the end of hostname
if ((offset + count) >= hostname.size())
{
count = hostname.size() - offset;
}
auto nameSize = static_cast<uint8_t>(hostname.size());
std::vector<char> data{hostname.begin() + offset,
hostname.begin() + offset + count};
return ipmi::responseSuccess(nameSize, data);
}
ipmi::RspType<uint8_t>
setMgmntCtrlIdStr(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count,
std::vector<char> data)
{
if ((offset > dcmi::maxCtrlIdStrLen) || (count > dcmi::maxBytes) ||
((offset + count) > dcmi::maxCtrlIdStrLen))
{
return ipmi::responseParmOutOfRange();
}
if (data.size() != count)
{
return ipmi::responseReqDataLenInvalid();
}
bool terminalWrite{data.back() == '\0'};
if (terminalWrite)
{
// remove the null termination from the data (no need with std::string)
data.resize(count - 1);
}
static std::string hostname{};
// read in the current value if not starting at offset 0
if (hostname.size() == 0 && offset != 0)
{
/* read old ctrlIdStr */
std::optional<std::string> hostnameResp = dcmi::getHostName(ctx);
if (!hostnameResp)
{
return ipmi::responseUnspecifiedError();
}
hostname = hostnameResp.value();
hostname.resize(offset);
}
// operation is to truncate at offset and append new data
hostname.append(data.begin(), data.end());
// do the update if this is the last write
if (terminalWrite)
{
boost::system::error_code ec = ipmi::setDbusProperty(
ctx, dcmi::networkServiceName, dcmi::networkConfigObj,
dcmi::networkConfigIntf, dcmi::hostNameProp, hostname);
hostname.clear();
if (ec.value())
{
return ipmi::responseUnspecifiedError();
}
}
auto totalIdSize = static_cast<uint8_t>(offset + count);
return ipmi::responseSuccess(totalIdSize);
}
ipmi::RspType<ipmi::message::Payload> getDCMICapabilities(uint8_t parameter)
{
std::ifstream dcmiCapFile(dcmi::gDCMICapabilitiesConfig);
if (!dcmiCapFile.is_open())
{
lg2::error("DCMI Capabilities file not found");
return ipmi::responseUnspecifiedError();
}
auto data = nlohmann::json::parse(dcmiCapFile, nullptr, false);
if (data.is_discarded())
{
lg2::error("DCMI Capabilities JSON parser failure");
return ipmi::responseUnspecifiedError();
}
constexpr bool reserved1{};
constexpr uint5_t reserved5{};
constexpr uint7_t reserved7{};
constexpr uint8_t reserved8{};
constexpr uint16_t reserved16{};
ipmi::message::Payload payload;
payload.pack(dcmi::specMajorVersion, dcmi::specMinorVersion,
dcmi::parameterRevision);
enum class DCMICapParameters : uint8_t
{
SupportedDcmiCaps = 0x01, // Supported DCMI Capabilities
MandatoryPlatAttributes = 0x02, // Mandatory Platform Attributes
OptionalPlatAttributes = 0x03, // Optional Platform Attributes
ManageabilityAccessAttributes = 0x04, // Manageability Access Attributes
};
switch (static_cast<DCMICapParameters>(parameter))
{
case DCMICapParameters::SupportedDcmiCaps:
{
bool powerManagement = data.value("PowerManagement", 0);
bool oobSecondaryLan = data.value("OOBSecondaryLan", 0);
bool serialTMode = data.value("SerialTMODE", 0);
bool inBandSystemInterfaceChannel =
data.value("InBandSystemInterfaceChannel", 0);
payload.pack(reserved8, powerManagement, reserved7,
inBandSystemInterfaceChannel, serialTMode,
oobSecondaryLan, reserved5);
break;
}
// Mandatory Platform Attributes
case DCMICapParameters::MandatoryPlatAttributes:
{
bool selAutoRollOver = data.value("SELAutoRollOver", 0);
bool flushEntireSELUponRollOver =
data.value("FlushEntireSELUponRollOver", 0);
bool recordLevelSELFlushUponRollOver =
data.value("RecordLevelSELFlushUponRollOver", 0);
uint12_t numberOfSELEntries =
data.value("NumberOfSELEntries", 0xcac);
uint8_t tempMonitoringSamplingFreq =
data.value("TempMonitoringSamplingFreq", 0);
payload.pack(numberOfSELEntries, reserved1,
recordLevelSELFlushUponRollOver,
flushEntireSELUponRollOver, selAutoRollOver,
reserved16, tempMonitoringSamplingFreq);
break;
}
// Optional Platform Attributes
case DCMICapParameters::OptionalPlatAttributes:
{
uint7_t powerMgmtDeviceTargetAddress =
data.value("PowerMgmtDeviceSlaveAddress", 0);
uint4_t bmcChannelNumber = data.value("BMCChannelNumber", 0);
uint4_t deviceRivision = data.value("DeviceRivision", 0);
payload.pack(powerMgmtDeviceTargetAddress, reserved1,
deviceRivision, bmcChannelNumber);
break;
}
// Manageability Access Attributes
case DCMICapParameters::ManageabilityAccessAttributes:
{
uint8_t mandatoryPrimaryLanOOBSupport =
data.value("MandatoryPrimaryLanOOBSupport", 0xff);
uint8_t optionalSecondaryLanOOBSupport =
data.value("OptionalSecondaryLanOOBSupport", 0xff);
uint8_t optionalSerialOOBMTMODECapability =
data.value("OptionalSerialOOBMTMODECapability", 0xff);
payload.pack(mandatoryPrimaryLanOOBSupport,
optionalSecondaryLanOOBSupport,
optionalSerialOOBMTMODECapability);
break;
}
default:
{
lg2::error("Invalid input parameter");
return ipmi::responseInvalidFieldRequest();
}
}
return ipmi::responseSuccess(payload);
}
namespace dcmi
{
namespace temp_readings
{
std::tuple<bool, bool, uint8_t>
readTemp(ipmi::Context::ptr& ctx, const std::string& dbusService,
const std::string& dbusPath)
{
// Read the temperature value from d-bus object. Need some conversion.
// As per the interface xyz.openbmc_project.Sensor.Value, the
// temperature is an double and in degrees C. It needs to be scaled by
// using the formula Value * 10^Scale. The ipmi spec has the temperature
// as a uint8_t, with a separate single bit for the sign.
ipmi::PropertyMap result{};
boost::system::error_code ec = ipmi::getAllDbusProperties(
ctx, dbusService, dbusPath, "xyz.openbmc_project.Sensor.Value", result);
if (ec.value())
{
return std::make_tuple(false, false, 0);
}
auto temperature =
std::visit(ipmi::VariantToDoubleVisitor(), result.at("Value"));
double absTemp = std::abs(temperature);
auto findFactor = result.find("Scale");
double factor = 0.0;
if (findFactor != result.end())
{
factor = std::visit(ipmi::VariantToDoubleVisitor(), findFactor->second);
}
double scale = std::pow(10, factor);
auto tempDegrees = absTemp * scale;
// Max absolute temp as per ipmi spec is 127.
constexpr auto maxTemp = 127;
if (tempDegrees > maxTemp)
{
tempDegrees = maxTemp;
}
return std::make_tuple(true, (temperature < 0),
static_cast<uint8_t>(tempDegrees));
}
std::tuple<std::vector<std::tuple<uint7_t, bool, uint8_t>>, uint8_t>
read(ipmi::Context::ptr& ctx, const std::string& type, uint8_t instance,
size_t count)
{
std::vector<std::tuple<uint7_t, bool, uint8_t>> response{};
auto data = parseJSONConfig(gDCMISensorsConfig);
static const std::vector<nlohmann::json> empty{};
std::vector<nlohmann::json> readings = data.value(type, empty);
for (const auto& j : readings)
{
// Max of 8 response data sets
if (response.size() == count)
{
break;
}
uint8_t instanceNum = j.value("instance", 0);
// Not in the instance range we're interested in
if (instanceNum < instance)
{
continue;
}
std::string path = j.value("dbus", "");
std::string service{};
boost::system::error_code ec = ipmi::getService(
ctx, "xyz.openbmc_project.Sensor.Value", path, service);
if (ec.value())
{
// not found on dbus
continue;
}
const auto& [ok, sign, temp] = readTemp(ctx, service, path);
if (ok)
{
response.emplace_back(uint7_t{temp}, sign, instanceNum);
}
}
auto totalInstances =
static_cast<uint8_t>(std::min(readings.size(), maxInstances));
return std::make_tuple(response, totalInstances);
}
} // namespace temp_readings
} // namespace dcmi
ipmi::RspType<uint8_t, // total instances for entity id
uint8_t, // number of instances in this reply
std::vector< // zero or more of the following two bytes
std::tuple<uint7_t, // temperature value
bool, // sign bit
uint8_t // entity instance
>>>
getTempReadings(ipmi::Context::ptr& ctx, uint8_t sensorType,
uint8_t entityId, uint8_t entityInstance,
uint8_t instanceStart)
{
auto it = dcmi::entityIdToName.find(entityId);
if (it == dcmi::entityIdToName.end())
{
lg2::error("Unknown Entity ID: {ENTITY_ID}", "ENTITY_ID", entityId);
return ipmi::responseInvalidFieldRequest();
}
if (sensorType != dcmi::temperatureSensorType)
{
lg2::error("Invalid sensor type: {SENSOR_TYPE}", "SENSOR_TYPE",
sensorType);
return ipmi::responseInvalidFieldRequest();
}
uint8_t requestedRecords = (entityInstance == 0) ? dcmi::maxRecords : 1;
// Read requested instances
const auto& [temps, totalInstances] = dcmi::temp_readings::read(
ctx, it->second, instanceStart, requestedRecords);
auto numInstances = static_cast<uint8_t>(temps.size());
return ipmi::responseSuccess(totalInstances, numInstances, temps);
}
ipmi::RspType<> setDCMIConfParams(ipmi::Context::ptr& ctx, uint8_t parameter,
uint8_t setSelector,
ipmi::message::Payload& payload)
{
if (setSelector)
{
return ipmi::responseInvalidFieldRequest();
}
// Take action based on the Parameter Selector
switch (static_cast<dcmi::DCMIConfigParameters>(parameter))
{
case dcmi::DCMIConfigParameters::ActivateDHCP:
{
uint7_t reserved{};
bool activate{};
if (payload.unpack(activate, reserved) || !payload.fullyUnpacked())
{
return ipmi::responseReqDataLenInvalid();
}
if (reserved)
{
return ipmi::responseInvalidFieldRequest();
}
std::optional<EthernetInterface::DHCPConf> dhcpEnabled =
dcmi::getDHCPEnabled(ctx);
if (!dhcpEnabled)
{
return ipmi::responseUnspecifiedError();
}
if (activate &&
(dhcpEnabled.value() != EthernetInterface::DHCPConf::none))
{
// When these conditions are met we have to trigger DHCP
// protocol restart using the latest parameter settings,
// but as per n/w manager design, each time when we
// update n/w parameters, n/w service is restarted. So
// we no need to take any action in this case.
}
break;
}
case dcmi::DCMIConfigParameters::DiscoveryConfig:
{
bool option12{};
uint6_t reserved1{};
bool randBackOff{};
if (payload.unpack(option12, reserved1, randBackOff) ||
!payload.fullyUnpacked())
{
return ipmi::responseReqDataLenInvalid();
}
// Systemd-networkd doesn't support Random Back off
if (reserved1 || randBackOff)
{
return ipmi::responseInvalidFieldRequest();
}
dcmi::setDHCPOption(ctx, dcmi::dhcpOpt12Enabled, option12);
break;
}
// Systemd-networkd doesn't allow to configure DHCP timigs
case dcmi::DCMIConfigParameters::DHCPTiming1:
case dcmi::DCMIConfigParameters::DHCPTiming2:
case dcmi::DCMIConfigParameters::DHCPTiming3:
default:
return ipmi::responseInvalidFieldRequest();
}
return ipmi::responseSuccess();
}
ipmi::RspType<ipmi::message::Payload> getDCMIConfParams(
ipmi::Context::ptr& ctx, uint8_t parameter, uint8_t setSelector)
{
if (setSelector)
{
return ipmi::responseInvalidFieldRequest();
}
ipmi::message::Payload payload;
payload.pack(dcmi::specMajorVersion, dcmi::specMinorVersion,
dcmi::configParameterRevision);
// Take action based on the Parameter Selector
switch (static_cast<dcmi::DCMIConfigParameters>(parameter))
{
case dcmi::DCMIConfigParameters::ActivateDHCP:
payload.pack(dcmi::activateDhcpReply);
break;
case dcmi::DCMIConfigParameters::DiscoveryConfig:
{
uint8_t discovery{};
std::optional<bool> enabled =
dcmi::getDHCPOption(ctx, dcmi::dhcpOpt12Enabled);
if (!enabled.has_value())
{
return ipmi::responseUnspecifiedError();
}
if (enabled.value())
{
discovery = dcmi::option12Mask;
}
payload.pack(discovery);
break;
}
// Get below values from Systemd-networkd source code
case dcmi::DCMIConfigParameters::DHCPTiming1:
payload.pack(dcmi::dhcpTiming1);
break;
case dcmi::DCMIConfigParameters::DHCPTiming2:
payload.pack(dcmi::dhcpTiming2);
break;
case dcmi::DCMIConfigParameters::DHCPTiming3:
payload.pack(dcmi::dhcpTiming3);
break;
default:
return ipmi::responseInvalidFieldRequest();
}
return ipmi::responseSuccess(payload);
}
static std::optional<uint16_t> readPower(ipmi::Context::ptr& ctx)
{
std::ifstream sensorFile(POWER_READING_SENSOR);
std::string objectPath;
if (!sensorFile.is_open())
{
lg2::error(
"Power reading configuration file not found: {POWER_SENSOR_FILE}",
"POWER_SENSOR_FILE", std::string_view{POWER_READING_SENSOR});
return std::nullopt;
}
auto data = nlohmann::json::parse(sensorFile, nullptr, false);
if (data.is_discarded())
{