Prepare include headers
[cascardo/ovs.git] / lib / packet-dpif.c
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "packet-dpif.h"
19
20 #include "ofpbuf.h"
21
22 struct dpif_packet *
23 dpif_packet_new_with_headroom(size_t size, size_t headroom)
24 {
25     struct dpif_packet *p = xmalloc(sizeof *p);
26     struct ofpbuf *b = &p->ofpbuf;
27
28     ofpbuf_init(b, size + headroom);
29     ofpbuf_reserve(b, headroom);
30
31     return p;
32 }
33
34 struct dpif_packet *
35 dpif_packet_clone_from_ofpbuf(const struct ofpbuf *b)
36 {
37     struct dpif_packet *p = xmalloc(sizeof *p);
38     size_t headroom = ofpbuf_headroom(b);
39
40     ofpbuf_init(&p->ofpbuf, ofpbuf_size(b) + headroom);
41     ofpbuf_reserve(&p->ofpbuf, headroom);
42
43     ofpbuf_put(&p->ofpbuf, ofpbuf_data(b), ofpbuf_size(b));
44
45     if (b->frame) {
46         uintptr_t data_delta
47             = (char *)ofpbuf_data(&p->ofpbuf) - (char *)ofpbuf_data(b);
48
49         p->ofpbuf.frame = (char *) b->frame + data_delta;
50     }
51     p->ofpbuf.l2_5_ofs = b->l2_5_ofs;
52     p->ofpbuf.l3_ofs = b->l3_ofs;
53     p->ofpbuf.l4_ofs = b->l4_ofs;
54
55     return p;
56 }
57
58 struct dpif_packet *
59 dpif_packet_clone(struct dpif_packet *p)
60 {
61     struct dpif_packet *newp;
62
63     newp = dpif_packet_clone_from_ofpbuf(&p->ofpbuf);
64
65     newp->dp_hash = p->dp_hash;
66
67     return newp;
68 }