iPXE
peerdist.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdio.h>
27 #include <ipxe/http.h>
28 #include <ipxe/settings.h>
29 #include <ipxe/peermux.h>
30 
31 /** @file
32  *
33  * Peer Content Caching and Retrieval (PeerDist) protocol
34  *
35  * This is quite possibly the ugliest protocol I have ever had the
36  * misfortune to encounter, and I've encountered multicast TFTP.
37  */
38 
39 /** PeerDist is globally enabled */
40 static long peerdist_enabled = 1;
41 
42 /**
43  * Check whether or not to support PeerDist encoding for this request
44  *
45  * @v http HTTP transaction
46  * @ret supported PeerDist encoding is supported for this request
47  */
48 static int http_peerdist_supported ( struct http_transaction *http ) {
49 
50  /* Allow PeerDist to be globally enabled/disabled */
51  if ( ! peerdist_enabled )
52  return 0;
53 
54  /* Support PeerDist encoding only if we can directly access an
55  * underlying data transfer buffer. Direct access is required
56  * in order to support decryption of data received via the
57  * retrieval protocol (which provides the AES initialisation
58  * vector only after all of the encrypted data has been
59  * received).
60  *
61  * This test simultaneously ensures that we do not attempt to
62  * use PeerDist encoding on a request which is itself a
63  * PeerDist individual block download, since the individual
64  * block downloads do not themselves provide direct access to
65  * an underlying data transfer buffer.
66  */
67  return ( xfer_buffer ( &http->xfer ) != NULL );
68 }
69 
70 /**
71  * Format HTTP "X-P2P-PeerDist" header
72  *
73  * @v http HTTP transaction
74  * @v buf Buffer
75  * @v len Length of buffer
76  * @ret len Length of header value, or negative error
77  */
78 static int http_format_p2p_peerdist ( struct http_transaction *http,
79  char *buf, size_t len ) {
80  int supported = http_peerdist_supported ( http );
81  int missing;
82 
83  /* PeerDist wants us to inform the server whenever we make a
84  * request for data that was missing from local peers
85  * (presumably for statistical purposes only). We use the
86  * heuristic of assuming that the combination of "this request
87  * may not itself use PeerDist content encoding" and "this is
88  * a range request" probably indicates that we are making a
89  * PeerDist block raw range request for missing data.
90  */
91  missing = ( http->request.range.len && ( ! supported ) );
92 
93  /* Omit header if PeerDist encoding is not supported and we
94  * are not reporting a missing data request.
95  */
96  if ( ! ( supported || missing ) )
97  return 0;
98 
99  /* Construct header */
100  return snprintf ( buf, len, "Version=1.1%s",
101  ( missing ? ", MissingDataRequest=true" : "" ) );
102 }
103 
104 /** HTTP "X-P2P-PeerDist" header */
105 struct http_request_header http_request_p2p_peerdist __http_request_header = {
106  .name = "X-P2P-PeerDist",
107  .format = http_format_p2p_peerdist,
108 };
109 
110 /**
111  * Format HTTP "X-P2P-PeerDistEx" header
112  *
113  * @v http HTTP transaction
114  * @v buf Buffer
115  * @v len Length of buffer
116  * @ret len Length of header value, or negative error
117  */
119  char *buf, size_t len ) {
120  int supported = http_peerdist_supported ( http );
121 
122  /* Omit header if PeerDist encoding is not supported */
123  if ( ! supported )
124  return 0;
125 
126  /* Construct header */
127  return snprintf ( buf, len, ( "MinContentInformation=1.0, "
128  "MaxContentInformation=2.0" ) );
129 }
130 
131 /** HTTP "X-P2P-PeerDist" header */
132 struct http_request_header http_request_p2p_peerdistex __http_request_header = {
133  .name = "X-P2P-PeerDistEx",
134  .format = http_format_p2p_peerdistex,
135 };
136 
137 /**
138  * Initialise PeerDist content encoding
139  *
140  * @v http HTTP transaction
141  * @ret rc Return status code
142  */
143 static int http_peerdist_init ( struct http_transaction *http ) {
144 
145  return peermux_filter ( &http->content, &http->transfer, http->uri );
146 }
147 
148 /** PeerDist HTTP content encoding */
149 struct http_content_encoding peerdist_encoding __http_content_encoding = {
150  .name = "peerdist",
151  .supported = http_peerdist_supported,
152  .init = http_peerdist_init,
153 };
154 
155 /** PeerDist enabled setting */
156 const struct setting peerdist_setting __setting ( SETTING_MISC, peerdist ) = {
157  .name = "peerdist",
158  .description = "PeerDist enabled",
159  .type = &setting_type_int8,
160 };
161 
162 /**
163  * Apply PeerDist settings
164  *
165  * @ret rc Return status code
166  */
167 static int apply_peerdist_settings ( void ) {
168 
169  /* Fetch global PeerDist enabled setting */
170  if ( fetch_int_setting ( NULL, &peerdist_setting,
171  &peerdist_enabled ) < 0 ) {
172  peerdist_enabled = 1;
173  }
174  DBGC ( &peerdist_enabled, "PEERDIST is %s\n",
175  ( peerdist_enabled ? "enabled" : "disabled" ) );
176 
177  return 0;
178 }
179 
180 /** PeerDist settings applicator */
181 struct settings_applicator peerdist_applicator __settings_applicator = {
183 };
struct interface xfer
Data transfer interface.
Definition: http.h:421
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static int apply_peerdist_settings(void)
Apply PeerDist settings.
Definition: peerdist.c:167
static int http_format_p2p_peerdist(struct http_transaction *http, char *buf, size_t len)
Format HTTP "X-P2P-PeerDist" header.
Definition: peerdist.c:78
int peermux_filter(struct interface *xfer, struct interface *info, struct uri *uri)
Add PeerDist content-encoding filter.
Definition: peermux.c:411
A data transfer buffer.
Definition: xferbuf.h:19
const char * name
Header name (e.g.
Definition: http.h:230
#define DBGC(...)
Definition: compiler.h:505
struct uri * uri
Request URI.
Definition: http.h:434
struct http_request_header http_request_p2p_peerdist __http_request_header
HTTP "X-P2P-PeerDist" header.
Definition: peerdist.c:105
A settings applicator.
Definition: settings.h:251
static long peerdist_enabled
PeerDist is globally enabled.
Definition: peerdist.c:40
#define SETTING_MISC
Miscellaneous settings.
Definition: settings.h:80
An HTTP request header.
Definition: http.h:228
int fetch_int_setting(struct settings *settings, const struct setting *setting, long *value)
Fetch value of signed integer setting.
Definition: settings.c:1022
An HTTP content encoding.
Definition: http.h:486
struct http_request_range range
Range descriptor.
Definition: http.h:220
struct http_request request
Request.
Definition: http.h:436
const char * name
Name.
Definition: settings.h:28
An HTTP transaction.
Definition: http.h:417
Hyper Text Transport Protocol.
Peer Content Caching and Retrieval (PeerDist) protocol multiplexer.
const char * name
Name.
Definition: http.h:488
Configuration settings.
struct interface content
Content-decoded interface.
Definition: http.h:423
struct interface transfer
Transfer-decoded interface.
Definition: http.h:425
A setting.
Definition: settings.h:23
size_t len
Range length, or zero for no range request.
Definition: http.h:141
uint32_t len
Length.
Definition: ena.h:14
static int http_format_p2p_peerdistex(struct http_transaction *http, char *buf, size_t len)
Format HTTP "X-P2P-PeerDistEx" header.
Definition: peerdist.c:118
static int http_peerdist_init(struct http_transaction *http)
Initialise PeerDist content encoding.
Definition: peerdist.c:143
struct http_content_encoding peerdist_encoding __http_content_encoding
PeerDist HTTP content encoding.
Definition: peerdist.c:149
int(* apply)(void)
Apply updated settings.
Definition: settings.h:256
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
static int http_peerdist_supported(struct http_transaction *http)
Check whether or not to support PeerDist encoding for this request.
Definition: peerdist.c:48
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct settings_applicator peerdist_applicator __settings_applicator
PeerDist settings applicator.
Definition: peerdist.c:181
const struct setting peerdist_setting __setting(SETTING_MISC, peerdist)
PeerDist enabled setting.