summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/common/attributeTank.C
blob: c6cad3c493e0ad026d4fef86fdf3f5f5b6347116 (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
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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/targeting/common/attributeTank.C $                    */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2013,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 *  @file attributeTank.C
 *
 *  @brief Implements the AttributeTank and associated classes.
 */
#include <stdlib.h>
#include <string.h>
#include <targeting/common/attributeTank.H>
#include <targeting/common/trace.H>
#include <targeting/common/predicates/predicatepostfixexpr.H>
#include <targeting/common/predicates/predicatectm.H>
#include <targeting/common/predicates/predicateattrtanktargetpos.H>
#include <targeting/common/iterators/rangefilter.H>
#include <targeting/common/targetservice.H>
#include <targeting/targplatutil.H>
#include <targeting/common/targreasoncodes.H>



namespace TARGETING
{

//******************************************************************************
AttributeTank::AttributeHeader::AttributeHeader() :
    iv_attrId(0), iv_targetType(0), iv_pos(0), iv_unitPos(0), iv_node(0),
    iv_flags(0), iv_valSize(0)
{

}

//******************************************************************************
AttributeTank::AttributeSerializedChunk::AttributeSerializedChunk() :
    iv_size(0), iv_pAttributes(NULL)
{

}

//******************************************************************************
AttributeTank::AttributeTank() :
    iv_attributesExist(false)
{
    TARG_MUTEX_INIT(iv_mutex);
}

//******************************************************************************
AttributeTank::~AttributeTank()
{
    for (AttributesItr_t l_itr = iv_attributes.begin();
         l_itr != iv_attributes.end(); ++l_itr)
    {
        delete (*l_itr);
        (*l_itr) = NULL;
    }

    TARG_MUTEX_DESTROY(iv_mutex);
}

//******************************************************************************
bool AttributeTank::syncEnabled()
{
    // TODO, RTC 42642. Check for CronusMode, probably using an attribute
    // but TBD. If CronusMode is not enabled then there should not be the
    // performance hit of adding written attributes to a SyncAttributeTank
    return false;
}

//******************************************************************************
void AttributeTank::clearAllAttributes(
    const NodeFilter i_nodeFilter,
    const uint8_t i_node)
{
    TARG_MUTEX_LOCK(iv_mutex);

    AttributesItr_t l_itr = iv_attributes.begin();

    while (l_itr != iv_attributes.end())
    {
        // Get a copy of the Attribute's node for quick access
        uint8_t l_node = (*l_itr)->getHeader().iv_node;
        if (i_nodeFilter == NODE_FILTER_NOT_ALL_NODES)
        {
            // Only clear attributes that are not for all nodes
            if (l_node == ATTR_NODE_NA)
            {
                l_itr++;
                continue;
            }
        }
        else if (i_nodeFilter == NODE_FILTER_SPECIFIC_NODE_AND_ALL)
        {
            // Only clear attributes associated with i_node or all
            if ( (l_node != ATTR_NODE_NA) &&
                 (l_node != i_node) )
            {
                l_itr++;
                continue;
            }
        }
        else if (i_nodeFilter == NODE_FILTER_SPECIFIC_NODE)
        {
            // Only clear attributes associated with i_node
            if (l_node != i_node)
            {
                l_itr++;
                continue;
            }
        }

        delete (*l_itr);
        (*l_itr) = NULL;
        l_itr = iv_attributes.erase(l_itr);
    }

    if (iv_attributes.empty())
    {
        iv_attributesExist = false;
    }

    TARG_MUTEX_UNLOCK(iv_mutex);
}

//******************************************************************************
void AttributeTank::clearNonConstAttribute(const uint32_t i_attrId,
                                           const uint32_t i_targetType,
                                           const uint16_t i_pos,
                                           const uint8_t i_unitPos,
                                           const uint8_t i_node)
{
    TARG_MUTEX_LOCK(iv_mutex);

    for (AttributesItr_t l_itr = iv_attributes.begin();
         l_itr != iv_attributes.end(); ++l_itr)
    {
        // Get a (constant) reference to the Attribute Header
        // for easy access to data members
        const AttributeHeader &l_attributeHeader = (*l_itr)->getHeader();

        // Find attribute that satisfies search criteria
        if ( (l_attributeHeader.iv_attrId == i_attrId) &&
             (l_attributeHeader.iv_targetType == i_targetType) &&
             (l_attributeHeader.iv_pos == i_pos) &&
             (l_attributeHeader.iv_unitPos == i_unitPos) &&
             (l_attributeHeader.iv_node == i_node) )
        {
            if (!(l_attributeHeader.iv_flags & ATTR_FLAG_CONST))
            {
                delete (*l_itr);
                (*l_itr) = NULL;
                iv_attributes.erase(l_itr);

                if (iv_attributes.empty())
                {
                    iv_attributesExist = false;
                }
            }

            break;
        }
    }

    TARG_MUTEX_UNLOCK(iv_mutex);
}

//******************************************************************************
void AttributeTank::setAttribute(const uint32_t i_attrId,
                                 const uint32_t i_targetType,
                                 const uint16_t i_pos,
                                 const uint8_t i_unitPos,
                                 const uint8_t i_node,
                                 const uint8_t i_flags,
                                 const uint32_t i_valSize,
                                 const void * i_pVal)
{
    TARG_MUTEX_LOCK(iv_mutex);

    // Search for an existing matching attribute
    bool l_found = false;

    for (AttributesItr_t l_itr = iv_attributes.begin();
         l_itr != iv_attributes.end(); ++l_itr)
    {
        // Get a reference to the Attribute Header
        // for easy access to data members
        const AttributeHeader &l_attributeHeader = (*l_itr)->getHeader();

        // Find attribute that satisfies search criteria
        if ( (l_attributeHeader.iv_attrId == i_attrId) &&
             (l_attributeHeader.iv_targetType == i_targetType) &&
             (l_attributeHeader.iv_pos == i_pos) &&
             (l_attributeHeader.iv_unitPos == i_unitPos) &&
             (l_attributeHeader.iv_node == i_node) &&
             (l_attributeHeader.iv_valSize == i_valSize) )
        {
            // Found existing attribute, update it unless the existing attribute
            // is const and the new attribute is non-const
            if (!( (l_attributeHeader.iv_flags & ATTR_FLAG_CONST) &&
                   (!(i_flags & ATTR_FLAG_CONST)) ) )
            {
                (*l_itr)->setFlags(i_flags);
                (*l_itr)->setValue(i_pVal, i_valSize);
            }
            l_found = true;
            break;
        }
    }

    if (!l_found)
    {
        // Add a new attribute to the tank
        Attribute * l_pAttr = new Attribute();

        l_pAttr->setId(i_attrId);
        l_pAttr->setTargetType(i_targetType);
        l_pAttr->setPosition(i_pos);
        l_pAttr->setUnitPosition(i_unitPos);
        l_pAttr->setNode(i_node);
        l_pAttr->setFlags(i_flags);
        l_pAttr->setValue(i_pVal, i_valSize);

        iv_attributesExist = true;
        iv_attributes.push_back(l_pAttr);
    }

    TARG_MUTEX_UNLOCK(iv_mutex);
}

//******************************************************************************
bool AttributeTank::getAttribute(const uint32_t i_attrId,
                                 const uint32_t i_targetType,
                                 const uint16_t i_pos,
                                 const uint8_t i_unitPos,
                                 const uint8_t i_node,
                                 void * o_pVal) const
{
    TARG_MUTEX_LOCK(iv_mutex);

    bool l_found = false;

    for (AttributesCItr_t l_itr = iv_attributes.begin(); l_itr
         != iv_attributes.end(); ++l_itr)
    {
        // Get a (constant) reference to the Attribute Header
        // for easy access to data members
        const AttributeHeader &l_attributeHeader = (*l_itr)->getHeader();

        // Find attribute that satisfies search criteria
        if ( (l_attributeHeader.iv_attrId == i_attrId) &&
             (l_attributeHeader.iv_targetType == i_targetType) &&
             ((l_attributeHeader.iv_pos == ATTR_POS_NA) ||
              (l_attributeHeader.iv_pos == i_pos)) &&
             ((l_attributeHeader.iv_unitPos == ATTR_UNIT_POS_NA) ||
              (l_attributeHeader.iv_unitPos == i_unitPos)) &&
             ((l_attributeHeader.iv_node == ATTR_NODE_NA) ||
              (l_attributeHeader.iv_node == i_node)) )
        {
            l_found = true;
            (*l_itr)->cloneValue(o_pVal, l_attributeHeader.iv_valSize);
            break;
        }
    }

    TARG_MUTEX_UNLOCK(iv_mutex);
    return l_found;
}

//******************************************************************************
void AttributeTank::serializeAttributes(
    const AllocType i_allocType,
    const uint32_t i_chunkSize,
    std::vector<AttributeSerializedChunk> & o_attributes,
    const NodeFilter i_nodeFilter,
    const uint8_t i_node) const
{
    TARG_MUTEX_LOCK(iv_mutex);

    // Temporary buffer of the requested chunk size for storing attributes
    uint8_t * l_pBuffer = new uint8_t[i_chunkSize];
    uint32_t l_index = 0;

    AttributesCItr_t l_itr = iv_attributes.begin();
    while (l_itr != iv_attributes.end())
    {
        // Fill up the buffer with as many attributes as possible
        while (l_itr != iv_attributes.end())
        {
            // Get a (constant) reference to the Attribute Header
            // for easy access to data members
            const AttributeHeader &l_attributeHeader =
                                                 (*l_itr)->getHeader();

            // Get a copy of the node for quick access
            uint8_t l_node = l_attributeHeader.iv_node;

            if (i_nodeFilter == NODE_FILTER_NOT_ALL_NODES)
            {
                // Only want attributes that are not for all nodes
                if (l_node == ATTR_NODE_NA)
                {
                    l_itr++;
                    continue;
                }
            }
            else if (i_nodeFilter == NODE_FILTER_SPECIFIC_NODE_AND_ALL)
            {
                // Only want attributes associated with i_node or all
                if ( (l_node != ATTR_NODE_NA) &&
                     (l_node != i_node) )
                {
                    l_itr++;
                    continue;
                }
            }
            else if (i_nodeFilter == NODE_FILTER_SPECIFIC_NODE)
            {
                // Only want attributes associated with i_node
                if (l_node != i_node)
                {
                    l_itr++;
                    continue;
                }
            }

            if ((l_index + (*l_itr)->getSize()) > i_chunkSize)
            {
                // Attribute will not fit into the buffer
                if (l_index == 0)
                {
                    // Attribute will not fit in an empty buffer of the
                    // requested chunk size, this should not happen, if it does,
                    // just move to the next attribute
                    TRACFCOMP(g_trac_targeting,
                        "serializeAttributes: Error, attr too big to serialize "
                        "(0x%x)",
                        l_attributeHeader.iv_valSize);
                    l_itr++;
                }
                else
                {
                    // Attribute will not fit in a partially filled buffer, the
                    // buffer is ready to send to the user
                    break;
                }
            }
            else
            {
                l_index += (*l_itr)->serialize(l_pBuffer + l_index,
                                               (*l_itr)->getSize());
                l_itr++;
            }
        }

        if (l_index)
        {
            // Create a chunk and add it to the caller's vector
            AttributeSerializedChunk l_chunk;

            if (i_allocType == ALLOC_TYPE_MALLOC)
            {
                l_chunk.iv_pAttributes =
                    static_cast<uint8_t *>(malloc(l_index));
            }
            else
            {
                l_chunk.iv_pAttributes = new uint8_t[l_index];
            }

            memcpy(l_chunk.iv_pAttributes, l_pBuffer, l_index);
            l_chunk.iv_size = l_index;
            o_attributes.push_back(l_chunk);

            // Reuse the buffer for the next attribute
            l_index = 0;
        }
    }

    delete [] l_pBuffer;

    TARG_MUTEX_UNLOCK(iv_mutex);
}

//******************************************************************************
bool AttributeTank::attributeExists(const uint32_t i_attrId) const
{
    // Note. The use-case is for the caller to call attributesExist() before
    // calling this function, i.e. the caller has already verified that
    // attributes exist in the tank. No need for this function to call
    // attributesExist() again.
    TARG_MUTEX_LOCK(iv_mutex);

    bool l_found = false;

    for (AttributesCItr_t l_itr = iv_attributes.begin(); l_itr
         != iv_attributes.end(); ++l_itr)
    {
        if ((*l_itr)->getHeader().iv_attrId == i_attrId)
        {
            l_found = true;
            break;
        }
    }

    TARG_MUTEX_UNLOCK(iv_mutex);
    return l_found;
}

//******************************************************************************
void AttributeTank::deserializeAttributes(
        const AttributeSerializedChunk & i_attributes,
        bool i_echoAttributes )
{
    TARG_MUTEX_LOCK(iv_mutex);

    uint32_t l_index = 0;

    // Get a handle to the serialized Attributes
    uint8_t* l_serializedData =
                        reinterpret_cast<uint8_t*>(i_attributes.iv_pAttributes);

    // Iterate thru the Attributes
    while (l_index < i_attributes.iv_size)
    {
        // Create a new Attribute
        Attribute * l_pAttribute = new Attribute();

        // Deserialize the data, if possible
        uint32_t l_deserializedDataSize = l_pAttribute->deserialize(
                                l_serializedData + l_index,
                                i_attributes.iv_size - l_index);

        if (!l_deserializedDataSize)
        {
            // Unable to deserialize data, delete Attribute
            delete l_pAttribute;
            l_pAttribute = NULL;

            // Remaining chunk smaller than attribute header, quit
            TRACFCOMP(g_trac_targeting,
                      "deserializeAttributes: Error, attribute too big for "
                      "chunk (0x%x)",
                      (i_attributes.iv_size - l_index));
            break;
        }

        // Was able to deserialize data, add Attribute to the tank
        iv_attributesExist = true;
        iv_attributes.push_back(l_pAttribute);

        // Increment the index after deserializing an attribute
        l_index += l_deserializedDataSize;

        if ( i_echoAttributes == true ) // attributes should be echo'd
        {
            // extract individual fields from attribute
            uint32_t attrId(l_pAttribute->getHeader().iv_attrId);
            uint32_t targetType(l_pAttribute->getHeader().iv_targetType);
            uint16_t pos(l_pAttribute->getHeader().iv_pos);
            uint8_t unitPos(l_pAttribute->getHeader().iv_unitPos);

            uint8_t node(l_pAttribute->getHeader().iv_node);
            uint8_t flags(l_pAttribute->getHeader().iv_flags);

            uint32_t valueLen(l_pAttribute->getHeader().iv_valSize);

            TRACFCOMP(g_trac_targeting,
                      "deserializeAttributes: Attribute Hdr: "
                      "ID = %.8X  Target Type = %.8X  Positon = %.4X  "
                      "Unit Position = %.2X  node = %.1X  flags = %.1X  "
                      "Parm Length = %.8X\n",
                      attrId, targetType, pos, unitPos, node, flags, valueLen);

            TRACFBIN(g_trac_targeting,
                      "deserializeAttributes: Parm Value: ",
                      l_pAttribute->getValue(), valueLen);
        } // end echo attributes
    }

    TARG_MUTEX_UNLOCK(iv_mutex);
}

//******************************************************************************
void AttributeTank::deserializeAttributes(
        const AttributeSerializedChunk & i_attributes)
{
    // deserialize without echo
    deserializeAttributes( i_attributes,
                           false );
}

//******************************************************************************
errlHndl_t AttributeTank::writePermAttributes()
{
    errlHndl_t l_err = NULL;

    for(AttributesCItr_t l_attrIter = iv_attributes.begin();
        l_attrIter != iv_attributes.end(); ++l_attrIter)
    {
        // Get a (constant) reference to the Attribute Header
        // for easy access to data members
        const AttributeHeader &l_attrHdr = (*l_attrIter)->getHeader();

        PredicatePostfixExpr l_permAttrOverrides;

        // Predicate to match target type
        PredicateCTM l_targetTypeFilter
                (CLASS_NA, static_cast<TYPE>(l_attrHdr.iv_targetType));

        // Predicate to match attribute tank target position
        PredicateAttrTankTargetPos l_targetPosFilter(l_attrHdr.iv_pos,
                                                     l_attrHdr.iv_unitPos,
                                                     l_attrHdr.iv_node);

        l_permAttrOverrides.push(&l_targetTypeFilter)
            .push(&l_targetPosFilter).And();

        // Apply the filter through all targets
        TargetRangeFilter l_permTargetList( targetService().begin(),
                                            targetService().end(),
                                            &l_permAttrOverrides);

        // Write permanent attributes to target
        for ( ; l_permTargetList; ++l_permTargetList)
        {
            bool l_success = (*l_permTargetList)->_trySetAttr(
                                static_cast<ATTRIBUTE_ID>(l_attrHdr.iv_attrId),
                                l_attrHdr.iv_valSize,
                                (*l_attrIter)->getValue() );

            if (l_success)
            {
                TRACFCOMP(g_trac_targeting, "writePermAttributes: Successful "
                    "permanent override of Attr ID:0x%X Value:0x%llX applied "
                    "to target 0x%X",
                    l_attrHdr.iv_attrId,
                    *reinterpret_cast<const uint64_t *>
                                                    ((*l_attrIter)->getValue()),
                    (*l_permTargetList)->getAttr<ATTR_HUID>() );
            }
            else
            {
                uint8_t * l_pAttrData(NULL);
                bool l_found = (*l_permTargetList)->_tryGetAttr(
                                static_cast<ATTRIBUTE_ID>(l_attrHdr.iv_attrId),
                                l_attrHdr.iv_valSize,
                                l_pAttrData);

                if (l_found)
                {
                    TRACFCOMP(g_trac_targeting, "writePermAttributes: Value "
                        "NOT applied to target, override failed for Attr "
                        "ID:0x%X Value:0x%llX on target 0x%X - current value "
                        "0x%llX",
                        l_attrHdr.iv_attrId,
                        *reinterpret_cast<const uint64_t *>
                                                    ((*l_attrIter)->getValue()),
                        (*l_permTargetList)->getAttr<ATTR_HUID>(),
                        *reinterpret_cast<uint64_t *>(l_pAttrData) );
                    /*@
                     * @errortype
                     * @moduleid     TARG_WRITE_PERM_ATTR
                     * @reasoncode   TARG_RC_WRITE_PERM_ATTR_FAIL
                     * @userdata1    Target specified
                     * @userdata2    Attribute specified
                     * @devdesc      Failure applying given attribute override
                     *               on given target
                     */
                    UTIL::createTracingError(
                       TARG_WRITE_PERM_ATTR,
                       TARG_RC_WRITE_PERM_ATTR_FAIL,
                       (*l_permTargetList)->getAttr<ATTR_HUID>(),
                       l_attrHdr.iv_attrId,
                       0,0,
                       l_err);
                    break;
                }
                else
                {
                    TRACFCOMP(g_trac_targeting, "writePermAttributes: Target "
                        "does not have attribute, override NOT applied for "
                        "Attr ID:0x%X on target 0x%X",
                        l_attrHdr.iv_attrId,
                        (*l_permTargetList)->getAttr<ATTR_HUID>() );
                    /*@
                     * @errortype
                     * @moduleid     TARG_WRITE_PERM_ATTR
                     * @reasoncode   TARG_RC_WRITE_PERM_ATTR_TARGET_FAIL
                     * @userdata1    Target specified
                     * @userdata2    Attribute specified
                     * @devdesc      Given target does not have given attribute
                     *               to apply override
                     */
                    UTIL::createTracingError(
                       TARG_WRITE_PERM_ATTR,
                       TARG_RC_WRITE_PERM_ATTR_TARGET_FAIL,
                       (*l_permTargetList)->getAttr<ATTR_HUID>(),
                       l_attrHdr.iv_attrId,
                       0,0,
                       l_err);
                    break;
                }
            }
        }
        if (l_err)
        {
            break;
        }
    }
    return l_err;
}

//******************************************************************************
size_t AttributeTank::size() const
{
    return iv_attributes.size();
}

//******************************************************************************
void AttributeTank::getAllAttributes( std::list<Attribute *>& o_attributes ) const
{
    o_attributes = iv_attributes;
}

//******************************************************************************
const char* AttributeTank::layerToString( TankLayer i_layer )
{
    switch(i_layer)
    {
        case(AttributeTank::TANK_LAYER_FAPI): return "FAPI";
        case(AttributeTank::TANK_LAYER_TARG): return "TARG";
        case(AttributeTank::TANK_LAYER_PERM): return "PERM";
        default: return "UNKNOWN";
    }
}

}

OpenPOWER on IntegriCloud