summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/common/attributeTank.C
blob: 76e330e699338fc552b06cd8115da9b335b61351 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/targeting/common/attributeTank.C $                    */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2013,2014              */
/*                                                                        */
/* 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>

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())
    {
        if (i_nodeFilter == NODE_FILTER_NOT_ALL_NODES)
        {
            // Only clear attributes that are not for all nodes
            if ((*l_itr)->iv_hdr.iv_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_itr)->iv_hdr.iv_node != ATTR_NODE_NA) &&
                 ((*l_itr)->iv_hdr.iv_node != i_node) )
            {
                l_itr++;
                continue;
            }
        }
        else if (i_nodeFilter == NODE_FILTER_SPECIFIC_NODE)
        {
            // Only clear attributes associated with i_node
            if ((*l_itr)->iv_hdr.iv_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)
    {
        if ( ((*l_itr)->iv_hdr.iv_attrId == i_attrId) &&
             ((*l_itr)->iv_hdr.iv_targetType == i_targetType) &&
             ((*l_itr)->iv_hdr.iv_pos == i_pos) &&
             ((*l_itr)->iv_hdr.iv_unitPos == i_unitPos) &&
             ((*l_itr)->iv_hdr.iv_node == i_node) )
        {
            if (!((*l_itr)->iv_hdr.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)
    {
        if ( ((*l_itr)->iv_hdr.iv_attrId == i_attrId) &&
             ((*l_itr)->iv_hdr.iv_targetType == i_targetType) &&
             ((*l_itr)->iv_hdr.iv_pos == i_pos) &&
             ((*l_itr)->iv_hdr.iv_unitPos == i_unitPos) &&
             ((*l_itr)->iv_hdr.iv_node == i_node) &&
             ((*l_itr)->iv_hdr.iv_valSize == i_valSize) )
        {
            // Found existing attribute, update it unless the existing attribute
            // is const and the new attribute is non-const
            if (!( ((*l_itr)->iv_hdr.iv_flags & ATTR_FLAG_CONST) &&
                   (!(i_flags & ATTR_FLAG_CONST)) ) )
            {
                (*l_itr)->iv_hdr.iv_flags = i_flags;
                memcpy((*l_itr)->iv_pVal, 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->iv_hdr.iv_attrId = i_attrId;
        l_pAttr->iv_hdr.iv_targetType = i_targetType;
        l_pAttr->iv_hdr.iv_pos = i_pos;
        l_pAttr->iv_hdr.iv_unitPos = i_unitPos;
        l_pAttr->iv_hdr.iv_node = i_node;
        l_pAttr->iv_hdr.iv_flags = i_flags;
        l_pAttr->iv_hdr.iv_valSize = i_valSize;
        l_pAttr->iv_pVal = new uint8_t[i_valSize];
        memcpy(l_pAttr->iv_pVal, 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)
    {
        // Allow match if attribute applies to all positions
        if ( ((*l_itr)->iv_hdr.iv_attrId == i_attrId) &&
             ((*l_itr)->iv_hdr.iv_targetType == i_targetType) &&
             (((*l_itr)->iv_hdr.iv_pos == ATTR_POS_NA) ||
              ((*l_itr)->iv_hdr.iv_pos == i_pos)) &&
             (((*l_itr)->iv_hdr.iv_unitPos == ATTR_UNIT_POS_NA) ||
              ((*l_itr)->iv_hdr.iv_unitPos == i_unitPos)) &&
             (((*l_itr)->iv_hdr.iv_node == ATTR_NODE_NA) ||
              ((*l_itr)->iv_hdr.iv_node == i_node)) )
        {
            l_found = true;
            memcpy(o_pVal, (*l_itr)->iv_pVal, (*l_itr)->iv_hdr.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())
        {
            if (i_nodeFilter == NODE_FILTER_NOT_ALL_NODES)
            {
                // Only want attributes that are not for all nodes
                if ((*l_itr)->iv_hdr.iv_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_itr)->iv_hdr.iv_node != ATTR_NODE_NA) &&
                     ((*l_itr)->iv_hdr.iv_node != i_node) )
                {
                    l_itr++;
                    continue;
                }
            }
            else if (i_nodeFilter == NODE_FILTER_SPECIFIC_NODE)
            {
                // Only want attributes associated with i_node
                if ((*l_itr)->iv_hdr.iv_node != i_node)
                {
                    l_itr++;
                    continue;
                }
            }

            if ((l_index + sizeof(AttributeHeader) +
                     (*l_itr)->iv_hdr.iv_valSize) > 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_itr)->iv_hdr.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
            {
                // Copy the attribute header to the buffer
                AttributeHeader * l_pHeader =
                    reinterpret_cast<AttributeHeader *>(l_pBuffer + l_index);
                *l_pHeader = (*l_itr)->iv_hdr;
                l_index += sizeof(AttributeHeader);

                // Copy the attribute value to the buffer
                memcpy((l_pBuffer + l_index), (*l_itr)->iv_pVal,
                       (*l_itr)->iv_hdr.iv_valSize);
                l_index += (*l_itr)->iv_hdr.iv_valSize;

                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)->iv_hdr.iv_attrId == i_attrId)
        {
            l_found = true;
            break;
        }
    }

    TARG_MUTEX_UNLOCK(iv_mutex);
    return l_found;
}

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

    uint32_t l_index = 0;

    while (l_index < i_attributes.iv_size)
    {
        AttributeHeader * l_pAttrHdr =
            reinterpret_cast<AttributeHeader *>
                (i_attributes.iv_pAttributes + l_index);

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

        l_index += sizeof(AttributeHeader);

        if (l_pAttrHdr->iv_valSize > (i_attributes.iv_size - l_index))
        {
            // Remaining chunk smaller than attribute value, quit
            TRACFCOMP(g_trac_targeting,
                      "deserializeAttributes: Error, attr too big for chunk (0x%x:0x%x)",
                      l_pAttrHdr->iv_valSize, (i_attributes.iv_size - l_index));
            break;
        }

        // Create a new Attribute and add it to the tank
        Attribute * l_pAttr = new Attribute();
        l_pAttr->iv_hdr = *l_pAttrHdr;
        l_pAttr->iv_pVal = new uint8_t[l_pAttrHdr->iv_valSize];
        memcpy(l_pAttr->iv_pVal, (i_attributes.iv_pAttributes + l_index),
               l_pAttrHdr->iv_valSize);

        l_index += l_pAttrHdr->iv_valSize;
        iv_attributesExist = true;
        iv_attributes.push_back(l_pAttr);
    }

    TARG_MUTEX_UNLOCK(iv_mutex);
}

//******************************************************************************
AttributeTank::Attribute::Attribute() :
    iv_pVal(NULL)
{

}

//******************************************************************************
AttributeTank::Attribute::~Attribute()
{
    delete[] iv_pVal;
    iv_pVal = NULL;
}

}
OpenPOWER on IntegriCloud