~ubuntu-branches/ubuntu/vivid/mstflint/vivid-proposed

« back to all changes in this revision

Viewing changes to mtcr_ul/packets_common.c

  • Committer: Package Import Robot
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2014-07-04 15:39:23 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140704153923-khknwv3o1jeap3oo
Tags: 3.7.0-1
* New upstream release: 3.7.0-1.10.gdf7ec73
* Add build depends on libibmad-dev and autotools-dev.
* Remove build depends on automake and libtool.
* Switch to dh 9 and source format version 3.0
* Remove placeholder manpages.
* Remove flag DM-Upload-Allowed.
* Remove all current Uploaders, they're welcome back anytime.
  Add myself to Uploaders.
* Bump Standards-Version to 3.9.5 (no changes required).
* Update homepage.
* Add a watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved.
 
3
 *
 
4
 * This software is available to you under a choice of one of two
 
5
 * licenses.  You may choose to be licensed under the terms of the GNU
 
6
 * General Public License (GPL) Version 2, available from the file
 
7
 * COPYING in the main directory of this source tree, or the
 
8
 * OpenIB.org BSD license below:
 
9
 *
 
10
 *     Redistribution and use in source and binary forms, with or
 
11
 *     without modification, are permitted provided that the following
 
12
 *     conditions are met:
 
13
 *
 
14
 *      - Redistributions of source code must retain the above
 
15
 *        copyright notice, this list of conditions and the following
 
16
 *        disclaimer.
 
17
 * 
 
18
 *      - Redistributions in binary form must reproduce the above
 
19
 *        copyright notice, this list of conditions and the following
 
20
 *        disclaimer in the documentation and/or other materials
 
21
 *        provided with the distribution.
 
22
 * 
 
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
24
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
25
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
26
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 
27
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 
28
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
29
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
30
 * SOFTWARE.
 
31
 */
 
32
 
 
33
 
 
34
#include "packets_common.h"
 
35
#include <stdio.h>
 
36
#include <stdlib.h>
 
37
#include <string.h>
 
38
#include <sys/types.h>
 
39
#ifdef __WIN__
 
40
    #include <Winsock2.h>   // for htonl
 
41
#endif
 
42
 
 
43
 
 
44
/************************************/
 
45
void push_to_buff_64(u_int8_t *buff, u_int32_t bit_offset, u_int64_t field_value)
 
46
{
 
47
#if defined(__ia64__)
 
48
    u_int32_t *buffer = PTR_32_OF_BUFF(buff, (bit_offset / 8));
 
49
    u_int64_t value = CPU_TO_BE64(field_value);
 
50
    memcpy(buffer, &value, sizeof(value));
 
51
#else
 
52
    u_int64_t *buffer = PTR_64_OF_BUFF(buff, (bit_offset / 8));
 
53
    memcpy(buffer, &field_value, sizeof(field_value));
 
54
    *buffer = CPU_TO_BE64(*buffer);
 
55
#endif
 
56
}
 
57
 
 
58
 
 
59
/************************************/
 
60
void push_to_buff_32(u_int8_t *buff, u_int32_t bit_offset, u_int32_t field_value)
 
61
{
 
62
    u_int32_t *buffer = PTR_32_OF_BUFF(buff, (bit_offset / 8));
 
63
    memcpy(buffer, &field_value, sizeof(field_value));
 
64
    *buffer = CPU_TO_BE32(*buffer);
 
65
}
 
66
 
 
67
 
 
68
/************************************/
 
69
//the next function will push the field into the buffer by inserting it's MSB bits first
 
70
//and therefore by doing it we save the CPU_TO_BE operation
 
71
void push_to_buff(u_int8_t *buff, u_int32_t bit_offset, u_int32_t field_size, u_int32_t field_value)
 
72
{
 
73
    u_int32_t i                 = 0;
 
74
    u_int32_t byte_n    = bit_offset / 8;
 
75
    u_int32_t byte_n_offset     = bit_offset % 8;
 
76
    u_int32_t to_push;
 
77
 
 
78
    //going over all bits in field
 
79
    while (i < field_size) {
 
80
        to_push = PCK_MIN(8 - byte_n_offset, field_size - i);
 
81
        i += to_push;
 
82
        //printf("Inserting %u bits(%u) to byte %u to bit %u\n", to_push, EXTRACT8(field_value, field_size - i, to_push), byte_n, 8 - to_push - byte_n_offset);
 
83
        INSERTF_8(BYTE_N(buff, byte_n), 8 - to_push - byte_n_offset, field_value, field_size - i, to_push);
 
84
        byte_n_offset  = 0;     //(byte_n_offset + to_push) % 8;
 
85
        byte_n++;
 
86
    }
 
87
}
 
88
 
 
89
 
 
90
/************************************/
 
91
u_int64_t pop_from_buff_64(u_int8_t *buff, u_int32_t bit_offset)
 
92
{
 
93
#if defined(__ia64__)
 
94
    u_int64_t value = 0;
 
95
    memcpy(&value, PTR_32_OF_BUFF(buff, (bit_offset / 8)), sizeof(u_int64_t));
 
96
    return (BE64_TO_CPU(value));
 
97
#else
 
98
    return (BE64_TO_CPU(FIELD_64_OF_BUFF(buff, (bit_offset / 8))));
 
99
#endif
 
100
}
 
101
 
 
102
 
 
103
/************************************/
 
104
u_int32_t pop_from_buff_32(u_int8_t *buff, u_int32_t bit_offset)
 
105
{
 
106
    return (BE32_TO_CPU(FIELD_32_OF_BUFF(buff, (bit_offset / 8))));
 
107
}
 
108
 
 
109
 
 
110
/************************************/
 
111
//the next function will pop the field into the buffer by removing it's MSB bits first
 
112
//and therefore by doing it we save the BE_TO_CPU operation
 
113
u_int32_t pop_from_buff(u_int8_t *buff, u_int32_t bit_offset, u_int32_t field_size)
 
114
{
 
115
    u_int32_t i                 = 0;
 
116
    u_int32_t byte_n    = bit_offset / 8;
 
117
    u_int32_t byte_n_offset     = bit_offset % 8;
 
118
    u_int32_t field_32  = 0;
 
119
    u_int32_t to_pop;
 
120
 
 
121
    //going over all bits in field
 
122
    while (i < field_size) {
 
123
        to_pop =  PCK_MIN(8 - byte_n_offset, field_size - i);
 
124
        i += to_pop;
 
125
        //printf("Removing %u bits(%u) from byte %u to bit %u\n", to_pop, EXTRACT8(BYTE_N(buff, byte_n), 8 - to_pop - byte_n_offset, to_pop), byte_n, field_size - i);
 
126
        INSERTF_8(field_32, field_size - i, BYTE_N(buff, byte_n), 8 - to_pop - byte_n_offset, to_pop);
 
127
        byte_n_offset  = 0;     //(byte_n_offset + to_pop) % 8;
 
128
        byte_n++;
 
129
    }
 
130
    return field_32;
 
131
}
 
132
 
 
133