summaryrefslogtreecommitdiff
path: root/cesar/cl/src/send.c
blob: 1c0866498f2ba3b43bbf1803fb654c7d3373afca (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
/* Cesar project {{{
 *
 * Copyright (C) 2012 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cl/src/send.c
 * \brief   Functions to handle data going out of the CL
 * \ingroup cl
 */
#include "common/std.h"
#include "common/defs/ethernet.h"

#include "common/ipmbox/msg.h"

#include "lib/utils.h"
#include "lib/bitstream.h"
#include "lib/seq_check.h"

#include "mac/common/timings.h"
#include "mac/common/ntb.h"

#include "cl/cl.h"
#include "cl/data_rate.h"
#include "cl/bridge_table.h"

#include "cl/inc/context.h"
#include "cl/inc/trace.h"
#include "cl/inc/send.h"
#include "config/cl.h"

/**
 * Search for the lid in the classifier
 * \param  ctx  CL context
 * \param  tei  the tei found previously by the mactotei table.
 * \param  tag  tag provided by the upper layer.
 * \param  bcast  true if the link is a broadcast one, false otherwise.
 * \param  acs  put true if the link should be process by the ACS
 * \param  drop put true if the data should be drop.
 */
PRIVATE inline uint
cl_classifer_get_lid (cl_t *ctx, uint tei, uint tag,
                      bool *bcast, bool *acs,
                      bool *drop)
{
    static uint lid_table[] = { 1, 0, 0, 1, 2, 2, 3, 3 };
    uint lid;
    dbg_claim (ctx);
    dbg_claim (bcast);
    dbg_claim (acs);
    dbg_claim (drop);
    dbg_claim (tag < COUNT (lid_table));
    if (tei == MAC_TEI_BCAST)
        *bcast = true;
    else
        *bcast = false;
    *acs = false;
    *drop = false;
    lid = lid_table[tag];
    if (CONFIG_CL_EOC_CLASSIFY)
    {
        if ((tei == MAC_TEI_UNASSOCIATED || tei == MAC_TEI_BCAST))
            lid = 0;
        else
            lid = MAC_LLID_MIN;
    }
    CL_TRACE (CLASSIFIER, phy_date (), tei, *bcast, *acs, *drop, lid);
    /** TODO fill the classifier */
    return lid;
}

/**
 * Sends a packet to the SAR.
 * \param  ctx  the CL context.
 * \param  buffer the buffer length.
 * \param  length  the buffer length.
 * \param  tei  the destination TEI.
 * \param  mme  true if the message comes from the CP, false otherwise.
 * \param  tag  the tag provided by the upper layer.
 * \param  arrival_time_ntb  the arrival time NTB.
 * \param  group_id  the group id for data multiunicast.
 */
static void
cl_send_multiunicast (cl_t *ctx, u8 *buffer, uint length, bool mme, uint tag,
                      u32 arrival_time_ntb, uint group_id)
{
    uint i;
    sta_t *sta;
    mfs_tx_t *mfs;
    bool added;
    dbg_claim (ctx);
    dbg_claim (buffer);

    if (mme)
    {
        for ( i = MAC_TEI_STA_MIN; i < MAC_TEI_STA_NB; i++)
        {
            sta = mac_store_sta_get (ctx->mac_store, i);

            if (sta && sta->multi_unicast_receiver)
            {
                mfs = mac_store_mfs_add_tx (ctx->mac_store,
                                            false /* MME */,
                                            true /* MME */,
                                            MAC_LID_NONE,
                                            i,
                                            &added);
                if (added)
                    sar_mfs_add (ctx->sar, (mfs_t *) mfs);

                CL_TRACE (MME_SEND_MULTIUNICAST, phy_date (), length, buffer,
                          i);
                sar_msdu_process (ctx->sar, buffer, length, mfs,
                                  arrival_time_ntb);
                blk_release (mfs);
                blk_release (sta);
            }
        }
        /* The buffer is no more used, be aware the SAR is polling the bridge
         * DMA, that why it can release the buffer here. */
        bufmgr_keep_buffer (ctx->bufmgr, buffer);
    }
    else
    {
        for (i = 0; i < ctx->groups.nb_actual_members[group_id]; i++)
        {
            uint lid;
            bool bcast;
            bool acs;
            bool drop;
            uint tei = ctx->groups.member_tei[group_id][i];
            dbg_assert (MAC_TEI_IS_STA (tei));

            /* Get some data from the classifier. */
            lid = cl_classifer_get_lid (ctx, tei, tag, &bcast, &acs, &drop);

            if (MAC_LID_IS_PLID (lid))
            {
                /* Create the MFS if it does not exits. */
                mfs = mac_store_mfs_add_tx (ctx->mac_store, bcast, false, lid,
                                            tei, &added);
                if (added)
                    sar_mfs_add (ctx->sar, (mfs_t *)mfs);
            }
            else
            {
                /* try to get the mfs from the store. */
                mfs = mac_store_mfs_get_tx (ctx->mac_store, bcast, false, lid,
                                            tei);
            }

            if (!drop && !acs && mfs)
            {
                CL_TRACE (DATA_SEND, phy_date (), buffer, length,
                          mfs->common.tei, mfs->common.lid,
                          mfs->common.bcast);
                sar_msdu_process (ctx->sar, buffer, length, mfs,
                                  arrival_time_ntb);
                /* update data rate informations associated to the TX
                 * from the local sta to the associated sta */
                cl_compute_datarate_on_sta (ctx, PARENT_OF (mfs_t, tx, mfs),
                                            length);
            }
            else
            {
                CL_TRACE (DATA_SEND_MULTI_FAILED, phy_date (), drop, acs,
                          mfs, tei, group_id);
            }
            if (mfs)
                blk_release (mfs);
        }
        /* The buffer is no more used, be aware the SAR is polling the bridge
         * DMA, that why it can release the buffer here. */
        bufmgr_give_back (ctx->bufmgr, buffer);
    }
}

/**
 * Send a data once the TEI has been find.
 *
 * \param  ctx  the cl context.
 * \param  buffer  the buffer containing the data to send.
 * \param  length  the buffer data length.
 * \param  tei  the TEI found from the cl_mactotei list.
 * \param  tag  the tag provided by the upper layer.
 * \param  arrival_time_ntb  the arrival time in NTB.
 * \param  dmac  the final destination mac address.
 * \param  smac  the source mac address.
 */
static inline void
cl_data_send_with_tei (cl_t *ctx, u8 *buffer, uint length, uint tei,
                       uint tag, u32 arrival_time_ntb, mac_t dmac, mac_t smac)
{
    uint lid;
    bool bcast;
    bool acs;
    bool drop;
    mfs_tx_t *mfs;

    dbg_claim (ctx);
    dbg_claim (buffer);
    dbg_claim (length >= ETH_PACKET_MIN_SIZE_ALLOWED
               && length <= ETH_PACKET_MAX_SIZE);
    dbg_claim (tei);

    /* Get some data from the classifier. */
    lid = cl_classifer_get_lid (ctx, tei, tag, &bcast, &acs, &drop);

    if (MAC_LID_IS_PLID (lid))
    {
        bool added;

        /* Create the MFS if it does not exits. */
        mfs = mac_store_mfs_add_tx (ctx->mac_store, bcast, false, lid, tei,
                                    &added);

        if (added)
            sar_mfs_add (ctx->sar, (mfs_t *)mfs);
    }
    else
    {
        /* try to get the mfs from the store. */
#if !CONFIG_MAC_COMMON_EOC_MFS
        mfs = mac_store_mfs_get_tx (ctx->mac_store, bcast, false, lid, tei);
#else
        mfs = mac_store_mfs_get_sta_tx_data (ctx->mac_store, lid, tei);
        if (mfs)
            blk_addref (mfs);
#endif
    }
#if CONFIG_MAC_COMMON_EOC_MFS
    if (mfs && mfs->seg_nb >= MFS_TX_MAX_SEG_NB)
        drop = true;
#endif
    if (!drop && !acs && mfs)
    {
        CL_TRACE (DATA_SEND, phy_date (), buffer, length, mfs->common.tei,
                  mfs->common.lid,  mfs->common.bcast);
        ctx->data_send_link.mfs = mfs;
        ctx->data_send_link.last_update_date_ntb = arrival_time_ntb;
        ctx->data_send_link.dmac = dmac;
        ctx->data_send_link.smac = smac;
        ctx->data_send_link.tag = tag;
        sar_msdu_process (ctx->sar, buffer, length, mfs, arrival_time_ntb);
        ctx->stats.tx_data ++;
        /* update data rate informations associated to the TX
         * from the local sta to the associated sta */
        cl_compute_datarate_on_sta (ctx, PARENT_OF (mfs_t, tx, mfs),
                                    length);
    }
    else
    {
        CL_TRACE (DATA_SEND_DROP, phy_date (), ctx->mac_config->authenticated,
                  buffer, length);
        ctx->stats.tx_data_drop_mfs++;
        /* Release the MFS */
        if (mfs)
            blk_release (mfs);
    }
    /* SAR ends using the buffer it can be given back.
     * BRGDMA is polled by the SAR thats why it the buffer can be given
     * back to the buffer manager. */
    bufmgr_give_back (ctx->bufmgr, buffer);
}

/**
 * Prepare the CL to send a Frame.
 * \param  ctx  the cl context.
 * \param  buffer  the Ethernet frame to send.
 * \param  length  the Ethernet frame length.
 * \param  tag  the tag for the classifier.
 * \param  arrrival_time_ntb  arrival time in the High layers.
 * \param  dmac  the destination mac address.
 * \param  smac  the source mac address.
 */
static void
cl_data_send_prepare (cl_t *ctx, u8 *buffer, uint length, uint tag,
                      u32 arrival_time_ntb, mac_t dmac, mac_t smac)
{
    /* The source MAC address should not be not be a multicast, a zero address
     * nor a broadcast one (note that checking multicast address also checks
     * for broadcast). Linux, in bridging mode, handle this problem and reject
     * any of those packets (see net/bridge/br_input.c:127 (in
     * br_handle_frame). */
    dbg_claim (smac != MAC_ZERO);
    dbg_claim (!mac_is_multicast (smac));
    /* Add the source MAC address to the local bridge table if not our MAC
     * address. */
    dbg_claim (ctx->mac_config);
    if (ctx->mac_config->sta_mac_address != smac && !CONFIG_CL_EOC_ROUTE)
        bridge_table_add (ctx, smac);

    /* Data are forbidden if not authenticated. */
    if (!ctx->mac_config->authenticated)
    {
        CL_TRACE (DATA_SEND_DROP, phy_date (), ctx->mac_config->authenticated,
                  buffer, length);
        ctx->stats.tx_data_drop_auth++;
        bufmgr_give_back (ctx->bufmgr, buffer);
        return;
    }

    /* Get the TEI from the mactotei table. */
    uint tei;
    uint group=0;
#if CONFIG_CL_EOC_ROUTE
    /* For STA, if tei is unassociated, or MAC limit is reached
     * drop the packet */
    if (MAC_TEI_IS_EOC_STA(ctx->mac_config->tei))
    {
        bool drop = false;
        tei = MAC_TEI_CCO_DEF;
        if (smac != ctx->mac_config->sta_mac_address)
            drop = !cl_eoc_mactotei_entry_insert (ctx, smac,
                                                  ctx->mac_config->tei);
        if (drop)
        {
            bufmgr_give_back (ctx->bufmgr, buffer);
            return;
        }
    }
    else
        tei = cl_eoc_mactotei_find_tei (ctx, dmac);
#else
    tei = cl_mactotei_table_find_tei_and_tag_from_mac (ctx, dmac, &group);
#endif

    dbg_assert (tei != ctx->mac_config->tei);

    if (tei == MAC_TEI_BCAST && ctx->groups.nb)
    {
        if (ctx->groups.nb_actual_members[group] > 1)
        {
            cl_send_multiunicast (ctx, buffer, length, 0, tag, arrival_time_ntb,
                                  group);
            return;
        }
        else
            tei = ctx->groups.member_tei[group][0];
    }

    /* If the TEI is not found the packet is send as broadcast. */
    cl_data_send_with_tei (ctx, buffer, length,
                           tei == MAC_TEI_UNASSOCIATED ? MAC_TEI_BCAST: tei,
                           tag, arrival_time_ntb, dmac, smac);
}

#if CONFIG_CL_MCAST
PRIVATE void
cl_data_send_group (cl_t *ctx, u8 *buffer, uint length, uint tag,
                    u32 arrival_time_ntb, mac_t dmac)
{
    uint i;
    /* Get the group from the IGMP local table. */
    for (i = 0; i < ctx->groups.nb; i++)
    {
        if (ctx->groups.group_mac[i] == dmac
            && ctx->groups.nb_actual_members[i])
        {
            cl_send_multiunicast (
                ctx, buffer, length, false, tag, arrival_time_ntb, i);
            return;
        }
    }
    /* No group drop it. */
    CL_TRACE (MULTICAST_NOT_FOUND, TRACE_U64 (dmac));
    bufmgr_give_back (ctx->bufmgr, buffer);
}
#endif /* CONFIG_CL_MCAST */

void
cl_data_send (cl_t *ctx, u8 *buffer, uint length, uint tag,
              u32 arrival_time_ntb)
{
    mac_t smac, dmac;
    dbg_claim (ctx);
    dbg_claim (buffer);
    dbg_claim (length >= ETH_PACKET_MIN_SIZE_ALLOWED
               && length <= ETH_PACKET_MAX_SIZE);
    /* check overload based on tag. */
    if (!blk_tagged_slack (tag))
    {
        CL_TRACE (DATA_SEND_DROP_PRIO, phy_date (), tag, buffer, length);
        ctx->stats.tx_data_drop_prio++;
        bufmgr_give_back (ctx->bufmgr, buffer);
        return;
    }
    /* Increase bytes count. */
    ctx->stats.tx_data_bytes += length;
    /* Check sequence number of throughput. */
    lib_seq_check_packet (&ctx->seq_check_tx_ctx, buffer, length);
    /* Get macs. */
    bitstream_direct_read_macs (buffer, &dmac, &smac);
    if (ctx->data_send_link.mfs
        && ctx->data_send_link.mfs->fsm_state != MFS_FSM_CMD_RELEASE
        && dmac == ctx->data_send_link.dmac
        && smac == ctx->data_send_link.smac
        && tag == ctx->data_send_link.tag
        && ctx->mac_config->authenticated
        && less_mod2p32 (arrival_time_ntb,
                         ctx->data_send_link.last_update_date_ntb
                         + MAC_MS_TO_TCK (CL_DATA_SEND_EXCEED_TIME_MS)))
    {
        CL_TRACE (DATA_SEND, phy_date (), buffer, length,
                  ctx->data_send_link.mfs->common.tei,
                  ctx->data_send_link.mfs->common.lid,
                  ctx->data_send_link.mfs->common.bcast);
#if CONFIG_MAC_COMMON_EOC_MFS
        if (ctx->data_send_link.mfs->seg_nb >= MFS_TX_MAX_SEG_NB)
        {
            CL_TRACE (DATA_SEND_DROP_MFS, phy_date (),
                ctx->data_send_link.mfs->common.tei,
                ctx->data_send_link.mfs->common.lid, buffer, length);
            ctx->stats.tx_data_drop_mfs++;
        }
        else
#endif
        {
            ctx->stats.tx_data++;
            sar_msdu_process (ctx->sar, buffer, length,
                              ctx->data_send_link.mfs, arrival_time_ntb);
            /* update data rate informations associated to the TX
             * from the local sta to the associated sta */
            cl_compute_datarate_on_sta (
                ctx, PARENT_OF (mfs_t, tx, ctx->data_send_link.mfs), length);
        }
        /* SAR ends using the buffer it can be given back.
         * BRGDMA is polled by the SAR thats why it the buffer can be given
         * back to the buffer manager. */
        bufmgr_give_back (ctx->bufmgr, buffer);
    }
    else
    {
        /* Release the MFS's reference. */
        if (ctx->data_send_link.mfs)
        {
            blk_release (ctx->data_send_link.mfs);
            ctx->data_send_link.mfs = NULL;
        }
#if CONFIG_CL_MCAST
        if (mac_is_multicast (dmac)
            && dmac != MAC_BROADCAST)
        {
            cl_data_send_group (
                ctx, buffer, length, tag, arrival_time_ntb, dmac);
            ctx->stats.tx_data_multicast++;
        }
        else
#endif
            cl_data_send_prepare (
                ctx, buffer, length, tag, arrival_time_ntb, dmac, smac);
    }
}

void
cl_mme_send (cl_t *ctx, u8 *buffer, uint length, uint tei)
{
    dbg_claim (ctx);
    dbg_claim (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && length <= ETH_PACKET_MAX_SIZE);

    ctx->stats.tx_mme++;
    /* Unicast or broadcast neighbour.
     * Unicast is allowed even if the station is not associated.
     * The broadcast is only allowed if the station is associated. */
    if (((MAC_TEI_IS_STA (tei) || (tei == MAC_TEI_BCAST)))
        && (MAC_TEI_IS_STA(ctx->mac_config->tei)))
    {
        mfs_tx_t *mfs;
        bool added;
        mfs = mac_store_mfs_add_tx (ctx->mac_store,
                                    tei == MAC_TEI_BCAST ? true : false,
                                    true /* mme */,
                                    MAC_LID_NONE,
                                    tei,
                                    &added);

        if (added)
            sar_mfs_add (ctx->sar, (mfs_t *) mfs);
        CL_TRACE (MME_SEND_UNICAST, phy_date (), length, buffer, tei);
        sar_msdu_process (ctx->sar, buffer, length, mfs, mac_ntb ());
        bufmgr_keep_buffer (ctx->bufmgr, buffer);
        blk_release (mfs);
    }
    /* Handle the unassociated unicast with temporary MFS. */
    else if (MAC_TEI_IS_STA (tei)
             && (ctx->mac_config->tei == MAC_TEI_UNASSOCIATED))
    {
        mfs_tx_t *mfs;

        mfs = &mac_store_mfs_unassociated_add (
            ctx->mac_store, true /* tx */, false /* unicast */,
            true /* mme */, MAC_LID_NONE, tei)->tx;

        dbg_assert (ctx->sar);
        sar_mfs_add (ctx->sar, (mfs_t *) mfs);

        CL_TRACE (MME_SEND_UNASSOC_UNICAST, phy_date (), length, buffer,
                  mfs->common.tei);
        sar_msdu_process (ctx->sar, buffer, length, mfs, mac_ntb());
        bufmgr_keep_buffer (ctx->bufmgr, buffer);
        blk_release (mfs);
    }
    /* It the destination TEI is broadcast and our station is not
     * associated, disallow to send a broadcast packet as a multi unicast.*/
    else if (tei == MAC_TEI_MULTI_UNICAST)
    {
        dbg_assert (ctx->mac_config->tei);
        cl_send_multiunicast (ctx, buffer, length, true /* MME */, 1,
                              mac_ntb(), 0);
    }
    /* the destination is not associated or associated and our station
     * can or not be associated */
    else if ((tei == MAC_TEI_UNASSOCIATED) || (tei == MAC_TEI_BCAST))
    {
        mfs_tx_t *mfs;

        mfs = &mac_store_mfs_unassociated_add (
            ctx->mac_store, true /* tx */, true /* bcast */,
            true /* mme */, MAC_LID_NONE, MAC_TEI_BCAST)->tx;

        /* Quick hack to send the CC_ASSOC.CNF before the
         * CC_SET_TEI_MAP.IND. */
        mfs->cap = 3;

        dbg_assert (ctx->sar);
        sar_mfs_add (ctx->sar, (mfs_t *) mfs);

        CL_TRACE (MME_SEND_TO_UNASSOC, phy_date (), length, buffer,
                  mfs->common.tei);
        sar_msdu_process (ctx->sar, buffer, length, mfs, mac_ntb());
        bufmgr_keep_buffer (ctx->bufmgr, buffer);
        blk_release (mfs);
    }
    else
    {
        CL_TRACE (MME_SEND_TO_DRIVER, phy_date (), length, buffer);
        cl_mbx_ipmbox_send (ctx->mbx, buffer, length);
    }
}

void
cl_lib_seq_check_tx_cb (void *user, uint vlan, uint seq_expected,
                        uint seq_actual)
{
    dbg_assert (user);
    trace_do (cl_t *ctx = (cl_t *) user);
    CL_TRACE (SEQ_CHECK_TX, vlan, seq_expected, seq_actual);
}