datapath.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  1. /*
  2. * Copyright (c) 2007-2012 Nicira Networks.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <linux/workqueue.h>
  49. #include <net/genetlink.h>
  50. #include "datapath.h"
  51. #include "flow.h"
  52. #include "vport-internal_dev.h"
  53. /**
  54. * DOC: Locking:
  55. *
  56. * Writes to device state (add/remove datapath, port, set operations on vports,
  57. * etc.) are protected by RTNL.
  58. *
  59. * Writes to other state (flow table modifications, set miscellaneous datapath
  60. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  61. * genl_mutex.
  62. *
  63. * Reads are protected by RCU.
  64. *
  65. * There are a few special cases (mostly stats) that have their own
  66. * synchronization but they nest under all of above and don't interact with
  67. * each other.
  68. */
  69. /* Global list of datapaths to enable dumping them all out.
  70. * Protected by genl_mutex.
  71. */
  72. static LIST_HEAD(dps);
  73. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  74. static void rehash_flow_table(struct work_struct *work);
  75. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  76. static struct vport *new_vport(const struct vport_parms *);
  77. static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
  78. const struct dp_upcall_info *);
  79. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
  80. const struct dp_upcall_info *);
  81. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  82. static struct datapath *get_dp(int dp_ifindex)
  83. {
  84. struct datapath *dp = NULL;
  85. struct net_device *dev;
  86. rcu_read_lock();
  87. dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
  88. if (dev) {
  89. struct vport *vport = ovs_internal_dev_get_vport(dev);
  90. if (vport)
  91. dp = vport->dp;
  92. }
  93. rcu_read_unlock();
  94. return dp;
  95. }
  96. /* Must be called with rcu_read_lock or RTNL lock. */
  97. const char *ovs_dp_name(const struct datapath *dp)
  98. {
  99. struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
  100. return vport->ops->get_name(vport);
  101. }
  102. static int get_dpifindex(struct datapath *dp)
  103. {
  104. struct vport *local;
  105. int ifindex;
  106. rcu_read_lock();
  107. local = rcu_dereference(dp->ports[OVSP_LOCAL]);
  108. if (local)
  109. ifindex = local->ops->get_ifindex(local);
  110. else
  111. ifindex = 0;
  112. rcu_read_unlock();
  113. return ifindex;
  114. }
  115. static void destroy_dp_rcu(struct rcu_head *rcu)
  116. {
  117. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  118. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  119. free_percpu(dp->stats_percpu);
  120. kfree(dp);
  121. }
  122. /* Called with RTNL lock and genl_lock. */
  123. static struct vport *new_vport(const struct vport_parms *parms)
  124. {
  125. struct vport *vport;
  126. vport = ovs_vport_add(parms);
  127. if (!IS_ERR(vport)) {
  128. struct datapath *dp = parms->dp;
  129. rcu_assign_pointer(dp->ports[parms->port_no], vport);
  130. list_add(&vport->node, &dp->port_list);
  131. }
  132. return vport;
  133. }
  134. /* Called with RTNL lock. */
  135. void ovs_dp_detach_port(struct vport *p)
  136. {
  137. ASSERT_RTNL();
  138. /* First drop references to device. */
  139. list_del(&p->node);
  140. rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
  141. /* Then destroy it. */
  142. ovs_vport_del(p);
  143. }
  144. /* Must be called with rcu_read_lock. */
  145. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  146. {
  147. struct datapath *dp = p->dp;
  148. struct sw_flow *flow;
  149. struct dp_stats_percpu *stats;
  150. struct sw_flow_key key;
  151. u64 *stats_counter;
  152. int error;
  153. int key_len;
  154. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  155. /* Extract flow from 'skb' into 'key'. */
  156. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  157. if (unlikely(error)) {
  158. kfree_skb(skb);
  159. return;
  160. }
  161. /* Look up flow. */
  162. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  163. if (unlikely(!flow)) {
  164. struct dp_upcall_info upcall;
  165. upcall.cmd = OVS_PACKET_CMD_MISS;
  166. upcall.key = &key;
  167. upcall.userdata = NULL;
  168. upcall.pid = p->upcall_pid;
  169. ovs_dp_upcall(dp, skb, &upcall);
  170. consume_skb(skb);
  171. stats_counter = &stats->n_missed;
  172. goto out;
  173. }
  174. OVS_CB(skb)->flow = flow;
  175. stats_counter = &stats->n_hit;
  176. ovs_flow_used(OVS_CB(skb)->flow, skb);
  177. ovs_execute_actions(dp, skb);
  178. out:
  179. /* Update datapath statistics. */
  180. u64_stats_update_begin(&stats->sync);
  181. (*stats_counter)++;
  182. u64_stats_update_end(&stats->sync);
  183. }
  184. static struct genl_family dp_packet_genl_family = {
  185. .id = GENL_ID_GENERATE,
  186. .hdrsize = sizeof(struct ovs_header),
  187. .name = OVS_PACKET_FAMILY,
  188. .version = OVS_PACKET_VERSION,
  189. .maxattr = OVS_PACKET_ATTR_MAX
  190. };
  191. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  192. const struct dp_upcall_info *upcall_info)
  193. {
  194. struct dp_stats_percpu *stats;
  195. int dp_ifindex;
  196. int err;
  197. if (upcall_info->pid == 0) {
  198. err = -ENOTCONN;
  199. goto err;
  200. }
  201. dp_ifindex = get_dpifindex(dp);
  202. if (!dp_ifindex) {
  203. err = -ENODEV;
  204. goto err;
  205. }
  206. if (!skb_is_gso(skb))
  207. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  208. else
  209. err = queue_gso_packets(dp_ifindex, skb, upcall_info);
  210. if (err)
  211. goto err;
  212. return 0;
  213. err:
  214. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  215. u64_stats_update_begin(&stats->sync);
  216. stats->n_lost++;
  217. u64_stats_update_end(&stats->sync);
  218. return err;
  219. }
  220. static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
  221. const struct dp_upcall_info *upcall_info)
  222. {
  223. struct dp_upcall_info later_info;
  224. struct sw_flow_key later_key;
  225. struct sk_buff *segs, *nskb;
  226. int err;
  227. segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
  228. if (IS_ERR(skb))
  229. return PTR_ERR(skb);
  230. /* Queue all of the segments. */
  231. skb = segs;
  232. do {
  233. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  234. if (err)
  235. break;
  236. if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
  237. /* The initial flow key extracted by ovs_flow_extract()
  238. * in this case is for a first fragment, so we need to
  239. * properly mark later fragments.
  240. */
  241. later_key = *upcall_info->key;
  242. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  243. later_info = *upcall_info;
  244. later_info.key = &later_key;
  245. upcall_info = &later_info;
  246. }
  247. } while ((skb = skb->next));
  248. /* Free all of the segments. */
  249. skb = segs;
  250. do {
  251. nskb = skb->next;
  252. if (err)
  253. kfree_skb(skb);
  254. else
  255. consume_skb(skb);
  256. } while ((skb = nskb));
  257. return err;
  258. }
  259. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
  260. const struct dp_upcall_info *upcall_info)
  261. {
  262. struct ovs_header *upcall;
  263. struct sk_buff *nskb = NULL;
  264. struct sk_buff *user_skb; /* to be queued to userspace */
  265. struct nlattr *nla;
  266. unsigned int len;
  267. int err;
  268. if (vlan_tx_tag_present(skb)) {
  269. nskb = skb_clone(skb, GFP_ATOMIC);
  270. if (!nskb)
  271. return -ENOMEM;
  272. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  273. if (!nskb)
  274. return -ENOMEM;
  275. nskb->vlan_tci = 0;
  276. skb = nskb;
  277. }
  278. if (nla_attr_size(skb->len) > USHRT_MAX) {
  279. err = -EFBIG;
  280. goto out;
  281. }
  282. len = sizeof(struct ovs_header);
  283. len += nla_total_size(skb->len);
  284. len += nla_total_size(FLOW_BUFSIZE);
  285. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  286. len += nla_total_size(8);
  287. user_skb = genlmsg_new(len, GFP_ATOMIC);
  288. if (!user_skb) {
  289. err = -ENOMEM;
  290. goto out;
  291. }
  292. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  293. 0, upcall_info->cmd);
  294. upcall->dp_ifindex = dp_ifindex;
  295. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  296. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  297. nla_nest_end(user_skb, nla);
  298. if (upcall_info->userdata)
  299. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  300. nla_get_u64(upcall_info->userdata));
  301. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  302. skb_copy_and_csum_dev(skb, nla_data(nla));
  303. err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
  304. out:
  305. kfree_skb(nskb);
  306. return err;
  307. }
  308. /* Called with genl_mutex. */
  309. static int flush_flows(int dp_ifindex)
  310. {
  311. struct flow_table *old_table;
  312. struct flow_table *new_table;
  313. struct datapath *dp;
  314. dp = get_dp(dp_ifindex);
  315. if (!dp)
  316. return -ENODEV;
  317. old_table = genl_dereference(dp->table);
  318. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  319. if (!new_table)
  320. return -ENOMEM;
  321. rcu_assign_pointer(dp->table, new_table);
  322. ovs_flow_tbl_deferred_destroy(old_table);
  323. return 0;
  324. }
  325. static int validate_actions(const struct nlattr *attr,
  326. const struct sw_flow_key *key, int depth);
  327. static int validate_sample(const struct nlattr *attr,
  328. const struct sw_flow_key *key, int depth)
  329. {
  330. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  331. const struct nlattr *probability, *actions;
  332. const struct nlattr *a;
  333. int rem;
  334. memset(attrs, 0, sizeof(attrs));
  335. nla_for_each_nested(a, attr, rem) {
  336. int type = nla_type(a);
  337. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  338. return -EINVAL;
  339. attrs[type] = a;
  340. }
  341. if (rem)
  342. return -EINVAL;
  343. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  344. if (!probability || nla_len(probability) != sizeof(u32))
  345. return -EINVAL;
  346. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  347. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  348. return -EINVAL;
  349. return validate_actions(actions, key, depth + 1);
  350. }
  351. static int validate_tp_port(const struct sw_flow_key *flow_key)
  352. {
  353. if (flow_key->eth.type == htons(ETH_P_IP)) {
  354. if (flow_key->ipv4.tp.src && flow_key->ipv4.tp.dst)
  355. return 0;
  356. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  357. if (flow_key->ipv6.tp.src && flow_key->ipv6.tp.dst)
  358. return 0;
  359. }
  360. return -EINVAL;
  361. }
  362. static int validate_set(const struct nlattr *a,
  363. const struct sw_flow_key *flow_key)
  364. {
  365. const struct nlattr *ovs_key = nla_data(a);
  366. int key_type = nla_type(ovs_key);
  367. /* There can be only one key in a action */
  368. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  369. return -EINVAL;
  370. if (key_type > OVS_KEY_ATTR_MAX ||
  371. nla_len(ovs_key) != ovs_key_lens[key_type])
  372. return -EINVAL;
  373. switch (key_type) {
  374. const struct ovs_key_ipv4 *ipv4_key;
  375. case OVS_KEY_ATTR_PRIORITY:
  376. case OVS_KEY_ATTR_ETHERNET:
  377. break;
  378. case OVS_KEY_ATTR_IPV4:
  379. if (flow_key->eth.type != htons(ETH_P_IP))
  380. return -EINVAL;
  381. if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
  382. return -EINVAL;
  383. ipv4_key = nla_data(ovs_key);
  384. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  385. return -EINVAL;
  386. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  387. return -EINVAL;
  388. break;
  389. case OVS_KEY_ATTR_TCP:
  390. if (flow_key->ip.proto != IPPROTO_TCP)
  391. return -EINVAL;
  392. return validate_tp_port(flow_key);
  393. case OVS_KEY_ATTR_UDP:
  394. if (flow_key->ip.proto != IPPROTO_UDP)
  395. return -EINVAL;
  396. return validate_tp_port(flow_key);
  397. default:
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. static int validate_userspace(const struct nlattr *attr)
  403. {
  404. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  405. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  406. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  407. };
  408. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  409. int error;
  410. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  411. attr, userspace_policy);
  412. if (error)
  413. return error;
  414. if (!a[OVS_USERSPACE_ATTR_PID] ||
  415. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  416. return -EINVAL;
  417. return 0;
  418. }
  419. static int validate_actions(const struct nlattr *attr,
  420. const struct sw_flow_key *key, int depth)
  421. {
  422. const struct nlattr *a;
  423. int rem, err;
  424. if (depth >= SAMPLE_ACTION_DEPTH)
  425. return -EOVERFLOW;
  426. nla_for_each_nested(a, attr, rem) {
  427. /* Expected argument lengths, (u32)-1 for variable length. */
  428. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  429. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  430. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  431. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  432. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  433. [OVS_ACTION_ATTR_SET] = (u32)-1,
  434. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  435. };
  436. const struct ovs_action_push_vlan *vlan;
  437. int type = nla_type(a);
  438. if (type > OVS_ACTION_ATTR_MAX ||
  439. (action_lens[type] != nla_len(a) &&
  440. action_lens[type] != (u32)-1))
  441. return -EINVAL;
  442. switch (type) {
  443. case OVS_ACTION_ATTR_UNSPEC:
  444. return -EINVAL;
  445. case OVS_ACTION_ATTR_USERSPACE:
  446. err = validate_userspace(a);
  447. if (err)
  448. return err;
  449. break;
  450. case OVS_ACTION_ATTR_OUTPUT:
  451. if (nla_get_u32(a) >= DP_MAX_PORTS)
  452. return -EINVAL;
  453. break;
  454. case OVS_ACTION_ATTR_POP_VLAN:
  455. break;
  456. case OVS_ACTION_ATTR_PUSH_VLAN:
  457. vlan = nla_data(a);
  458. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  459. return -EINVAL;
  460. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  461. return -EINVAL;
  462. break;
  463. case OVS_ACTION_ATTR_SET:
  464. err = validate_set(a, key);
  465. if (err)
  466. return err;
  467. break;
  468. case OVS_ACTION_ATTR_SAMPLE:
  469. err = validate_sample(a, key, depth);
  470. if (err)
  471. return err;
  472. break;
  473. default:
  474. return -EINVAL;
  475. }
  476. }
  477. if (rem > 0)
  478. return -EINVAL;
  479. return 0;
  480. }
  481. static void clear_stats(struct sw_flow *flow)
  482. {
  483. flow->used = 0;
  484. flow->tcp_flags = 0;
  485. flow->packet_count = 0;
  486. flow->byte_count = 0;
  487. }
  488. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  489. {
  490. struct ovs_header *ovs_header = info->userhdr;
  491. struct nlattr **a = info->attrs;
  492. struct sw_flow_actions *acts;
  493. struct sk_buff *packet;
  494. struct sw_flow *flow;
  495. struct datapath *dp;
  496. struct ethhdr *eth;
  497. int len;
  498. int err;
  499. int key_len;
  500. err = -EINVAL;
  501. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  502. !a[OVS_PACKET_ATTR_ACTIONS] ||
  503. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  504. goto err;
  505. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  506. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  507. err = -ENOMEM;
  508. if (!packet)
  509. goto err;
  510. skb_reserve(packet, NET_IP_ALIGN);
  511. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  512. skb_reset_mac_header(packet);
  513. eth = eth_hdr(packet);
  514. /* Normally, setting the skb 'protocol' field would be handled by a
  515. * call to eth_type_trans(), but it assumes there's a sending
  516. * device, which we may not have. */
  517. if (ntohs(eth->h_proto) >= 1536)
  518. packet->protocol = eth->h_proto;
  519. else
  520. packet->protocol = htons(ETH_P_802_2);
  521. /* Build an sw_flow for sending this packet. */
  522. flow = ovs_flow_alloc();
  523. err = PTR_ERR(flow);
  524. if (IS_ERR(flow))
  525. goto err_kfree_skb;
  526. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  527. if (err)
  528. goto err_flow_free;
  529. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  530. &flow->key.phy.in_port,
  531. a[OVS_PACKET_ATTR_KEY]);
  532. if (err)
  533. goto err_flow_free;
  534. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  535. if (err)
  536. goto err_flow_free;
  537. flow->hash = ovs_flow_hash(&flow->key, key_len);
  538. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  539. err = PTR_ERR(acts);
  540. if (IS_ERR(acts))
  541. goto err_flow_free;
  542. rcu_assign_pointer(flow->sf_acts, acts);
  543. OVS_CB(packet)->flow = flow;
  544. packet->priority = flow->key.phy.priority;
  545. rcu_read_lock();
  546. dp = get_dp(ovs_header->dp_ifindex);
  547. err = -ENODEV;
  548. if (!dp)
  549. goto err_unlock;
  550. local_bh_disable();
  551. err = ovs_execute_actions(dp, packet);
  552. local_bh_enable();
  553. rcu_read_unlock();
  554. ovs_flow_free(flow);
  555. return err;
  556. err_unlock:
  557. rcu_read_unlock();
  558. err_flow_free:
  559. ovs_flow_free(flow);
  560. err_kfree_skb:
  561. kfree_skb(packet);
  562. err:
  563. return err;
  564. }
  565. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  566. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  567. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  568. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  569. };
  570. static struct genl_ops dp_packet_genl_ops[] = {
  571. { .cmd = OVS_PACKET_CMD_EXECUTE,
  572. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  573. .policy = packet_policy,
  574. .doit = ovs_packet_cmd_execute
  575. }
  576. };
  577. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  578. {
  579. int i;
  580. struct flow_table *table = genl_dereference(dp->table);
  581. stats->n_flows = ovs_flow_tbl_count(table);
  582. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  583. for_each_possible_cpu(i) {
  584. const struct dp_stats_percpu *percpu_stats;
  585. struct dp_stats_percpu local_stats;
  586. unsigned int start;
  587. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  588. do {
  589. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  590. local_stats = *percpu_stats;
  591. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  592. stats->n_hit += local_stats.n_hit;
  593. stats->n_missed += local_stats.n_missed;
  594. stats->n_lost += local_stats.n_lost;
  595. }
  596. }
  597. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  598. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  599. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  600. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  601. };
  602. static struct genl_family dp_flow_genl_family = {
  603. .id = GENL_ID_GENERATE,
  604. .hdrsize = sizeof(struct ovs_header),
  605. .name = OVS_FLOW_FAMILY,
  606. .version = OVS_FLOW_VERSION,
  607. .maxattr = OVS_FLOW_ATTR_MAX
  608. };
  609. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  610. .name = OVS_FLOW_MCGROUP
  611. };
  612. /* Called with genl_lock. */
  613. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  614. struct sk_buff *skb, u32 pid,
  615. u32 seq, u32 flags, u8 cmd)
  616. {
  617. const int skb_orig_len = skb->len;
  618. const struct sw_flow_actions *sf_acts;
  619. struct ovs_flow_stats stats;
  620. struct ovs_header *ovs_header;
  621. struct nlattr *nla;
  622. unsigned long used;
  623. u8 tcp_flags;
  624. int err;
  625. sf_acts = rcu_dereference_protected(flow->sf_acts,
  626. lockdep_genl_is_held());
  627. ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
  628. if (!ovs_header)
  629. return -EMSGSIZE;
  630. ovs_header->dp_ifindex = get_dpifindex(dp);
  631. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  632. if (!nla)
  633. goto nla_put_failure;
  634. err = ovs_flow_to_nlattrs(&flow->key, skb);
  635. if (err)
  636. goto error;
  637. nla_nest_end(skb, nla);
  638. spin_lock_bh(&flow->lock);
  639. used = flow->used;
  640. stats.n_packets = flow->packet_count;
  641. stats.n_bytes = flow->byte_count;
  642. tcp_flags = flow->tcp_flags;
  643. spin_unlock_bh(&flow->lock);
  644. if (used &&
  645. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  646. goto nla_put_failure;
  647. if (stats.n_packets &&
  648. nla_put(skb, OVS_FLOW_ATTR_STATS,
  649. sizeof(struct ovs_flow_stats), &stats))
  650. goto nla_put_failure;
  651. if (tcp_flags &&
  652. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  653. goto nla_put_failure;
  654. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  655. * this is the first flow to be dumped into 'skb'. This is unusual for
  656. * Netlink but individual action lists can be longer than
  657. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  658. * The userspace caller can always fetch the actions separately if it
  659. * really wants them. (Most userspace callers in fact don't care.)
  660. *
  661. * This can only fail for dump operations because the skb is always
  662. * properly sized for single flows.
  663. */
  664. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  665. sf_acts->actions);
  666. if (err < 0 && skb_orig_len)
  667. goto error;
  668. return genlmsg_end(skb, ovs_header);
  669. nla_put_failure:
  670. err = -EMSGSIZE;
  671. error:
  672. genlmsg_cancel(skb, ovs_header);
  673. return err;
  674. }
  675. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  676. {
  677. const struct sw_flow_actions *sf_acts;
  678. int len;
  679. sf_acts = rcu_dereference_protected(flow->sf_acts,
  680. lockdep_genl_is_held());
  681. /* OVS_FLOW_ATTR_KEY */
  682. len = nla_total_size(FLOW_BUFSIZE);
  683. /* OVS_FLOW_ATTR_ACTIONS */
  684. len += nla_total_size(sf_acts->actions_len);
  685. /* OVS_FLOW_ATTR_STATS */
  686. len += nla_total_size(sizeof(struct ovs_flow_stats));
  687. /* OVS_FLOW_ATTR_TCP_FLAGS */
  688. len += nla_total_size(1);
  689. /* OVS_FLOW_ATTR_USED */
  690. len += nla_total_size(8);
  691. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  692. return genlmsg_new(len, GFP_KERNEL);
  693. }
  694. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  695. struct datapath *dp,
  696. u32 pid, u32 seq, u8 cmd)
  697. {
  698. struct sk_buff *skb;
  699. int retval;
  700. skb = ovs_flow_cmd_alloc_info(flow);
  701. if (!skb)
  702. return ERR_PTR(-ENOMEM);
  703. retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
  704. BUG_ON(retval < 0);
  705. return skb;
  706. }
  707. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  708. {
  709. struct nlattr **a = info->attrs;
  710. struct ovs_header *ovs_header = info->userhdr;
  711. struct sw_flow_key key;
  712. struct sw_flow *flow;
  713. struct sk_buff *reply;
  714. struct datapath *dp;
  715. struct flow_table *table;
  716. int error;
  717. int key_len;
  718. /* Extract key. */
  719. error = -EINVAL;
  720. if (!a[OVS_FLOW_ATTR_KEY])
  721. goto error;
  722. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  723. if (error)
  724. goto error;
  725. /* Validate actions. */
  726. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  727. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  728. if (error)
  729. goto error;
  730. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  731. error = -EINVAL;
  732. goto error;
  733. }
  734. dp = get_dp(ovs_header->dp_ifindex);
  735. error = -ENODEV;
  736. if (!dp)
  737. goto error;
  738. table = genl_dereference(dp->table);
  739. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  740. if (!flow) {
  741. struct sw_flow_actions *acts;
  742. /* Bail out if we're not allowed to create a new flow. */
  743. error = -ENOENT;
  744. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  745. goto error;
  746. /* Expand table, if necessary, to make room. */
  747. if (ovs_flow_tbl_need_to_expand(table)) {
  748. struct flow_table *new_table;
  749. new_table = ovs_flow_tbl_expand(table);
  750. if (!IS_ERR(new_table)) {
  751. rcu_assign_pointer(dp->table, new_table);
  752. ovs_flow_tbl_deferred_destroy(table);
  753. table = genl_dereference(dp->table);
  754. }
  755. }
  756. /* Allocate flow. */
  757. flow = ovs_flow_alloc();
  758. if (IS_ERR(flow)) {
  759. error = PTR_ERR(flow);
  760. goto error;
  761. }
  762. flow->key = key;
  763. clear_stats(flow);
  764. /* Obtain actions. */
  765. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  766. error = PTR_ERR(acts);
  767. if (IS_ERR(acts))
  768. goto error_free_flow;
  769. rcu_assign_pointer(flow->sf_acts, acts);
  770. /* Put flow in bucket. */
  771. flow->hash = ovs_flow_hash(&key, key_len);
  772. ovs_flow_tbl_insert(table, flow);
  773. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  774. info->snd_seq,
  775. OVS_FLOW_CMD_NEW);
  776. } else {
  777. /* We found a matching flow. */
  778. struct sw_flow_actions *old_acts;
  779. struct nlattr *acts_attrs;
  780. /* Bail out if we're not allowed to modify an existing flow.
  781. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  782. * because Generic Netlink treats the latter as a dump
  783. * request. We also accept NLM_F_EXCL in case that bug ever
  784. * gets fixed.
  785. */
  786. error = -EEXIST;
  787. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  788. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  789. goto error;
  790. /* Update actions. */
  791. old_acts = rcu_dereference_protected(flow->sf_acts,
  792. lockdep_genl_is_held());
  793. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  794. if (acts_attrs &&
  795. (old_acts->actions_len != nla_len(acts_attrs) ||
  796. memcmp(old_acts->actions, nla_data(acts_attrs),
  797. old_acts->actions_len))) {
  798. struct sw_flow_actions *new_acts;
  799. new_acts = ovs_flow_actions_alloc(acts_attrs);
  800. error = PTR_ERR(new_acts);
  801. if (IS_ERR(new_acts))
  802. goto error;
  803. rcu_assign_pointer(flow->sf_acts, new_acts);
  804. ovs_flow_deferred_free_acts(old_acts);
  805. }
  806. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  807. info->snd_seq, OVS_FLOW_CMD_NEW);
  808. /* Clear stats. */
  809. if (a[OVS_FLOW_ATTR_CLEAR]) {
  810. spin_lock_bh(&flow->lock);
  811. clear_stats(flow);
  812. spin_unlock_bh(&flow->lock);
  813. }
  814. }
  815. if (!IS_ERR(reply))
  816. genl_notify(reply, genl_info_net(info), info->snd_pid,
  817. ovs_dp_flow_multicast_group.id, info->nlhdr,
  818. GFP_KERNEL);
  819. else
  820. netlink_set_err(init_net.genl_sock, 0,
  821. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  822. return 0;
  823. error_free_flow:
  824. ovs_flow_free(flow);
  825. error:
  826. return error;
  827. }
  828. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  829. {
  830. struct nlattr **a = info->attrs;
  831. struct ovs_header *ovs_header = info->userhdr;
  832. struct sw_flow_key key;
  833. struct sk_buff *reply;
  834. struct sw_flow *flow;
  835. struct datapath *dp;
  836. struct flow_table *table;
  837. int err;
  838. int key_len;
  839. if (!a[OVS_FLOW_ATTR_KEY])
  840. return -EINVAL;
  841. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  842. if (err)
  843. return err;
  844. dp = get_dp(ovs_header->dp_ifindex);
  845. if (!dp)
  846. return -ENODEV;
  847. table = genl_dereference(dp->table);
  848. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  849. if (!flow)
  850. return -ENOENT;
  851. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  852. info->snd_seq, OVS_FLOW_CMD_NEW);
  853. if (IS_ERR(reply))
  854. return PTR_ERR(reply);
  855. return genlmsg_reply(reply, info);
  856. }
  857. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  858. {
  859. struct nlattr **a = info->attrs;
  860. struct ovs_header *ovs_header = info->userhdr;
  861. struct sw_flow_key key;
  862. struct sk_buff *reply;
  863. struct sw_flow *flow;
  864. struct datapath *dp;
  865. struct flow_table *table;
  866. int err;
  867. int key_len;
  868. if (!a[OVS_FLOW_ATTR_KEY])
  869. return flush_flows(ovs_header->dp_ifindex);
  870. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  871. if (err)
  872. return err;
  873. dp = get_dp(ovs_header->dp_ifindex);
  874. if (!dp)
  875. return -ENODEV;
  876. table = genl_dereference(dp->table);
  877. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  878. if (!flow)
  879. return -ENOENT;
  880. reply = ovs_flow_cmd_alloc_info(flow);
  881. if (!reply)
  882. return -ENOMEM;
  883. ovs_flow_tbl_remove(table, flow);
  884. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
  885. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  886. BUG_ON(err < 0);
  887. ovs_flow_deferred_free(flow);
  888. genl_notify(reply, genl_info_net(info), info->snd_pid,
  889. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  890. return 0;
  891. }
  892. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  893. {
  894. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  895. struct datapath *dp;
  896. struct flow_table *table;
  897. dp = get_dp(ovs_header->dp_ifindex);
  898. if (!dp)
  899. return -ENODEV;
  900. table = genl_dereference(dp->table);
  901. for (;;) {
  902. struct sw_flow *flow;
  903. u32 bucket, obj;
  904. bucket = cb->args[0];
  905. obj = cb->args[1];
  906. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  907. if (!flow)
  908. break;
  909. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  910. NETLINK_CB(cb->skb).pid,
  911. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  912. OVS_FLOW_CMD_NEW) < 0)
  913. break;
  914. cb->args[0] = bucket;
  915. cb->args[1] = obj;
  916. }
  917. return skb->len;
  918. }
  919. static struct genl_ops dp_flow_genl_ops[] = {
  920. { .cmd = OVS_FLOW_CMD_NEW,
  921. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  922. .policy = flow_policy,
  923. .doit = ovs_flow_cmd_new_or_set
  924. },
  925. { .cmd = OVS_FLOW_CMD_DEL,
  926. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  927. .policy = flow_policy,
  928. .doit = ovs_flow_cmd_del
  929. },
  930. { .cmd = OVS_FLOW_CMD_GET,
  931. .flags = 0, /* OK for unprivileged users. */
  932. .policy = flow_policy,
  933. .doit = ovs_flow_cmd_get,
  934. .dumpit = ovs_flow_cmd_dump
  935. },
  936. { .cmd = OVS_FLOW_CMD_SET,
  937. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  938. .policy = flow_policy,
  939. .doit = ovs_flow_cmd_new_or_set,
  940. },
  941. };
  942. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  943. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  944. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  945. };
  946. static struct genl_family dp_datapath_genl_family = {
  947. .id = GENL_ID_GENERATE,
  948. .hdrsize = sizeof(struct ovs_header),
  949. .name = OVS_DATAPATH_FAMILY,
  950. .version = OVS_DATAPATH_VERSION,
  951. .maxattr = OVS_DP_ATTR_MAX
  952. };
  953. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  954. .name = OVS_DATAPATH_MCGROUP
  955. };
  956. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  957. u32 pid, u32 seq, u32 flags, u8 cmd)
  958. {
  959. struct ovs_header *ovs_header;
  960. struct ovs_dp_stats dp_stats;
  961. int err;
  962. ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
  963. flags, cmd);
  964. if (!ovs_header)
  965. goto error;
  966. ovs_header->dp_ifindex = get_dpifindex(dp);
  967. rcu_read_lock();
  968. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  969. rcu_read_unlock();
  970. if (err)
  971. goto nla_put_failure;
  972. get_dp_stats(dp, &dp_stats);
  973. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  974. goto nla_put_failure;
  975. return genlmsg_end(skb, ovs_header);
  976. nla_put_failure:
  977. genlmsg_cancel(skb, ovs_header);
  978. error:
  979. return -EMSGSIZE;
  980. }
  981. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
  982. u32 seq, u8 cmd)
  983. {
  984. struct sk_buff *skb;
  985. int retval;
  986. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  987. if (!skb)
  988. return ERR_PTR(-ENOMEM);
  989. retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
  990. if (retval < 0) {
  991. kfree_skb(skb);
  992. return ERR_PTR(retval);
  993. }
  994. return skb;
  995. }
  996. /* Called with genl_mutex and optionally with RTNL lock also. */
  997. static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
  998. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  999. {
  1000. struct datapath *dp;
  1001. if (!a[OVS_DP_ATTR_NAME])
  1002. dp = get_dp(ovs_header->dp_ifindex);
  1003. else {
  1004. struct vport *vport;
  1005. rcu_read_lock();
  1006. vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
  1007. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1008. rcu_read_unlock();
  1009. }
  1010. return dp ? dp : ERR_PTR(-ENODEV);
  1011. }
  1012. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1013. {
  1014. struct nlattr **a = info->attrs;
  1015. struct vport_parms parms;
  1016. struct sk_buff *reply;
  1017. struct datapath *dp;
  1018. struct vport *vport;
  1019. int err;
  1020. err = -EINVAL;
  1021. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1022. goto err;
  1023. rtnl_lock();
  1024. err = -ENODEV;
  1025. if (!try_module_get(THIS_MODULE))
  1026. goto err_unlock_rtnl;
  1027. err = -ENOMEM;
  1028. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1029. if (dp == NULL)
  1030. goto err_put_module;
  1031. INIT_LIST_HEAD(&dp->port_list);
  1032. /* Allocate table. */
  1033. err = -ENOMEM;
  1034. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1035. if (!dp->table)
  1036. goto err_free_dp;
  1037. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1038. if (!dp->stats_percpu) {
  1039. err = -ENOMEM;
  1040. goto err_destroy_table;
  1041. }
  1042. /* Set up our datapath device. */
  1043. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1044. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1045. parms.options = NULL;
  1046. parms.dp = dp;
  1047. parms.port_no = OVSP_LOCAL;
  1048. parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1049. vport = new_vport(&parms);
  1050. if (IS_ERR(vport)) {
  1051. err = PTR_ERR(vport);
  1052. if (err == -EBUSY)
  1053. err = -EEXIST;
  1054. goto err_destroy_percpu;
  1055. }
  1056. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1057. info->snd_seq, OVS_DP_CMD_NEW);
  1058. err = PTR_ERR(reply);
  1059. if (IS_ERR(reply))
  1060. goto err_destroy_local_port;
  1061. list_add_tail(&dp->list_node, &dps);
  1062. rtnl_unlock();
  1063. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1064. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1065. GFP_KERNEL);
  1066. return 0;
  1067. err_destroy_local_port:
  1068. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1069. err_destroy_percpu:
  1070. free_percpu(dp->stats_percpu);
  1071. err_destroy_table:
  1072. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1073. err_free_dp:
  1074. kfree(dp);
  1075. err_put_module:
  1076. module_put(THIS_MODULE);
  1077. err_unlock_rtnl:
  1078. rtnl_unlock();
  1079. err:
  1080. return err;
  1081. }
  1082. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1083. {
  1084. struct vport *vport, *next_vport;
  1085. struct sk_buff *reply;
  1086. struct datapath *dp;
  1087. int err;
  1088. rtnl_lock();
  1089. dp = lookup_datapath(info->userhdr, info->attrs);
  1090. err = PTR_ERR(dp);
  1091. if (IS_ERR(dp))
  1092. goto exit_unlock;
  1093. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1094. info->snd_seq, OVS_DP_CMD_DEL);
  1095. err = PTR_ERR(reply);
  1096. if (IS_ERR(reply))
  1097. goto exit_unlock;
  1098. list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
  1099. if (vport->port_no != OVSP_LOCAL)
  1100. ovs_dp_detach_port(vport);
  1101. list_del(&dp->list_node);
  1102. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1103. /* rtnl_unlock() will wait until all the references to devices that
  1104. * are pending unregistration have been dropped. We do it here to
  1105. * ensure that any internal devices (which contain DP pointers) are
  1106. * fully destroyed before freeing the datapath.
  1107. */
  1108. rtnl_unlock();
  1109. call_rcu(&dp->rcu, destroy_dp_rcu);
  1110. module_put(THIS_MODULE);
  1111. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1112. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1113. GFP_KERNEL);
  1114. return 0;
  1115. exit_unlock:
  1116. rtnl_unlock();
  1117. return err;
  1118. }
  1119. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1120. {
  1121. struct sk_buff *reply;
  1122. struct datapath *dp;
  1123. int err;
  1124. dp = lookup_datapath(info->userhdr, info->attrs);
  1125. if (IS_ERR(dp))
  1126. return PTR_ERR(dp);
  1127. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1128. info->snd_seq, OVS_DP_CMD_NEW);
  1129. if (IS_ERR(reply)) {
  1130. err = PTR_ERR(reply);
  1131. netlink_set_err(init_net.genl_sock, 0,
  1132. ovs_dp_datapath_multicast_group.id, err);
  1133. return 0;
  1134. }
  1135. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1136. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1137. GFP_KERNEL);
  1138. return 0;
  1139. }
  1140. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1141. {
  1142. struct sk_buff *reply;
  1143. struct datapath *dp;
  1144. dp = lookup_datapath(info->userhdr, info->attrs);
  1145. if (IS_ERR(dp))
  1146. return PTR_ERR(dp);
  1147. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1148. info->snd_seq, OVS_DP_CMD_NEW);
  1149. if (IS_ERR(reply))
  1150. return PTR_ERR(reply);
  1151. return genlmsg_reply(reply, info);
  1152. }
  1153. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1154. {
  1155. struct datapath *dp;
  1156. int skip = cb->args[0];
  1157. int i = 0;
  1158. list_for_each_entry(dp, &dps, list_node) {
  1159. if (i >= skip &&
  1160. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
  1161. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1162. OVS_DP_CMD_NEW) < 0)
  1163. break;
  1164. i++;
  1165. }
  1166. cb->args[0] = i;
  1167. return skb->len;
  1168. }
  1169. static struct genl_ops dp_datapath_genl_ops[] = {
  1170. { .cmd = OVS_DP_CMD_NEW,
  1171. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1172. .policy = datapath_policy,
  1173. .doit = ovs_dp_cmd_new
  1174. },
  1175. { .cmd = OVS_DP_CMD_DEL,
  1176. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1177. .policy = datapath_policy,
  1178. .doit = ovs_dp_cmd_del
  1179. },
  1180. { .cmd = OVS_DP_CMD_GET,
  1181. .flags = 0, /* OK for unprivileged users. */
  1182. .policy = datapath_policy,
  1183. .doit = ovs_dp_cmd_get,
  1184. .dumpit = ovs_dp_cmd_dump
  1185. },
  1186. { .cmd = OVS_DP_CMD_SET,
  1187. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1188. .policy = datapath_policy,
  1189. .doit = ovs_dp_cmd_set,
  1190. },
  1191. };
  1192. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1193. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1194. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1195. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1196. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1197. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1198. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1199. };
  1200. static struct genl_family dp_vport_genl_family = {
  1201. .id = GENL_ID_GENERATE,
  1202. .hdrsize = sizeof(struct ovs_header),
  1203. .name = OVS_VPORT_FAMILY,
  1204. .version = OVS_VPORT_VERSION,
  1205. .maxattr = OVS_VPORT_ATTR_MAX
  1206. };
  1207. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1208. .name = OVS_VPORT_MCGROUP
  1209. };
  1210. /* Called with RTNL lock or RCU read lock. */
  1211. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1212. u32 pid, u32 seq, u32 flags, u8 cmd)
  1213. {
  1214. struct ovs_header *ovs_header;
  1215. struct ovs_vport_stats vport_stats;
  1216. int err;
  1217. ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
  1218. flags, cmd);
  1219. if (!ovs_header)
  1220. return -EMSGSIZE;
  1221. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1222. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1223. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1224. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1225. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid))
  1226. goto nla_put_failure;
  1227. ovs_vport_get_stats(vport, &vport_stats);
  1228. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1229. &vport_stats))
  1230. goto nla_put_failure;
  1231. err = ovs_vport_get_options(vport, skb);
  1232. if (err == -EMSGSIZE)
  1233. goto error;
  1234. return genlmsg_end(skb, ovs_header);
  1235. nla_put_failure:
  1236. err = -EMSGSIZE;
  1237. error:
  1238. genlmsg_cancel(skb, ovs_header);
  1239. return err;
  1240. }
  1241. /* Called with RTNL lock or RCU read lock. */
  1242. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
  1243. u32 seq, u8 cmd)
  1244. {
  1245. struct sk_buff *skb;
  1246. int retval;
  1247. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1248. if (!skb)
  1249. return ERR_PTR(-ENOMEM);
  1250. retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
  1251. if (retval < 0) {
  1252. kfree_skb(skb);
  1253. return ERR_PTR(retval);
  1254. }
  1255. return skb;
  1256. }
  1257. /* Called with RTNL lock or RCU read lock. */
  1258. static struct vport *lookup_vport(struct ovs_header *ovs_header,
  1259. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1260. {
  1261. struct datapath *dp;
  1262. struct vport *vport;
  1263. if (a[OVS_VPORT_ATTR_NAME]) {
  1264. vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
  1265. if (!vport)
  1266. return ERR_PTR(-ENODEV);
  1267. if (ovs_header->dp_ifindex &&
  1268. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1269. return ERR_PTR(-ENODEV);
  1270. return vport;
  1271. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1272. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1273. if (port_no >= DP_MAX_PORTS)
  1274. return ERR_PTR(-EFBIG);
  1275. dp = get_dp(ovs_header->dp_ifindex);
  1276. if (!dp)
  1277. return ERR_PTR(-ENODEV);
  1278. vport = rcu_dereference_rtnl(dp->ports[port_no]);
  1279. if (!vport)
  1280. return ERR_PTR(-ENOENT);
  1281. return vport;
  1282. } else
  1283. return ERR_PTR(-EINVAL);
  1284. }
  1285. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1286. {
  1287. struct nlattr **a = info->attrs;
  1288. struct ovs_header *ovs_header = info->userhdr;
  1289. struct vport_parms parms;
  1290. struct sk_buff *reply;
  1291. struct vport *vport;
  1292. struct datapath *dp;
  1293. u32 port_no;
  1294. int err;
  1295. err = -EINVAL;
  1296. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1297. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1298. goto exit;
  1299. rtnl_lock();
  1300. dp = get_dp(ovs_header->dp_ifindex);
  1301. err = -ENODEV;
  1302. if (!dp)
  1303. goto exit_unlock;
  1304. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1305. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1306. err = -EFBIG;
  1307. if (port_no >= DP_MAX_PORTS)
  1308. goto exit_unlock;
  1309. vport = rtnl_dereference(dp->ports[port_no]);
  1310. err = -EBUSY;
  1311. if (vport)
  1312. goto exit_unlock;
  1313. } else {
  1314. for (port_no = 1; ; port_no++) {
  1315. if (port_no >= DP_MAX_PORTS) {
  1316. err = -EFBIG;
  1317. goto exit_unlock;
  1318. }
  1319. vport = rtnl_dereference(dp->ports[port_no]);
  1320. if (!vport)
  1321. break;
  1322. }
  1323. }
  1324. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1325. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1326. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1327. parms.dp = dp;
  1328. parms.port_no = port_no;
  1329. parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1330. vport = new_vport(&parms);
  1331. err = PTR_ERR(vport);
  1332. if (IS_ERR(vport))
  1333. goto exit_unlock;
  1334. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1335. OVS_VPORT_CMD_NEW);
  1336. if (IS_ERR(reply)) {
  1337. err = PTR_ERR(reply);
  1338. ovs_dp_detach_port(vport);
  1339. goto exit_unlock;
  1340. }
  1341. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1342. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1343. exit_unlock:
  1344. rtnl_unlock();
  1345. exit:
  1346. return err;
  1347. }
  1348. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1349. {
  1350. struct nlattr **a = info->attrs;
  1351. struct sk_buff *reply;
  1352. struct vport *vport;
  1353. int err;
  1354. rtnl_lock();
  1355. vport = lookup_vport(info->userhdr, a);
  1356. err = PTR_ERR(vport);
  1357. if (IS_ERR(vport))
  1358. goto exit_unlock;
  1359. err = 0;
  1360. if (a[OVS_VPORT_ATTR_TYPE] &&
  1361. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1362. err = -EINVAL;
  1363. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1364. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1365. if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
  1366. vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1367. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1368. OVS_VPORT_CMD_NEW);
  1369. if (IS_ERR(reply)) {
  1370. netlink_set_err(init_net.genl_sock, 0,
  1371. ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
  1372. goto exit_unlock;
  1373. }
  1374. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1375. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1376. exit_unlock:
  1377. rtnl_unlock();
  1378. return err;
  1379. }
  1380. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1381. {
  1382. struct nlattr **a = info->attrs;
  1383. struct sk_buff *reply;
  1384. struct vport *vport;
  1385. int err;
  1386. rtnl_lock();
  1387. vport = lookup_vport(info->userhdr, a);
  1388. err = PTR_ERR(vport);
  1389. if (IS_ERR(vport))
  1390. goto exit_unlock;
  1391. if (vport->port_no == OVSP_LOCAL) {
  1392. err = -EINVAL;
  1393. goto exit_unlock;
  1394. }
  1395. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1396. OVS_VPORT_CMD_DEL);
  1397. err = PTR_ERR(reply);
  1398. if (IS_ERR(reply))
  1399. goto exit_unlock;
  1400. ovs_dp_detach_port(vport);
  1401. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1402. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1403. exit_unlock:
  1404. rtnl_unlock();
  1405. return err;
  1406. }
  1407. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1408. {
  1409. struct nlattr **a = info->attrs;
  1410. struct ovs_header *ovs_header = info->userhdr;
  1411. struct sk_buff *reply;
  1412. struct vport *vport;
  1413. int err;
  1414. rcu_read_lock();
  1415. vport = lookup_vport(ovs_header, a);
  1416. err = PTR_ERR(vport);
  1417. if (IS_ERR(vport))
  1418. goto exit_unlock;
  1419. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1420. OVS_VPORT_CMD_NEW);
  1421. err = PTR_ERR(reply);
  1422. if (IS_ERR(reply))
  1423. goto exit_unlock;
  1424. rcu_read_unlock();
  1425. return genlmsg_reply(reply, info);
  1426. exit_unlock:
  1427. rcu_read_unlock();
  1428. return err;
  1429. }
  1430. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1431. {
  1432. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1433. struct datapath *dp;
  1434. u32 port_no;
  1435. int retval;
  1436. dp = get_dp(ovs_header->dp_ifindex);
  1437. if (!dp)
  1438. return -ENODEV;
  1439. rcu_read_lock();
  1440. for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
  1441. struct vport *vport;
  1442. vport = rcu_dereference(dp->ports[port_no]);
  1443. if (!vport)
  1444. continue;
  1445. if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
  1446. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1447. OVS_VPORT_CMD_NEW) < 0)
  1448. break;
  1449. }
  1450. rcu_read_unlock();
  1451. cb->args[0] = port_no;
  1452. retval = skb->len;
  1453. return retval;
  1454. }
  1455. static void rehash_flow_table(struct work_struct *work)
  1456. {
  1457. struct datapath *dp;
  1458. genl_lock();
  1459. list_for_each_entry(dp, &dps, list_node) {
  1460. struct flow_table *old_table = genl_dereference(dp->table);
  1461. struct flow_table *new_table;
  1462. new_table = ovs_flow_tbl_rehash(old_table);
  1463. if (!IS_ERR(new_table)) {
  1464. rcu_assign_pointer(dp->table, new_table);
  1465. ovs_flow_tbl_deferred_destroy(old_table);
  1466. }
  1467. }
  1468. genl_unlock();
  1469. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1470. }
  1471. static struct genl_ops dp_vport_genl_ops[] = {
  1472. { .cmd = OVS_VPORT_CMD_NEW,
  1473. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1474. .policy = vport_policy,
  1475. .doit = ovs_vport_cmd_new
  1476. },
  1477. { .cmd = OVS_VPORT_CMD_DEL,
  1478. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1479. .policy = vport_policy,
  1480. .doit = ovs_vport_cmd_del
  1481. },
  1482. { .cmd = OVS_VPORT_CMD_GET,
  1483. .flags = 0, /* OK for unprivileged users. */
  1484. .policy = vport_policy,
  1485. .doit = ovs_vport_cmd_get,
  1486. .dumpit = ovs_vport_cmd_dump
  1487. },
  1488. { .cmd = OVS_VPORT_CMD_SET,
  1489. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1490. .policy = vport_policy,
  1491. .doit = ovs_vport_cmd_set,
  1492. },
  1493. };
  1494. struct genl_family_and_ops {
  1495. struct genl_family *family;
  1496. struct genl_ops *ops;
  1497. int n_ops;
  1498. struct genl_multicast_group *group;
  1499. };
  1500. static const struct genl_family_and_ops dp_genl_families[] = {
  1501. { &dp_datapath_genl_family,
  1502. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1503. &ovs_dp_datapath_multicast_group },
  1504. { &dp_vport_genl_family,
  1505. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1506. &ovs_dp_vport_multicast_group },
  1507. { &dp_flow_genl_family,
  1508. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1509. &ovs_dp_flow_multicast_group },
  1510. { &dp_packet_genl_family,
  1511. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1512. NULL },
  1513. };
  1514. static void dp_unregister_genl(int n_families)
  1515. {
  1516. int i;
  1517. for (i = 0; i < n_families; i++)
  1518. genl_unregister_family(dp_genl_families[i].family);
  1519. }
  1520. static int dp_register_genl(void)
  1521. {
  1522. int n_registered;
  1523. int err;
  1524. int i;
  1525. n_registered = 0;
  1526. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1527. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1528. err = genl_register_family_with_ops(f->family, f->ops,
  1529. f->n_ops);
  1530. if (err)
  1531. goto error;
  1532. n_registered++;
  1533. if (f->group) {
  1534. err = genl_register_mc_group(f->family, f->group);
  1535. if (err)
  1536. goto error;
  1537. }
  1538. }
  1539. return 0;
  1540. error:
  1541. dp_unregister_genl(n_registered);
  1542. return err;
  1543. }
  1544. static int __init dp_init(void)
  1545. {
  1546. struct sk_buff *dummy_skb;
  1547. int err;
  1548. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
  1549. pr_info("Open vSwitch switching datapath\n");
  1550. err = ovs_flow_init();
  1551. if (err)
  1552. goto error;
  1553. err = ovs_vport_init();
  1554. if (err)
  1555. goto error_flow_exit;
  1556. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1557. if (err)
  1558. goto error_vport_exit;
  1559. err = dp_register_genl();
  1560. if (err < 0)
  1561. goto error_unreg_notifier;
  1562. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1563. return 0;
  1564. error_unreg_notifier:
  1565. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1566. error_vport_exit:
  1567. ovs_vport_exit();
  1568. error_flow_exit:
  1569. ovs_flow_exit();
  1570. error:
  1571. return err;
  1572. }
  1573. static void dp_cleanup(void)
  1574. {
  1575. cancel_delayed_work_sync(&rehash_flow_wq);
  1576. rcu_barrier();
  1577. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1578. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1579. ovs_vport_exit();
  1580. ovs_flow_exit();
  1581. }
  1582. module_init(dp_init);
  1583. module_exit(dp_cleanup);
  1584. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1585. MODULE_LICENSE("GPL");