datapath.c 46 KB

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